2019-06-19 09:18:15 +02:00
|
|
|
#!/bin/csh
|
|
|
|
## config section
|
2020-01-03 18:11:47 +01:00
|
|
|
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"`
|
2019-06-19 09:18:15 +02:00
|
|
|
|
2020-01-03 18:11:47 +01:00
|
|
|
# snapshot vm
|
2019-06-19 09:18:15 +02:00
|
|
|
echo ""
|
2020-01-03 18:11:47 +01:00
|
|
|
foreach vm ($vm)
|
2019-06-19 09:18:15 +02:00
|
|
|
|
2020-01-03 18:11:47 +01:00
|
|
|
## prepare
|
|
|
|
echo "################## PREPARE ####################"
|
2019-06-19 09:18:15 +02:00
|
|
|
|
2020-01-03 18:11:47 +01:00
|
|
|
vm list | grep $vm ## DELETE ME
|
2019-06-19 09:18:15 +02:00
|
|
|
|
2020-01-03 18:11:47 +01:00
|
|
|
if ( $vm_pid != 0 ) then
|
|
|
|
(vm stop $vm > /dev/null) >& /dev/null
|
2019-06-19 09:18:15 +02:00
|
|
|
|
2020-01-03 18:11:47 +01:00
|
|
|
### check when vm is shutdown properly
|
|
|
|
echo "Waiting for $vm to shutdown properly"
|
|
|
|
|
|
|
|
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
|
2019-06-19 09:18:15 +02:00
|
|
|
|
2020-01-03 18:11:47 +01:00
|
|
|
## 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
|
2019-06-19 09:18:15 +02:00
|
|
|
echo ""
|
|
|
|
echo "################## BACKUP VM ##################"
|
2020-01-03 18:11:47 +01:00
|
|
|
foreach vm ($vm)
|
2019-06-19 09:18:15 +02:00
|
|
|
|
2020-01-03 18:11:47 +01:00
|
|
|
echo "Send snapshot to local backup destination"
|
|
|
|
sleep 1
|
2019-06-19 09:18:15 +02:00
|
|
|
|
2020-01-03 18:11:47 +01:00
|
|
|
## 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
|
2019-06-19 09:18:15 +02:00
|
|
|
|
2020-01-03 18:11:47 +01:00
|
|
|
end
|
2019-06-19 09:18:15 +02:00
|
|
|
|
|
|
|
# report done
|
|
|
|
echo ""
|
|
|
|
echo "############### ALL DONE THANKS ###############"
|
2020-01-03 18:11:47 +01:00
|
|
|
echo ""
|