diff --git a/borg_partclone_backup.sh b/borg_partclone_backup.sh index 721ebdc..410a36d 100755 --- a/borg_partclone_backup.sh +++ b/borg_partclone_backup.sh @@ -7,16 +7,43 @@ 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" || exit 1 +test `id -u` == "0" || exitmsg "Run this script as root" # Create the snapshot -lvcreate -y -n $SNP_NM -s -L $SNP_SZ $VGNAME/$LVNAME +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 | \ +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 -s $SNP_NM - + borg2 -r ssh://$SRV_HOST$SRV_BORG_REPO create --stdin-name image.pcl $SNP_NM - || exitmsg_rm "Backup failed for $SNP_NM" -# Remove the snapshot -lvremove -y $VGNAME/$SNP_NM +exit_ok diff --git a/borg_partclone_backup_multipartition.sh b/borg_partclone_backup_multipartition.sh new file mode 100755 index 0000000..74be46f --- /dev/null +++ b/borg_partclone_backup_multipartition.sh @@ -0,0 +1,65 @@ +#!/usr/bin/bash +# Usage: borg_partclone_backup.sh VGNAME LVNAME SNP_SZ PART_NO SRV_HOST SRV_BORG_REPO +VGNAME=$1 +LVNAME=$2 +DATE=`date +%Y%m%dT%H%M%S` +SNP_NM=$LVNAME-$DATE +SNP_SZ=$3 +PART_NO=$4 +SRV_HOST=$5 +SRV_BORG_REPO=$6 +SRV_BORG_BACKUP_NAME=$LVNAME-$7-$DATE + +exitmsg () { + echo $1 + exit 1; +} + +rmsnap () { + # Remove the snapshot + lvremove -y $VGNAME/$SNP_NM >/dev/null +} + +rmloop () { + # Free the loop device + losetup -D /dev/loop0 >/dev/null + +} + +exitmsg_rm () { + rmsnap + exitmsg $1 +} + +exitmsg_rm_loop () { + rmloop + exitmsg_rm $1 +} + +exit_ok () { + rmloop + rmsnap + echo "Successful backup for $VGNAME/$LVNAME part. $PART_NO" +} + + +echo "Starting backup for $VGNAME/$LVNAME part. $PART_NO" + +# 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" + +# Setup loopback device +losetup -P /dev/loop0 /dev/$VGNAME/$SNP_NM || exitmsg_rm "/dev/loop0 already used" + +# Check filesystem +fsck -y /dev/loop0p$PART_NO || exitmsg_rm_loop "Failed to check filesystem /dev/loop0p$PART_NO" + +# Clone +partclone.ext4 -s /dev/loop0p$PART_NO -c -q 2>&1 | \ + BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=yes \ + borg2 -r ssh://$SRV_HOST$SRV_BORG_REPO create --stdin-name image.pcl $SRV_BORG_BACKUP_NAME - || exitmsg_rm_loop "Backup failed for $VGNAME/$SNP_NM part. $PART_NO" + +exit_ok