[VM-Backup] Added bash version and updated csh version
This commit is contained in:
parent
04534bd3ed
commit
52b7ac88a5
69
FreeBSD/vm-backup-single.bash
Normal file
69
FreeBSD/vm-backup-single.bash
Normal 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 ""
|
@ -1,45 +1,69 @@
|
||||
#!/bin/csh
|
||||
|
||||
## config section
|
||||
set vm = $1
|
||||
set vm_dataset = "zroot/vms"
|
||||
set backup_dataset = "tank/backup/bhyve"
|
||||
set backup_date = `date "+%Y-%m-%d_%H:%M"`
|
||||
set vm_pid = `/usr/bin/pgrep -f "bhyve: $vm"`
|
||||
set backup_location = "/tank/backup/vms/"
|
||||
set backup_date = `date "+%Y-%m-%d"`
|
||||
|
||||
# Snapshot all VMs
|
||||
# snapshot vm
|
||||
echo ""
|
||||
echo "################# SNAPSHOT VM #################"
|
||||
foreach vm ($vm)
|
||||
|
||||
## stopping vm
|
||||
vm stop $vm
|
||||
## prepare
|
||||
echo "################## PREPARE ####################"
|
||||
|
||||
## give vm time to shut down properly
|
||||
sleep 15
|
||||
vm list | grep $vm ## DELETE ME
|
||||
|
||||
## snapshot vm
|
||||
echo "Snapshot VM: $vm"
|
||||
zfs snapshot $vm_dataset/$vm@$backup_date
|
||||
echo ""
|
||||
if ( $vm_pid != 0 ) then
|
||||
(vm stop $vm > /dev/null) >& /dev/null
|
||||
|
||||
## start VM
|
||||
vm start $vm
|
||||
echo ""
|
||||
### check when vm is shutdown properly
|
||||
echo "Waiting for $vm to shutdown properly"
|
||||
|
||||
# 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 "################## BACKUP VM ##################"
|
||||
foreach vm ($vm)
|
||||
|
||||
## Create ZFS dataset
|
||||
echo "Creating destination dataset for $vm"
|
||||
zfs create $backup_dataset/$vm-$backup_date
|
||||
echo "Send snapshot to local backup destination"
|
||||
sleep 1
|
||||
|
||||
## Send snapshot to backup destination
|
||||
echo "Backup VM: $vm"
|
||||
zfs send $vm_dataset/$vm@$backup_date | zfs recv -F $backup_dataset/$vm-$backup_date
|
||||
echo ""
|
||||
## 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
|
||||
zfs destroy $vm_dataset/$vm@$backup_date
|
||||
## delete snapshot
|
||||
sleep 1
|
||||
zfs destroy $vm_dataset/$vm@$backup_date
|
||||
|
||||
end
|
||||
|
||||
# report done
|
||||
echo ""
|
||||
|
Loading…
Reference in New Issue
Block a user