From 52b7ac88a55e0b81d6078d848af7480764e724b2 Mon Sep 17 00:00:00 2001 From: Simon Cornet Date: Fri, 3 Jan 2020 18:11:47 +0100 Subject: [PATCH] [VM-Backup] Added bash version and updated csh version --- FreeBSD/vm-backup-single.bash | 69 +++++++++++++++++++++++++++++ FreeBSD/vm-backup-single.csh | 82 ++++++++++++++++++++++------------- 2 files changed, 122 insertions(+), 29 deletions(-) create mode 100644 FreeBSD/vm-backup-single.bash diff --git a/FreeBSD/vm-backup-single.bash b/FreeBSD/vm-backup-single.bash new file mode 100644 index 0000000..f8e3992 --- /dev/null +++ b/FreeBSD/vm-backup-single.bash @@ -0,0 +1,69 @@ +#!/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 "" diff --git a/FreeBSD/vm-backup-single.csh b/FreeBSD/vm-backup-single.csh index 3838074..ccecec9 100644 --- a/FreeBSD/vm-backup-single.csh +++ b/FreeBSD/vm-backup-single.csh @@ -1,47 +1,71 @@ #!/bin/csh - ## config section -set vm = $1 -set vm_dataset = "zroot/vms" -set backup_dataset = "tank/backup/bhyve" -set backup_date = `date "+%Y-%m-%d_%H:%M"` +set vm = $1 +set vm_dataset = "zroot/vms" +set vm_pid = `/usr/bin/pgrep -f "bhyve: $vm"` +set backup_location = "/tank/backup/vms/" +set backup_date = `date "+%Y-%m-%d"` -# Snapshot all VMs +# snapshot vm echo "" -echo "################# SNAPSHOT VM #################" +foreach vm ($vm) -## stopping vm -vm stop $vm + ## prepare + echo "################## PREPARE ####################" -## give vm time to shut down properly -sleep 15 + vm list | grep $vm ## DELETE ME -## snapshot vm -echo "Snapshot VM: $vm" -zfs snapshot $vm_dataset/$vm@$backup_date -echo "" + if ( $vm_pid != 0 ) then + (vm stop $vm > /dev/null) >& /dev/null -## start VM -vm start $vm -echo "" + ### check when vm is shutdown properly + echo "Waiting for $vm to shutdown properly" -# Backup all VM snapshots + while ({ /usr/bin/pgrep $vm_pid }) + echo "Waiting for pid $vm_pid" + sleep 2 + end + + vm list | grep $vm ## DELETE ME + + endif + + 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" + sleep 1 + (vm start $vm > /dev/null) >& /dev/null + +end + +# backup vm snapshot echo "" echo "################## BACKUP VM ##################" +foreach vm ($vm) -## Create ZFS dataset -echo "Creating destination dataset for $vm" -zfs create $backup_dataset/$vm-$backup_date + echo "Send snapshot to local backup destination" + sleep 1 -## Send snapshot to backup destination -echo "Backup VM: $vm" -zfs send $vm_dataset/$vm@$backup_date | zfs recv -F $backup_dataset/$vm-$backup_date -echo "" + ## 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 -zfs destroy $vm_dataset/$vm@$backup_date + ## delete snapshot + sleep 1 + zfs destroy $vm_dataset/$vm@$backup_date + +end # report done echo "" echo "############### ALL DONE THANKS ###############" -echo "" \ No newline at end of file +echo ""