added checks for userinput

This commit is contained in:
snaki 2022-07-08 11:12:12 +02:00
parent 928c310fcb
commit aa9a87adf9

70
btrbk
View file

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