seems to work when disk is not mounted and vm works

This commit is contained in:
cig
2022-09-15 22:42:16 +02:00
committed by root
parent 5be3d73987
commit fb3f38d77d

View File

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