diff --git a/vmbackup.sh b/vmbackup.sh index 3a7a30d..754694a 100755 --- a/vmbackup.sh +++ b/vmbackup.sh @@ -3,8 +3,10 @@ set -e set -u -statusfile=/home/cig/vmbackup.status +statusfile=/var/run/zabbix/vmbackup.status basedir=/mnt/backups/vms +vbm="echo sudo -u cig vboxmanage" +rsync="echo rsync" _exit () { echo $1 > $statusfile @@ -15,7 +17,7 @@ _mail () { echo "$@" | mail -s "$(hostname -f) : vm backup error" root } -mount /mnt/backups &> /dev/null +mount /mnt/backups &> /dev/null || _exit "mount failed" if [ ! -d $basedir ] ; then _mail Dir $basedir not mounted @@ -32,7 +34,7 @@ fi function checkRunning { local machine=$1 - vboxmanage list runningvms | grep -q $machine + sudo -u cig vboxmanage list runningvms | grep -q $machine return $? } @@ -43,15 +45,16 @@ if ( checkRunning $machine ) ; then mv ${basedir}/current ${basedir}/old ln -s ${outputdir} ${basedir}/current test $? -eq 0 || { _mail "erreur de copie" ; _exit 44 ; } - vboxmanage controlvm $machine acpipowerbutton shutdown + $vbm controlvm $machine acpipowerbutton shutdown sleep 60 - rsync --inplace -a /srv/VM/Win/roofline ${outputdir}/ + $rsync --inplace -a /srv/VM/Win/roofline ${outputdir}/ ret=$? test $ret -eq 0 || _mail "rsync error $ret" - vboxmanage startvm $machine + $vbm startvm $machine ln -s ${outputdir} ${basedir}/current test $ret -eq 0 && find ${basedir} -maxdepth 1 -type d -mtime +7 -exec echo rm -fr {} \; - umount /mnt/backups &> /dev/null + umount /mnt/backups &> /dev/null || _mail "umount failed" + _mail "backup done with status $ret" _exit $ret fi _mail "vm $machine not running"