[VM-Backup] Added bash version and updated csh version

This commit is contained in:
Simon Cornet 2020-01-03 18:11:47 +01:00
parent 04534bd3ed
commit 52b7ac88a5
2 changed files with 122 additions and 29 deletions

View File

@ -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 ""

View File

@ -1,45 +1,69 @@
#!/bin/csh #!/bin/csh
## config section ## config section
set vm = $1 set vm = $1
set vm_dataset = "zroot/vms" set vm_dataset = "zroot/vms"
set backup_dataset = "tank/backup/bhyve" set vm_pid = `/usr/bin/pgrep -f "bhyve: $vm"`
set backup_date = `date "+%Y-%m-%d_%H:%M"` set backup_location = "/tank/backup/vms/"
set backup_date = `date "+%Y-%m-%d"`
# Snapshot all VMs # snapshot vm
echo "" echo ""
echo "################# SNAPSHOT VM #################" foreach vm ($vm)
## stopping vm ## prepare
vm stop $vm echo "################## PREPARE ####################"
## give vm time to shut down properly vm list | grep $vm ## DELETE ME
sleep 15
## snapshot vm if ( $vm_pid != 0 ) then
echo "Snapshot VM: $vm" (vm stop $vm > /dev/null) >& /dev/null
zfs snapshot $vm_dataset/$vm@$backup_date
echo ""
## start VM ### check when vm is shutdown properly
vm start $vm echo "Waiting for $vm to shutdown properly"
echo ""
# 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 ""
echo "################## BACKUP VM ##################" echo "################## BACKUP VM ##################"
foreach vm ($vm)
## Create ZFS dataset echo "Send snapshot to local backup destination"
echo "Creating destination dataset for $vm" sleep 1
zfs create $backup_dataset/$vm-$backup_date
## Send snapshot to backup destination ## send snapshot to backup destination
echo "Backup VM: $vm" zfs send $vm_dataset/$vm@$backup_date | \
zfs send $vm_dataset/$vm@$backup_date | zfs recv -F $backup_dataset/$vm-$backup_date gzip | \
echo "" openssl enc -aes-256-cbc -a -pbkdf2 -iter 1000 -salt -k development \
> $backup_location/$vm-$backup_date.gz.enc
## Delete snapshot ## delete snapshot
zfs destroy $vm_dataset/$vm@$backup_date sleep 1
zfs destroy $vm_dataset/$vm@$backup_date
end
# report done # report done
echo "" echo ""