scripts/borg_partclone_backup_multipartition.sh

66 lines
1.4 KiB
Bash
Executable File

#!/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