scripts/FreeBSD/vm/vm-backup.csh

58 lines
1.2 KiB
Tcsh
Raw Normal View History

2019-04-07 18:48:30 +02:00
#!/bin/csh
## config section
set vm_dataset = "zroot/vms"
set backup_dataset = "tank/backup/bhyve"
set backup_date = `date "+%Y-%m-%d"`
# Snapshot all VMs
echo ""
echo "################# SNAPSHOT VM #################"
foreach vm (`ls /usr/vms | \
grep -v .config | \
grep -v .iso | \
grep -v .templates`)
## stopping vm
vm stop $vm
## give vm time to shut down properly
2019-06-19 09:17:40 +02:00
sleep 15
2019-04-07 18:48:30 +02:00
## snapshot vm
2019-06-19 09:17:40 +02:00
echo "Snapshot VM: $vm"
2019-04-07 18:48:30 +02:00
zfs snapshot $vm_dataset/$vm@$backup_date
echo ""
## start VM
vm start $vm
echo ""
end
# Backup all VM snapshots
echo ""
echo "################## BACKUP VM ##################"
foreach vm (`ls /usr/vms | \
grep -v .config | \
grep -v .iso | \
grep -v .templates`)
2019-06-19 09:17:40 +02:00
## Create ZFS dataset
echo "Creating destination dataset for $vm"
zfs create $backup_dataset/$vm-$backup_date
2019-04-07 18:48:30 +02:00
## Send snapshot to backup destination
2019-06-19 09:17:40 +02:00
echo "Backup VM: $vm"
zfs send $vm_dataset/$vm@$backup_date | zfs recv -F $backup_dataset/$vm-$backup_date
2019-04-07 18:48:30 +02:00
echo ""
2019-06-19 09:17:40 +02:00
## Delete snapshot
2019-04-07 18:48:30 +02:00
zfs destroy $vm_dataset/$vm@$backup_date
end
# report done
echo ""
echo "############### ALL DONE THANKS ###############"
2019-06-19 09:17:40 +02:00
echo ""