#!/usr/bin/bash # Usage: borg_partclone_backup.sh VGNAME LVNAME SNP_SZ SRV_HOST SRV_BORG_REPO VGNAME=$1 LVNAME=$2 SNP_NM=$LVNAME-`date +%Y%m%dT%H%M%S` SNP_SZ=$3 SRV_HOST=$4 SRV_BORG_REPO=$5 exitmsg () { echo $1 >&2; exit 1; } rmsnap () { # Remove the snapshot lvremove -y $VGNAME/$SNP_NM >/dev/null } exitmsg_rm () { rmsnap exitmsg $1 } exit_ok () { rmsnap echo "Successful backup for $VGNAME/$LVNAME" exit 0 } echo "Starting backup for $VGNAME/$LVNAME" # Be root test `id -u` == "0" || exitmsg "Run this script as root" # Create the snapshot lvcreate -y -n $SNP_NM -s -L $SNP_SZ $VGNAME/$LVNAME >/dev/null || exitmsg "Cannot create snapshot" # Check filesystem fsck -y /dev/$VGNAME/$SNP_NM || exitmsg_rm "Failed to check filesystem /dev/$VGNAME/$SNP_NM" # Clone partclone.ext4 -s /dev/$VGNAME/$SNP_NM -c -q >/dev/null 2>&1 | \ BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=yes \ borg2 -r ssh://$SRV_HOST$SRV_BORG_REPO create --stdin-name image.pcl $SNP_NM - || exitmsg_rm "Backup failed for $SNP_NM" exit_ok