#!/usr/local/bin/bash ## config section vm=$1 vm_dataset="zroot/vms" vm_pid=`/usr/bin/pgrep -f "bhyve: $vm"` backup_location="/tank/backup/vms/" backup_date=`date "+%Y-%m-%d"` enc_password="supersecretstuff" default_state="started" state="${2:-$default_state}" # snapshot vm echo "" for vm in $vm; do ## prepare echo "################## PREPARE ####################" if [ $vm_pid != 0 ] then (vm stop $vm > /dev/null) >& /dev/null ### check when vm is shutdown properly echo "Waiting for $vm to shutdown properly" while [[ ${?} == 0 ]] do ps -p $vm_pid > /dev/null done fi echo "" echo "################## SNAPSHOT ###################" echo "Making a snapshot of $vm" sleep 1 ## create snapshot zfs snapshot -r $vm_dataset/$vm@$backup_date ## start VM if required if [ $state = "started" ] then echo "Starting $vm" (vm start $vm > /dev/null) >& /dev/null fi done # backup vm snapshot echo "" echo "################## BACKUP VM ##################" for vm in $vm; do echo "Send snapshot to local backup destination" sleep 1 ## send snapshot to backup destination zfs send -Rc $vm_dataset/$vm@$backup_date | \ pigz -c -p 4 | \ openssl enc \ -aes-256-ctr \ -a \ -pbkdf2 \ -iter 1000 \ -salt \ -k $enc_password \ > $backup_location/$vm-$backup_date.pigz.enc ## delete snapshot sleep 1 zfs destroy -r $vm_dataset/$vm@$backup_date done # report done echo "" echo "############### ALL DONE THANKS ###############" echo ""