From b16f0b0345a898657dcec36cba3ca12df82ba55d Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Tue, 6 Aug 2019 22:42:31 -0300 Subject: Include BORG_PASSPHRASE config for each destination in an array --- borger | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/borger b/borger index 3448101..a4d2f73 100755 --- a/borger +++ b/borger @@ -29,6 +29,7 @@ DESTINATION="$1" OPTION="$2" BASE_CONFIG="$HOME/.config/borger" CONFIG="$BASE_CONFIG/$DESTINATION" +INTERVAL="1h" # Print info function info { @@ -90,7 +91,6 @@ function borger_config { KEEPMONTHLY="6" ENCRYPTION="keyfile" PLACEHOLDER="{user}" - INTERVAL="1h" source $CONFIG @@ -205,8 +205,7 @@ function borger_set_lockfile { if ( set -o noclobber; echo "$$" > "$LOCKFILE" ) &> /dev/null; then trap 'borger_unset_lockfile' INT TERM EXIT else - echo "Could not create lockfile $LOCKFILE, exiting" - exit 1 + fatal "Could not create lockfile $LOCKFILE, exiting" fi fi } @@ -226,8 +225,7 @@ function borger_check_lockfile { pid="`cat $LOCKFILE`" process="`ps --no-headers -o comm $pid`" if [ "$?" == "0" ] && [ "`ps --no-headers -o comm $$`" == "$process" ]; then - echo "Another program is running for $LOCKFILE, skipping run" - exit + fatal "Another program is running for $LOCKFILE, skipping run" else echo "Found old lockfile $LOCKFILE, removing it" borger_unset_lockfile @@ -265,6 +263,7 @@ function borger_single { export BORG_PASSCOMMAND="" fi + # Run as a subprocess so we do not exit on any fatal error $FULLNAME $DESTINATION } @@ -272,10 +271,32 @@ function borger_single { function borger_multiple { info "Multiple destination \"$DESTINATION\" found. Processing each subconfig..." + # Needs bash 4 + # https://stackoverflow.com/questions/1494178/how-to-define-hash-tables-in-bash + if echo $BASH_VERSION | grep -q "^3"; then + fatal "$BASENAME requires bash 4 or newer." + fi + + # Passphrase array + declare -A BORG_PASSPHRASES + export BORG_PASSCOMMAND="" + + # Evaluate each config + for config in `ls $CONFIG`; do + # Include BORG_PASSPHRASE config for each destination in an array + if grep -q "BORG_PASSCOMMAND" $CONFIG/$config; then + COMMAND="`grep BORG_PASSCOMMAND $CONFIG/$config | cut -d = -f 2 | sed -e "s/^'//" -e "s/'$//" -e 's/^"//' -e 's/"$//'`" + #BORG_PASSPHRASES[$config]="BORG_PASSPHRASE=`$COMMAND`" + BORG_PASSPHRASES[$config]="`$COMMAND`" + else + BORG_PASSPHRASES[$config]="" + fi + done + # Serial approach - # FIXME: export BORG_PASSPHRASE config for each destination using an array for config in `ls $CONFIG`; do info "Calling borger for $DESTINATION/$config..." + export BORG_PASSPHRASE="${BORG_PASSPHRASES[$config]}" $FULLNAME $DESTINATION/$config 2>&1 | sed -e "s/^\[borger\]/[borger] [$config]/" -e "s/^\([^\[]\)/[borger] [$config] \1/" done @@ -284,7 +305,10 @@ function borger_multiple { ## and call borger for each config in parallel #for config in `ls $CONFIG`; do # info "Calling borger for $DESTINATION/$config..." - # ( $FULLNAME $DESTINATION/$config $MULTIPLE_OPTION 2>&1 | sed -e "s/^\[borger\]/[borger] [$config]/" -e "s/^\([^\[]\)/[borger] [$config] \1/" ) & + # ( + # export BORG_PASSPHRASE="${BORG_PASSPHRASES[$config]}" + # $FULLNAME $DESTINATION/$config $MULTIPLE_OPTION 2>&1 | sed -e "s/^\[borger\]/[borger] [$config]/" -e "s/^\([^\[]\)/[borger] [$config] \1/" + # ) & #done ## Since we dispatched everything to subprocesses, @@ -316,7 +340,6 @@ function borger_continuous { # Run until interruption while true; do - # Run as a subprocess so we do not exit on any fatal error if [ "$MULTIPLE" == "yes" ]; then borger_multiple else -- cgit v1.2.3