I have a KVM Virtual Machine on LVM with two drives on separate logical volumes (LVs).
When I start my backup with borg I have first to create snapshots.
How do I handle that in a bash script?
First create snapshot of drive one. Than backup with borg. Delete snapshot drive one.
Second create snapshot of drive two. Than backup with borg. Delete snapshot drive two.
End of script.
In the following example the snapshots are all generated bevor backup with borg and deleted after.
What is the best (or only way) way to handle it?
Thank You
Michael
#!/bin/bash
echo "Backup `hostname`"
REPOSITORY=/media/vmbackup/server1/
LVPATH=/dev/vg_vmimages/
LVNAME=`lvs --noheadings -o lv_name | tr -d ' '`
#remove old snapshots
for VM in $LVNAME;
do
lvremove -f /dev/vg_vmimages/$VM-snapshot
echo "old snapshots deleted"
done
#create snapshots
for VM in $LVNAME;
do
lvcreate --size 5120M --snapshot --name "$VM-snapshot" $LVPATH$VM
echo "Snapshots created"
done
#create lvdisplay.txt
lvdisplay > lvdisplay.txt
echo "lvdisplay created"
#create borg backup
borg create --progress --info --list --stats --compress lz4 --read-special \
$REPOSITORY::`hostname`-`date +%Y-%m-%d` lvdisplay.txt $LVPATH*-snapshot
#remove lvdisplay.txt
rm -f lvdisplay.txt
echo "lvdisplay.txt deleted"
#remove snapshots
for VM in $LVNAME;
do
lvremove -f /dev/vg_vmimages/$VM-snapshot
echo "Snapshots deleted"
done
#delete all files which do not meet "keep-conditions"
borg prune --info --list $REPOSITORY --prefix `hostname`- \
--keep-daily=7 --keep-weekly=4 --keep-monthly=3
Well you could do something along the lines of (untested)
echo "Backup `hostname`"
REPOSITORY=/media/vmbackup/server1/
LVPATH=/dev/vg_vmimages/
LVNAME=`lvs --noheadings -o lv_name | tr -d ' '`
backup_LV() {
echo "Backing up LV $1"
echo " => Creating snapshot $1-snapshot"
lvcreate --size 5120M --snapshot --name "$1-snapshot" $LVPATH$1
echo " => Creating archive"
SNAPSHOT=$LVPATH$1-snapshot
borg create -svp --list -C lz4 --read-special \
$REPOSITORY::`hostname`-`date +%Y-%m-%d` lvdisplay.txt $SNAPSHOT
# ^- can also use https://borgbackup.rtfd.io/en/latest/usage.html#borg-help-placeholders
echo " => Deleting snapshot"
lvremove -f $SNAPSHOT
echo "Done with LV $1"
}
for VM in $LVNAME;
do
backup_LV $VM
done
#delete all files which do not meet "keep-conditions"
borg prune --info --list $REPOSITORY --prefix `hostname`- \
--keep-daily=7 --keep-weekly=4 --keep-monthly=3
FWIW here's how I backup my KVM libvirt-managed VMs while they're running, no snapshots needed; I just use QEMU's copy-on-write image types to hotswap the image it's running off of:
https://gist.github.com/milkey-mouse/b8cb865910b4d574d9f63ffcd8db09f5
Maybe everybody could check if above adds something to the already existing stuff there:
https://github.com/borgbackup/community/
If so, add a link there, so we can close this issue.
I am working on a more robust version of that script in Python (using the libvirt & borg APIs directly). When I finish it I will submit a PR to the community repo.
Most helpful comment
Well you could do something along the lines of (untested)