rsync –inplace /vmware
So I’ve been evaluating our backup procedures. Of course I’d like to go ahead and buy some software and do it all proper, but the $$’s not there, so I’m just trying to improve the free solutions we have at our disposal. So browsing around the IntraWeb I found some more info about rsync, which I’ve used for backups before. Specifically the “–inplace” options, which would just sync the difference between two large files instead of deleting the old file and copying the new one. So I wrote a new backup script. The new script checks to see if our virtual machines are up and running and if they are it suspends them, then mounts the necessary partitions, uses rsync –inplace to sync the VMDK files, then starts the VM’s back up if they were running before the whole thing started. When all is said and done it emails me the logs. I’m backing up over ISCSI, so I added a bit to make sure the iscsi-initiator actually connects, and if not to stop the backup process, restart the VM’s and email me. Anyway, it’s still a work in progress, needs cleaned up and probably a bit more error-checking. I run it nightly via cron by invoking:
backupservers > /var/log/backup.log 2>> /var/log/backup.log
This starts it up and sends all the output to /var/log/backup.log which gets emailed to me when it’s done. It’s not eloquent, but here’s the code:
#!/bin/bash
#let’s make some variables, kinda messy right now, need to clean up later
stateon=”getstate() = on”
stateoff=”getstate() = off”
statesusp=”getstate() = suspended”
vmdir=”/vm”
S1=”PDC”
S1dir=”/PDC/”
S1dev=”/dev/sdb1″
S2=”LAMP.vmx”
S2dir=”/LAMP/”
S2dev=”/dev/sdb2″
CMD=”/usr/bin/vmware-cmd”
backupdir=”/mnt/Backup”
rys=”/usr/bin/rsync –inplace -v”
log=”/var/log/backup.log”
EMAIL=”bmedlock@chcchurch.org”#Send an email to let you know how it went
function email {
/bin/mail -s “$SUBJECT” “$EMAIL” < $log
}#Exit if something goes wrong
function quit {
exit
}/bin/date
for i in `seq 1 2`;
do
#Which Server to backup
if [ $i == 1 ]
then
dir=$vmdir$S1dir
server=$dir$S1
dev=$S1dev
else
dir=$vmdir$S2dir
server=$dir$S2
dev=$S2dev
fi
echo “Suspend the VM if it’s running\n”
#Now let’s check to see if the VM is running
state=`$CMD $server getstate`
echo -e “The current state of $server is $state\n”
if [ "$state" = "$stateon" ]
then
$CMD $server suspend
echo -e “$server is now suspended\n”
fi#mount the backup directory
umount $backupdir
mount $dev $backupdir
status=$?
if [ $status != 0 ]
then
/root/iscsilogout
/root/iscsilogin
sleep 2
mount $dev $backupdir
status=$?
if [ $status != 0 ]
then
SUBJECT=”mount failed with status $status”
$CMD $server start
quit
fi
fi
#now let’s back it up
echo -e “Starting backup of $dir\n”
nice -n -15 $rys $dir* $backupdir
echo -e “The Server at $server is now backed up\n”#let’s clean up
umount $backupdir
if [ "$state" = "$stateon" ]
then
$CMD $server start
echo -e “Server restarted\n”
else
echo -e “The server wasn’t on to begin with, so why start again?\n”
fidone
/bin/date
echo -e “Backup Complete\n”
SUBJECT=”Backup Complete”
This is fantastic Bryson! Now for the big question: Have you recovered a VM from your insync backup where you know you have made changes that had to be received by the insync process, and then tried to come up on the restored files? Does it actually work is the question?
???? I hope it does. If so, I will start backing up my VMs this way.