diff --git a/btrbk b/btrbk index c6affe9..77c9f62 100755 --- a/btrbk +++ b/btrbk @@ -18,9 +18,9 @@ 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[0]}/"$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[0]}/"$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 ${!bkSuffix[@]}; do mapfile -t oldBk < <(rclone lsf $RCLONEDIR | grep ....-..-${bkSuffix[$o]} | head -n -${keepTime[$o]}) @@ -59,52 +59,79 @@ function runRestore { } function runFullRestore { - read -r -p "Do You want to run rclone config?[y/N]: " rcloneConf + [[ "${restoreDir}" != */ ]] && restoreDir="${restoreDir}/" + if ! btrfs subvolume list "$restoreDir" > /dev/null 2>&1; then + echo "destination is not a valid btrfs" + exit 1 + fi + read -e -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 - read -r -p "Import GPG key?[y/N]: " gpgtest - case "$gpgtest" in - [yY]) - echo "Enter Path of your gpg public and secret key" - read -r -p "Publickey: " gpgPub - read -r -p "Secretkey: " gpgPriv - gpg --import $gpgPub - gpg --pinentry-mode loopback --import $gpgPriv;; - *) ;; - esac + echo "Specify the remote path [remote:/path/to/backupdir]" + while true; do + read -e -r -p "Dir: " rcloneDir + [[ "${rcloneDir}" != */ ]] && rcloneDir="${rcloneDir}/" + if ! rclone lsf "$rcloneDir" > /dev/null 2>&1; then + echo "Not a valid directory" + continue + else + break + fi + done if ! [ -e "/etc/btrbk.conf" ]; then - rclone copy $rcloneDir"config/btrbk.conf" "/etc/btrbk.conf" + rclone copy $rcloneDir"config/btrbk.conf" "/etc/" fi source /etc/btrbk.conf + read -e -r -p "Import GPG key?[y/N]: " gpgtest + case "$gpgtest" in + [yY]) + echo "Enter Path of your gpg secret key" + while true; do + read -e -r -p "Secretkey: " gpgPriv + [ ! -f "$gpgPriv" ] && echo "Not a File" && continue + while true; do + read -e -r -p "Passphrase: " gpgPass + gpg --pinentry-mode=loopback --passphrase "$gpgPass" --import $gpgPriv > /dev/null 2>&1 && break + echo "Wrong Passphrase or file is not a valid secret key" + done + break + done + echo "${PUBKEY}:6:" | gpg --import-ownertrust;; + *) + echo "Enter gpg Passphrase" + while true; do + read -e -r -p "Passphrase: " gpgPass + echo "test" | gpg --pinentry-mode=loopback --passphrase "$gpgPass" -as > /dev/null 2>&1 && echo "Passphrase correct" && break + echo "Wrong Passphrase" + done;; + esac local bkSuffix=("${INCR[0]}" "${FULL[0]}") btrfs subvolume create $restoreDir"restore" lastBkDate=$(rclone lsf "$rcloneDir" | grep ....-..-${FULL[0]} | tail -n -1 | sed "s/full\/$//") for i in ${!bkSuffix[@]}; do mapfile -t bkFile < <(rclone lsf "$rcloneDir$lastBkDate${bkSuffix[$i]}" 2>/dev/null) for o in ${!bkFile[@]}; do - rclone rcat "$rcloneDir$lastBkDate${bkSuffix[$i]}/${bkFile[$o]}" | gpg --batch -d $PUBKEY -o - | btrfs receive $restoreDir"restore" + rclone cat "$rcloneDir$lastBkDate${bkSuffix[$i]}/${bkFile[$o]}" | gpg --batch --pinentry-mode=loopback --passphrase "$gpgPass" -d | btrfs receive $restoreDir"restore" done done for i in ${!VOLNAME[@]}; do newestSnap=$(ls -a $restoreDir"restore" | grep ${VOLNAME[$i]}.\....-..- | tail -n -1 ) btrfs subvolume snapshot $restoreDir"restore/"$newestSnap $restoreDir${VOLNAME[$i]} done + end } function getSnap { for i in $1; do snapLast=$SNAPDIR${VOLNAME[$i]}.$(date -d "$date -1 day" +%Y-%m-%d) - if ! [[ -d "$snapLast" ]]; then + if ! [ -d "$snapLast" ]; then snapLast=$SNAPDIR$(ls -a $SNAPDIR | grep ${VOLNAME[$i]}.$(date -d "$date" +%Y-%m)- | tail -n 1) fi done } - function testConf { source /etc/btrbk.conf if [ -z ${rDate+x} ]; then @@ -139,7 +166,8 @@ function testDate { } function end { - echo "backup finished in $(( ($(date +%s) - startDate) / 60 )) min" + echo "finished in $(( ($(date +%s) - startDate) / 60 )) min" + exit 1 } function echoHelp { @@ -158,7 +186,6 @@ function echoHelp { options=$(getopt -l "backup,force,date::,restore::,full-restore::,help" -o "bfd::r::x::h" -- "$@") eval set -- "$options" - while true; do case $1 in -b|--backup) backup=true; shift;; @@ -172,6 +199,7 @@ while true; do esac shift done + if [ "$fullRestore" = true ]; then runFullRestore elif [ "$backup" = true ]; then