#!/bin/bash OSUSER=vbuser VMNAME=vmname VMFILE=/mnt/share/CentOS7.vdi VMBACKUP=/mnt/share/backup/CentOS7.vdi VBOXFILE=/mnt/share/CentOS7.vbox VBOXBACKUP=/mnt/share/backup/CentOS7.vbox DATETIME=`date "+%Y-%m-%d %H:%M:%S"` echo -e "\n$DATETIME\n" >> /var/log/backup-vm.log 2>&1 echo -e "saving state for $VMNAME...\n" >> /var/log/backup-vm.log 2>&1 sudo -u $OSUSER VBoxManage controlvm $VMNAME savestate >> /var/log/backup-vm.log 2>&1 sleep 30 echo -e "starting backup of vmdisk..." >> /var/log/backup-vm.log 2>&1 sudo -u $OSUSER cp -f $VMFILE $VMBACKUP sudo -u $OSUSER cp -f $VBOXFILE $VBOXBACKUP sleep 10 echo -e "\nrestarting $VMNAME...\n" >> /var/log/backup-vm.log 2>&1 sudo -u $OSUSER VBoxManage startvm $VMNAME --type headless >> /var/log/backup-vm.log 2>&1