From 805449870e0b11fa151e7faf00e4d1e06d23cc2a Mon Sep 17 00:00:00 2001 From: snaki Date: Wed, 6 Jul 2022 16:32:40 +0200 Subject: [PATCH] added restore --- btrbk | 109 +++++++++++++++++++++++++++++++++++++---------------- btrbk.conf | 6 +++ 2 files changed, 83 insertions(+), 32 deletions(-) diff --git a/btrbk b/btrbk index cbd2858..c1a02a2 100755 --- a/btrbk +++ b/btrbk @@ -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 diff --git a/btrbk.conf b/btrbk.conf index 6800866..55e3233 100644 --- a/btrbk.conf +++ b/btrbk.conf @@ -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