scripts/FreeBSD/vm/unused_vm-backup-single.csh

72 lines
1.5 KiB
Tcsh
Raw Normal View History

2019-06-19 09:18:15 +02:00
#!/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"`
2019-06-19 09:18:15 +02:00
# snapshot vm
2019-06-19 09:18:15 +02:00
echo ""
foreach vm ($vm)
2019-06-19 09:18:15 +02:00
## prepare
echo "################## PREPARE ####################"
2019-06-19 09:18:15 +02:00
vm list | grep $vm ## DELETE ME
2019-06-19 09:18:15 +02:00
if ( $vm_pid != 0 ) then
(vm stop $vm > /dev/null) >& /dev/null
2019-06-19 09:18:15 +02: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
## 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 ##################"
foreach vm ($vm)
2019-06-19 09:18:15 +02:00
echo "Send snapshot to local backup destination"
sleep 1
2019-06-19 09:18:15 +02: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
end
2019-06-19 09:18:15 +02:00
# report done
echo ""
echo "############### ALL DONE THANKS ###############"
echo ""