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:


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

# Backup directory without trailing slash

# temp dir where we ZFS send and gzip before moving to backupdir

svcadm enable autofs

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

    # create some helper varibles for alias and uuid

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

    # echo "Starting $VM"
    vmadm start $uuid

    # disk space is cheap, uncomment if you disagree.
    #pbzip2 $tmpdir/$alias


mv $tmpdir/*.bz2 $backupdir

Create a cronjob entry to schedule the backups:

crontab -e
2 6 * * 0 /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 allows me to 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:

# decompress the backup file.
pbzip2 -d backup-file.bz2

# ingest the backup file into the hypervisor.
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.

Looking for a better comment system?

You should try Remarkbox — a hosted comment service that embeds in your pages to keep the conversation in the same place as your content. It works everywhere, even static sites!

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

© Russell Ballestrini.