#!/bin/bash
# based on http://blog.beulink.org/smartos-migrate-backup-and-restore-kvm-vms/
ALIASES=($(vmadm list -o alias | awk '{if(NR>1)print}'))
UUIDS=($(vmadm list -o uuid | awk '{if(NR>1)print}'))
BACKUP_PATH=/zones/backup
for i in "${!UUIDS[@]}"
do
UUID=${UUIDS[$i]}
ALIAS=${ALIASES[$i]}
LABEL=${ALIAS}_${UUID}
echo "backup ${LABEL}"
vmadm get ${UUID} | json zfs_filesystem disks
mkdir -p $BACKUP_PATH/${LABEL}
vmadm get ${UUID} > $BACKUP_PATH/${LABEL}/${LABEL}.json
vmadm stop ${UUID}
zfs snapshot zones/${UUID}@sending
zfs send -p zones/${UUID}@sending > $BACKUP_PATH/${LABEL}/${LABEL}.zfs
zfs destroy zones/${UUID}@sending
tar -czf $BACKUP_PATH/${LABEL}/${LABEL}.zfs.tar.gz $BACKUP_PATH/${LABEL}/${LABEL}.zfs
openssl dgst -md5 $BACKUP_PATH/${LABEL}/${LABEL}.zfs > $BACKUP_PATH/${LABEL}/${LABEL}.zfs.md5
rm $BACKUP_PATH/${LABEL}/${LABEL}.zfs
done