Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backup of KVM Virtual Machines on LVM with --read-special #1832

Closed
unlandm opened this issue Nov 11, 2016 · 4 comments
Closed

Backup of KVM Virtual Machines on LVM with --read-special #1832

unlandm opened this issue Nov 11, 2016 · 4 comments
Labels

Comments

@unlandm
Copy link

unlandm commented Nov 11, 2016

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
@unlandm unlandm changed the title Backup ov KVM Virtual Machines on LVM with --read-special Backup of KVM Virtual Machines on LVM with --read-special Nov 11, 2016
@enkore
Copy link
Contributor

enkore commented Nov 12, 2016

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

@milkey-mouse
Copy link
Contributor

milkey-mouse commented Mar 10, 2017

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

@ThomasWaldmann
Copy link
Member

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.

@milkey-mouse
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants