added restore

This commit is contained in:
snaki 2022-07-06 16:32:40 +02:00
parent 6512f81d54
commit 805449870e
2 changed files with 83 additions and 32 deletions

109
btrbk
View file

@ -5,7 +5,7 @@ startDate=$(date +%s)
function bkRun {
local date=$(date +%Y-%m-%d)
local month=$(date +%Y-%B)
local month=$(date +%Y-%m)
local bkSuffix=("${INCR[0]}" "${FULL[0]}")
local keepTime=("${INCR[1]}" "${FULL[1]}")
for i in ${!SUBVOLS[@]}; do
@ -19,11 +19,11 @@ function bkRun {
sync
fi
if [[ -d "$snapLast" && $1 = "incremental" ]]; then
btrfs send -p $snapLast $SNAPDIR$snapName | gpg --batch -er $PUBKEY -o - | rclone rcat $RCLONEDIR$month"-incr/"$snapName.pgp
btrfs send -p $snapLast $SNAPDIR$snapName | gpg --batch -er $PUBKEY -o - | rclone rcat $RCLONEDIR$month"-${INCR[0]}/"$snapName.pgp
else
btrfs send $SNAPDIR$snapName | gpg --batch -er $PUBKEY -o - | rclone rcat $RCLONEDIR$month"-full/"$snapName.pgp
btrfs send $SNAPDIR$snapName | gpg --batch -er $PUBKEY -o - | rclone rcat $RCLONEDIR$month"-${FULL[0]}/"$snapName.pgp
if [[ $1 = "full" ]]; then
for o in ${!bkName[@]}; do
for o in ${!bkSuffix[@]}; do
mapfile -t oldBk < <(rclone lsf $RCLONEDIR | grep ....-..-${bkSuffix[$o]} | head -n -${keepTime[$o]})
for n in ${!oldBk[@]}; do
rclone delete $RCLONEDIR${oldBk[$n]}
@ -31,29 +31,65 @@ function bkRun {
done
fi
fi
mapfile -t oldSnaps < <(ls -a $SNAPDIR | grep ${VOLNAME[$i]}.\....-..- | head -n -3)
mapfile -t oldSnaps < <(ls -a $SNAPDIR | grep ${VOLNAME[$i]}.\....-..- | head -n -$KEEPLOCAL)
for snap in ${!oldSnaps[@]}; do
btrfs subvolume delete $SNAPDIR${oldSnaps[$snap]}
done
done
rclone copy /etc/btrbk.conf $RCLONEDIR"config/"
end
}
function runRestore {
if [ -z ${rDate+x} ]; then
for o in ${!VOLNAME[@]}; do
if [[ "${VOLNAME[$o]}" = "${rest}" ]]; then
if [[ "${VOLNAME[$o]}" = "${restoreVol}" ]]; then
getSnap "$o"
break
elif [[ ! "${VOLNAME[@]}" =~ "${rest}" ]]; then
elif [[ ! "${VOLNAME[@]}" =~ "${restoreVol}" ]]; then
echo "Subvolume doesnt exist"
exit
exit 1
fi
done
mount /dev/mappaer/hideri /mnt/btrfsroot
btrfs
# mkdir /mnt/$BTRFSUUID
# mount -o subvolid=1 -U $BTRFSUUID /mnt/$BTRFSUUID
# btrfs subvolume delete /mnt/$BTRFSUUID/
fi
echo "not implemented yet™"
exit 1
}
function runFullRestore {
read -r -p "Do You want to run rclone config?[y/N]: " rcloneConf
case "$rcloneConf" in
[yY]) rclone config;;
*) ;;
esac
echo "specify the remote path with trailing slash [remote:/path/to/backupdir/]"
read -r -p "Dir: " rcloneDir
rclone copy $rcloneDir"config/btrbk.conf" /etc/btrbk.conf
echo "Supply Path of your gpg public and secret key"
read -r -p "Publickey: " gpgPub
read -r -p "Secretkey: " gpgPriv
gpg --import $gpgPub
gpg --import $gpgPriv
source /etc/btrbk.conf
local bkSuffix=("${INCR[0]}" "${FULL[0]}")
btrfs subvolume create $restoreDir"snapshots"
lastBk=$(rclone lsf $RCLONEDIR | grep ....-..-${FULL[0]} | tail -n -1 | sed "s/\/$//")
if rclone lsf $RCLONEDIR$($lastBk | sed "s/${FULL[0]}/${INCR[0]}") > /dev/null 2>&1; then
for i in ${!bkSuffix[@]}; do
mapfile -t bkFile < <(rclone lsf "$RCLONEDIR$lastBk${bkSuffix[$i]}/")
for o in ${!bkFile[@]}; do
rclone rcat "$RCLONEDIR$lastBk${bkSuffix[$i]}/${bkFile[$o]}" | gpg --batch -d $PUBKEY -o - | btrfs recive $restoreDir"snapshots"
done
done
for i in ${!SUBVOLS[@]}; do
rclone rcat /mnt/bk/root-bk.gpg | gpg --batch -d --passphrase-file /mnt/bk/gpgpass -o - | btrfs receive /mnt/restore
done
}
function getSnap {
@ -65,24 +101,31 @@ function getSnap {
done
}
function testconf {
if rclone ls $RCLONEDIR > /dev/null 2>&1; then
if gpg --list-key $PUBKEY > /dev/null 2>&1; then
if [ $fullRestore=1 ]; then
runFull
elif [ $restore=1 ]; then
runRestore
else
testDate
function testConf {
if [ -z ${rDate+x} ]; then
if ! date -d "$rDate" > /dev/null 2>&1; then
echo "date is in wrong format: YYYY-MM-DD"
exit 1
fi
else
echo "pubkey wrong or not found exit code: $?"
exit
fi
else
echo "rclone dir wrong or not found exit code: $?"
exit
fi
if rclone ls $RCLONEDIR > /dev/null 2>&1; then
if gpg --list-key $PUBKEY > /dev/null 2>&1; then
if [ $fullRestore=1 ]; then
runFullRestore
elif [ $restore=1 ]; then
runRestore
elif [ $backup=1 ]; then
testDate
fi
else
echo "pubkey wrong or not found exit code: $?"
exit 1
fi
else
echo "rclone dir wrong or not found exit code: $?"
exit 1
fi
}
function testDate {
@ -97,7 +140,7 @@ function end {
echo "backup finished in $(( ($(date +%s) - startDate) / 60 )) min"
}
function Help {
function echoHelp {
echo "Usage: btrbk [options]"
echo "A script to automatically or manually backup your btrfs subvolumes."
echo "options:"
@ -105,9 +148,10 @@ function Help {
echo " -f, --force force backup to override today's backup"
echo " -d, --date=YYYY-MM-DD define a date from which to restore when using -r"
echo " -r, --restore=VOLNAME restores the latest snapshot of a given subvolume"
echo " -x, --full-restore=PATH this walks you through an complete config restore
and will restore the whole system to a given directory"
echo " -x, --full-restore=PATH this walks you through an complete config restore"
echo " and will restore the whole system to a given directory"
echo " -h, --help display this help and exit"
exit 1
}
options=$(getopt -l "backup,force,date:,restore:,full-restore:,help" -o "bfd:r:x:h" -- "$@")
@ -120,9 +164,10 @@ while true; do
-r|--restore) restore=1; restoreVol=$2; shift;;
-x|--full-restore) fullRestore=1; restoreDir=$2; shift;;
-h|--help) Help; shift; break;;
--) shift; break;;
--) shift;;
\?) echo "error"; exit 1;;
*) echoHelp; shift; break;;
esac
shift
done
testConf

View file

@ -10,12 +10,18 @@ SUBVOLS=("/" "/home")
#VOLNAME has to be in the same order as SUBVOLS to be interpreted correctly
VOLNAME=("root" "home")
#UUID of the Disk with the btrfs root
BTRFSUUID="d1b20b8a-3cf4-41f7-bdc8-d50d53dc77c2"
#btrfs volume too keep snapshots in
SNAPDIR="/.snapshots/"
#Rclone Directory on remote storage
RCLONEDIR="test:/mnt/bk/new/"
#How many local snapshots should be kept
KEEPLOCAL="10"
#Suffix name for incremental/full backups and retention
#example: YYYY-mm-full and will keep the last two backups
#For full Backups dont set retention below 1 or you will always delete your current backup which renders all following incremental backups useless