aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvio Rhatto <rhatto@riseup.net>2019-08-06 22:15:52 -0300
committerSilvio Rhatto <rhatto@riseup.net>2019-08-06 22:15:52 -0300
commit4a09769d421994f5351851bff18c74301c8a528f (patch)
treeb9637530e2af7555c3ccc8212f18ac6f799342f3
parenta8f3112b4066a7bcad5e80ec658704baf5ca9e4d (diff)
downloadborger-4a09769d421994f5351851bff18c74301c8a528f.tar.gz
borger-4a09769d421994f5351851bff18c74301c8a528f.tar.bz2
Major refactor
-rwxr-xr-xborger175
1 files changed, 106 insertions, 69 deletions
diff --git a/borger b/borger
index 095f6a6..3448101 100755
--- a/borger
+++ b/borger
@@ -1,12 +1,26 @@
-#!/bin/bash
+#!/usr/bin/env bash
#
# Borg script for home folder backups.
# Adapted from https://borgbackup.readthedocs.io/en/stable/quickstart.html#automating-backups
#
-# See also:
+# See also: https://borgbackup.readthedocs.io/en/stable/faq.html#if-a-backup-stops-mid-way-does-the-already-backed-up-data-stay-there
+# https://borgbackup.readthedocs.io/en/stable/usage/help.html#borg-help-placeholders
+#
+# Copyright (C) 2019 Silvio Rhatto - rhatto at riseup.net
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published
+# by the Free Software Foundation, either version 3 of the License,
+# or any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
-# https://borgbackup.readthedocs.io/en/stable/faq.html#if-a-backup-stops-mid-way-does-the-already-backed-up-data-stay-there
-# https://borgbackup.readthedocs.io/en/stable/usage/help.html#borg-help-placeholders
# Parameters
FULLNAME="$0"
@@ -35,44 +49,29 @@ function borger_usage {
exit 1
}
-# Check if in multiple mode
-function borger_check_multiple {
+# Setup
+function borger_setup {
+ # Ensure we have our base config folder
+ mkdir -p $BASE_CONFIG
+
+ # Check config
if [ ! -e "$CONFIG" ]; then
fatal "No such config $CONFIG"
exit 1
elif [ -d "$CONFIG" ]; then
MULTIPLE="yes"
+ fi
- # Avoid too many loops
- if [ "$OPTION" == "--continuous" ]; then
- MULTIPLE_OPTION=""
- else
- MULTIPLE_OPTION="$OPTION"
- fi
-
- info "Multiple destination \"$DESTINATION\" found. Processing each subconfig..."
-
- # Config is a folder, so we iterate over all items
- # 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/" ) &
- done
-
- # Since we dispatched everything to subprocesses,
- # there's nothing to do here.
- #exit
- wait
+ # Lockfile location
+ if [ "$OPTION" == "--continuous" ]; then
+ LOCKFILE="$TMP/$BASENAME/$DESTINATION-continuous.lock"
+ else
+ LOCKFILE="$TMP/$BASENAME/$DESTINATION.lock"
fi
}
# Process configuration
function borger_config {
- # If running on multiple mode, stop here
- if [ "$MULTIPLE" == "yes" ]; then
- exit
- fi
-
# Ensure we have an username
if [ -z "$USER" ]; then
USER="`whoami`"
@@ -100,13 +99,6 @@ function borger_config {
export BORG_REPO_DIR="/var/backups/users/$USER/borg"
export BORG_REPO="ssh://$SSH_SERVER:$SSH_PORT/$BORG_REPO_DIR"
fi
-
- # Lockfile location
- if [ "$OPTION" == "--continuous" ]; then
- LOCKFILE="$TMP/$BASENAME/$DESTINATION-continuous.lock"
- else
- LOCKFILE="$TMP/$BASENAME/$DESTINATION.lock"
- fi
}
# List
@@ -245,17 +237,63 @@ function borger_check_lockfile {
# Main backup procedure
function borger_run {
- borger_check_multiple
- borger_config
borger_check_lockfile
borger_set_lockfile
- borger_trap
- borger_init
- borger_create
- borger_prune
- borger_exit
+
+ # Run for single or multiple destinations
+ if [ "$MULTIPLE" == "yes" ]; then
+ borger_multiple
+ else
+ borger_config
+ borger_trap
+ borger_init
+ borger_create
+ borger_prune
+ borger_exit
+ fi
+}
+
+# Run for a single destination
+function borger_single {
+ borger_config
+
+ # Convert the pass command to passphrase otherwise
+ # the user would be interrupted by a passphrase prompt
+ # at every iteration
+ if [ ! -z "$BORG_PASSCOMMAND" ]; then
+ export BORG_PASSPHRASE="`$BORG_PASSCOMMAND`"
+ export BORG_PASSCOMMAND=""
+ fi
+
+ $FULLNAME $DESTINATION
+}
+
+# Run for multiple destinations
+function borger_multiple {
+ info "Multiple destination \"$DESTINATION\" found. Processing each subconfig..."
+
+ # 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..."
+ $FULLNAME $DESTINATION/$config 2>&1 | sed -e "s/^\[borger\]/[borger] [$config]/" -e "s/^\([^\[]\)/[borger] [$config] \1/"
+ done
+
+ # Parallel approach
+ ## Config is a folder, so we iterate over all items
+ ## 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/" ) &
+ #done
+
+ ## Since we dispatched everything to subprocesses,
+ ## there's nothing to do here.
+ ##exit
+ #wait
}
+# Exit procedure
function borger_exit {
# Use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
@@ -271,8 +309,28 @@ function borger_exit {
exit ${global_exit}
}
-# Ensure we have our base config folder
-mkdir -p $BASE_CONFIG
+# Continuous backup processing
+function borger_continuous {
+ borger_check_lockfile
+ borger_set_lockfile
+
+ # 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
+ borger_single
+ fi
+
+ # Wait
+ info "Running on continous mode... sleeping $INTERVAL..."
+ sleep $INTERVAL
+ done
+}
+
+# Setup
+borger_setup
# Dispatch
if [ -z "$DESTINATION" ]; then
@@ -280,35 +338,14 @@ if [ -z "$DESTINATION" ]; then
elif [ -z "$OPTION" ]; then
borger_run
elif [ "$OPTION" == "--list" ]; then
- borger_check_multiple
borger_config
borger_list
elif [ "$OPTION" == "--check" ]; then
- borger_check_multiple
borger_config
borger_check
elif [ "$OPTION" == "--info" ]; then
- borger_check_multiple
borger_config
borger_info
elif [ "$OPTION" == "--continuous" ]; then
- borger_check_multiple
- borger_config
- borger_check_lockfile
- borger_set_lockfile
-
- # Convert the pass command to passphrase otherwise
- # the user would be interrupted by a passphrase prompt
- # at every iteration
- if [ ! -z "$BORG_PASSCOMMAND" ]; then
- export BORG_PASSPHRASE="`$BORG_PASSCOMMAND`"
- export BORG_PASSCOMMAND=""
- fi
-
- while true; do
- # Run as a subprocess so we do not exit on any fatal error
- $FULLNAME $DESTINATION
- info "Running on continous mode... sleeping $INTERVAL..."
- sleep $INTERVAL
- done
+ borger_continuous
fi