diff --git a/vmbackup.sh b/vmbackup.sh index 754694a..b0f31f8 100755 --- a/vmbackup.sh +++ b/vmbackup.sh @@ -5,23 +5,23 @@ set -u statusfile=/var/run/zabbix/vmbackup.status basedir=/mnt/backups/vms -vbm="echo sudo -u cig vboxmanage" -rsync="echo rsync" +vbm="sudo -u cig vboxmanage" +rsync="rsync" _exit () { echo $1 > $statusfile - exit $1 + exit $2 } _mail () { echo "$@" | mail -s "$(hostname -f) : vm backup error" root } -mount /mnt/backups &> /dev/null || _exit "mount failed" +mount /mnt/backups &> /dev/null || _exit "mount failed" 2 if [ ! -d $basedir ] ; then _mail Dir $basedir not mounted - _exit 41 + _exit "no base dir" 41 fi outputdir=${basedir}/$(date -I) @@ -29,33 +29,32 @@ outputdir=${basedir}/$(date -I) if [ $# -ne 1 ] ; then echo "Usage: $O vm" echo "Stop, backup and start given vm" - _exit 42 + _exit "Usage" 42 fi function checkRunning { local machine=$1 - sudo -u cig vboxmanage list runningvms | grep -q $machine + $vbm list runningvms | grep -q $machine return $? } +function _shutdown_vm { + net rpc shutdown -I 192.168.69.250 -U tom%123Soleil2022! + sleep 30 +} + machine=$1 if ( checkRunning $machine ) ; then mkdir -p ${outputdir} - cp -ar ${basedir}/current/. --target-directory=${outputdir} - mv ${basedir}/current ${basedir}/old - ln -s ${outputdir} ${basedir}/current - test $? -eq 0 || { _mail "erreur de copie" ; _exit 44 ; } - $vbm controlvm $machine acpipowerbutton shutdown - sleep 60 - $rsync --inplace -a /srv/VM/Win/roofline ${outputdir}/ + _shutdown_vm + $rsync --inplace --delete --delete-after -a /srv/VM/Win/roofline ${outputdir}/ ret=$? test $ret -eq 0 || _mail "rsync error $ret" $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 || _mail "umount failed" _mail "backup done with status $ret" - _exit $ret + _exit "backup done" $ret fi _mail "vm $machine not running" _exit 43