Last active
October 30, 2017 15:03
-
-
Save SharkyRawr/d5b7e2e3b9effb0155acf006d16bfefb to your computer and use it in GitHub Desktop.
LXC LVM volume backup script using duplicity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /bin/bash -e | |
| # | |
| ## LVM backup script by Hendrik 'Sharky' Schumann | |
| # | |
| GROUP="vg0" | |
| VOLUMES="lxc-xana lxc-githost" | |
| BACKDIR="/mnt/autoback" | |
| # Duplicity settings | |
| DUPLICITY="/usr/local/bin/duplicity" | |
| DEST="b2://...changeme..." | |
| DUPLICITY_OPTIONS="-v5 --full-if-older-than 1M --asynchronous-upload" | |
| DUPLICITY_EXCLUDE="--exclude /dev/ --exclude /sys/ --exclude /var/lib/lxcfs --exclude /proc/ --exclude /run/ --exclude /tmp/" | |
| # Extra excludes example | |
| #DUPLICITY_EXCLUDE="$DUPLICITY_EXCLUDE --exclude $BACKDIR/lxc-somecontainer-autoback/home/someuser" | |
| # Duplicity key | |
| DUPLICITY_KEY="BADBABE" | |
| . /root/.duplicity | |
| export PASSPHRASE | |
| function is_autoback { | |
| if [[ "$V1" == *"-autoback" ]]; then | |
| return 1 | |
| fi | |
| return 0 | |
| } | |
| function create_snapshot { | |
| V="$1" | |
| lvcreate -L512M -s -n "$V-autoback" /dev/$GROUP/$V | |
| return $? | |
| } | |
| function mount_snapshot { | |
| V="$1" | |
| SNAPNAME="$V-autoback" | |
| mkdir -p $BACKDIR/$SNAPNAME || true | |
| mount /dev/$GROUP/$SNAPNAME $BACKDIR/$SNAPNAME -o ro | |
| return $? | |
| } | |
| function unmount_and_destroy_snapshot { | |
| V="$1" | |
| SNAPNAME="$V-autoback" | |
| FORCE="$2" | |
| if [[ "x$FORCE" != "x" ]]; then | |
| umount $BACKDIR/$SNAPNAME || true | |
| lvremove -f /dev/$GROUP/$SNAPNAME || true | |
| else | |
| umount $BACKDIR/$SNAPNAME | |
| lvremove -f /dev/$GROUP/$SNAPNAME | |
| fi | |
| return $? | |
| } | |
| trap ctrl_c INT | |
| function ctrl_c() { | |
| echo " *** Trapped CTRL-C" | |
| for V in $VOLUMES; do | |
| unmount_and_destroy_snapshot $V force | |
| done | |
| exit 1 | |
| } | |
| for V in $VOLUMES; do | |
| echo " - $V" | |
| create_snapshot $V | |
| SNAPNAME="$V-autoback" | |
| mount_snapshot $V | |
| #du -hs $BACKDIR/$SNAPNAME | |
| done | |
| echo " * Invoking duplicity" | |
| ## | |
| # everything mounted, invoke duplicity | |
| ## | |
| "$DUPLICITY" $DUPLICITY_OPTIONS $DUPLICITY_EXCLUDE --encrypt-sign-key $DUPLICITY_KEY / "$DEST" | |
| "$DUPLICITY" -vn remove-all-but-n-full 3 --force "$DEST" | |
| echo " * Cleaning up autoback snapshots" | |
| for V in $VOLUMES; do | |
| unmount_and_destroy_snapshot $V | |
| done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment