scripts/FreeBSD/vm/vm-backup-single.bash

68 lines
1.4 KiB
Bash

#!/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="nDNmPPBPk7jQnwke"
# 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 $vm_dataset/$vm@$backup_date
## start VM
echo "Starting $vm"
(vm start $vm > /dev/null) >& /dev/null
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 $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 $vm_dataset/$vm@$backup_date
done
# report done
echo ""
echo "############### ALL DONE THANKS ###############"
echo ""