Backing up Virtual Machines (or other large files) on OS X

For my Oracle based lab configurations i use virtualization software on my Mac. I have used VMWare Fusion in the past, currently use Parallels Desktop and am considering to switch to Oracle Virtualbox in the near future. Since we have multiple Macs in our household an old mini running OS X server with a big external harddisk (WD MyBook Studio) serves as a Time Machine backup server. Time Machine which is part of OS X is a perfect tool for regular backups but for two reasons really useless for making backups of Virtual Machines. First, most of my virtual machines have virtual disks which are 10GB to 40GB in size. Most of them are not split in 2GB files, therefore starting a VM will immediately result in a new Time Machine backup of over 40GB if these virtual machines are not excluded from the backup. Secondly, for most virtualization software the VM consists of multiple configuration, disk and memory files. All these files are bundled in a Package, inconsistency between one or more of these files makes the whole VM corrupt, and since backing up all files of one VM can take a substantial amount of time the chance that you only have a partial backup of your VM using Time Machine is substantial. Therefore i excluded all my VirtualMachines from Time Machine and rely on RSync to sync my VMs to a destination folder on another Mac. In my case this is the same OS X server i use for the Time Machine backups, but you can use any other Mac with File Sharing enabled. I prefer to use the same account to access both Macs, but you could backup your VMs on your source machine under account 'a' to a folder owned by account 'b' on a destination machine. It takes a few simple steps to sync your VMs easely and much faster.

- Open Terminal on your source Mac under the account you want to make the backups from
- First you have to create a public and private key, type the instruction below. You can replace the comment "rsync" with any comment, if you want to explore the possibilities of ssh-keygen just type man ssh-keygen.
ssh-keygen -t dsa -f ~/.ssh/id_dsa -C "rsync"
You will be requested to fill in a random passphrase. Just type one, but keep this one in mind, because you will need it later.
- Now the public key needs to be copied to your destination Mac. To be able to do this, Remote Login on your destination must be enabled.
If not enabled you can do this from System Preferences/Sharing. Copy your public key by executing the instruction below where {user} and {remote host} are the username and ip-address of the Mac used as the destination for you backups.
cat ~/.ssh/ | ssh {user}@{remote host} 'cat - >> ~/.ssh/authorized_keys'
- Now create on the destination Mac a folder where you want to sync your VMs to, e.g. /Volumes/MyBook/VMBackup
- I am lazy, so want to sync my VMs using a single click. If you like this too, create a folder in your /Applications folder, mine is called syncfolders. You can create the folder by typing mkdir /Applications/Syncfolders.
- Change to this new folder with typing the instruction cd /Applications/syncfolders.
Create the backup script syncfolders.command using vi, vim or pico and paste the script below into it. Although the script is a Bash script, make sure to use the .command extension for the file. Using the .command extension it enables you to start the script from the finder. Now replace the values for the exported variables TUSER (username target Mac) and TIP (IP address of your target Mac). And finally specify the folders to sync to which target folder on your destination Mac by adding a line below the line ## Call syncfolder. Some example lines are added, basically you have to call the syncfolder function with the parameters sourcefolder and targetfolder. So adding the line syncfolder ~/Documents/Parallels ~/Documents would sync your folder Parallels in Documents to the Documents folder on your destination Mac.

export TUSER="clockwise"
export TIP=""

function syncfolder {
 targetfolder=`echo $targetdir | sed "s#^.*\/\(.*\)#\1#"`

echo Start RSync from "${sourcedir}"
/usr/bin/rsync --archive --hard-links --one-file-system --compress --progress --exclude-from=syncfolders.exclude --log-file=${sourcedir}/rsync.log --rsync-path="/usr/bin/rsync" ${sourcedir} ${TUSER}@${TIP}:${targetdir}
if [ $? == 0 ] ; then
 echo "Successful"
 echo "Some errors $!"
echo scp ${sourcedir}/rsync.log ${TUSER}@${TIP}:${targetdir}/${targetfolder}


## Call syncfolder 
#synchronize from local folder /Volumes/Chigatze_II to folder /Volumes/MyBook/VMBackup on target Mac
#syncfolder /Volumes/Chigatze_II /Volumes/MyBook/VMBackup
#synchronize from local folder /Users/petervannes/Documents/Parallels to folder /Volumes/MyBook/VMBackup on target Mac
#syncfolder /Users/petervannes/Documents/Parallels /Volumes/MyBook/VMBackup
- Create a second file syncfolders.exclude in the same folder as syncfolders.command. In this file you can add files to be excluded from the backup. The example below excludes document revisions, deleted files, spotlight indexes and the logfile written by the syncfolders.command script.
### Sample Sync Exclude file list syncfolders.exclude
- You are done! Start your first synchronization by executing syncfolders.command from Applications/Syncfolders/syncfolders.command. The first time it will take some time to synchronize the folders, but subsequentl synchronizations will only take a fraction of the time.

- One final tip. I you want to make it a little bit prettier you can assign an icon to the .command script. On OS X 10.8 (Mountain Lion) you can find some nice icons in the folder /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources. You can open this folder by selecting Finder in your dock, press shift-command-G to open the go-to folder menu. Enter the foldername here and press Go. Open the folder containing the .command script in another finder window, select the .command script and press command+I. Now drag any icon over the existing icon of the .command script in the info window.

Screen Shot 2013-02-25 at 22.35.05

blog comments powered by Disqus