72 lines
1.5 KiB
Tcsh
72 lines
1.5 KiB
Tcsh
#!/bin/csh
|
|
## config section
|
|
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 vm
|
|
echo ""
|
|
foreach vm ($vm)
|
|
|
|
## prepare
|
|
echo "################## PREPARE ####################"
|
|
|
|
vm list | grep $vm ## DELETE ME
|
|
|
|
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 ({ /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)
|
|
|
|
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
|
|
|
|
end
|
|
|
|
# report done
|
|
echo ""
|
|
echo "############### ALL DONE THANKS ###############"
|
|
echo ""
|