scripts/FreeBSD/vm-backup-single.bash

70 lines
1.3 KiB
Bash
Raw Normal View History

#!/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"`
# 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
vm list | grep $vm ## DELETE ME
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 | \
gzip | \
openssl enc -aes-256-cbc -a -pbkdf2 -iter 1000 -salt -k development \
> $backup_location/$vm-$backup_date.gz.enc
## delete snapshot
sleep 1
zfs destroy $vm_dataset/$vm@$backup_date
done
# report done
echo ""
echo "############### ALL DONE THANKS ###############"
echo ""