Random   •   Archives   •   RSS   •   About   •   Contact

Backup all virtual machines on a SmartOS hypervisor with smart-back.sh

This post will explain how to create a cronjob to backup of every virtual machine on a SmartOS hypervisor.

Create the following bash script in /opt/smart-back.sh:

#!/usr/bin/bash

# Backup all virtual machines on a SmartOS hypervisor
# Author:  russell@ballestrini.net
# Website: http://russell.ballestrini.net/

# Backup directory without trailing slash
backupdir=/opt/backups

# temp dir where we ZFS send and gzip before moving to backupdir
tmpdir=/opt

svcadm enable autofs

for VM in `vmadm list -p -o alias,uuid`
  do
    # create an array called VM_PARTS splitting on ':'
    IFS=':' VM_PARTS=($VM)

    # create some helper varibles for alias and uuid
    alias=${VM_PARTS[0]}
    uuid=${VM_PARTS[1]}

    # echo "Backup started for $VM"
    vmadm send $uuid > $tmpdir/$alias

    # echo "Starting $VM"
    vmadm start $uuid

    gzip $tmpdir/$alias
    mv $tmpdir/$alias.gz $backupdir/$alias.gz

  done

Create a cronjob entry to schedule the backups:

crontab -e
2 15 * * * /usr/bin/bash /opt/smart-back.sh

If I expand on this script much more, I plan to stick it into revision control.

If you look closely, I have also added a hack to enable autofs (svcadm enable autofs) which lets me automount an NFS share on my remote FreeNAS by setting backupdir=/net/[ip-or-fqdn-of-freenas]/mnt/zfs-mirror/backup/vms.

We have scheduled a backup of each virtual machine on your SmartOS hypervisor!

If or when the time comes to restore a VM from a backup, use the following:

vmadm receive -f /path/to/backup-file

Just make sure the VM doesn't currently exist on the hypervisor.

This strategy is great for complete backups of machines which could be used during a manual migration, or if corruption happened to the VM and we wanted to restore to a previous version.



© Russell Ballestrini.