]> gitweb.fluxo.info Git - scripts.git/commitdiff
Borger: support multiple destinations
authorSilvio Rhatto <rhatto@riseup.net>
Wed, 23 May 2018 12:18:15 +0000 (09:18 -0300)
committerSilvio Rhatto <rhatto@riseup.net>
Wed, 23 May 2018 12:18:15 +0000 (09:18 -0300)
borger

diff --git a/borger b/borger
index 33c313cc648fa339885bab74e779d738644af356..240de3691b8970607d76cd52414c3e534bcd7597 100755 (executable)
--- a/borger
+++ b/borger
@@ -3,21 +3,50 @@
 # Borg script for home folder backups.
 # Adapted from https://borgbackup.readthedocs.io/en/stable/quickstart.html#automating-backups
 #
-# Stuff to put in your config:
+# Example config to be put at ~/.config/borger/destination-name:
 #
-# Backup destination
-#export SSH_SERVER="user@host"
-#export SSH_PORT="2202"
-# Setting this, so you won't be asked for your repository passphrase:
-#export BORG_PASSPHRASE='HACKME'
-# or this to ask an external program to supply the passphrase:
-#export BORG_PASSCOMMAND='pass show backup'
-#export BORG_PASSCOMMAND='keyringer default decrypt pessoal/backups/borg 2> /dev/null'
+#   # Backup destination
+#   export SSH_SERVER="user@host"
+#   export SSH_PORT="2202"
+#
+#   # Repository path
+#   export BORG_REPO_DIR="/var/backups/users/$USER/borg"
+#   export BORG_REPO="ssh://$SSH_SERVER:$SSH_PORT/$BORG_REPO_DIR"
+#
+#   # Setting this, so you won't be asked for your repository passphrase:
+#   #export BORG_PASSPHRASE='HACKME'
+#   #export BORG_PASSCOMMAND='pass show backup'
+#   #export BORG_PASSCOMMAND='keyringer default decrypt pessoal/backups/borg 2> /dev/null'
+#
+#   # Backup config
+#   keepdaily="7"
+#   keepweekly="4"
+#   keepmonth="3"
+#   encryption="keyfile"
 
 # Parameters
-CONFIG="$HOME/.config/borger"
+BASENAME="`basename $0`"
+DESTINATION="$1"
+BASE_CONFIG="$HOME/.config/borger"
+CONFIG="$BASE_CONFIG/$DESTINATION"
 HOSTNAME=`cat /etc/hostname`
 
+# Some helpers and error handling:
+info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
+fatal() { info $*; exit 1; }
+
+# Ensure we have our base config folder
+mkdir -p $BASE_CONFIG
+
+# List available targets
+if [ -z "$DESTINATION" ]; then
+  echo "usage: $BASENAME <destination> [--check]"
+  echo -n "available destinations from $BASE_CONFIG: "
+  ls $BASE_CONFIG
+  exit 1
+fi
+
+# Ensure we have an username
 if [ -z "$USER" ]; then
   USER="`whoami`"
 fi
@@ -38,26 +67,29 @@ encryption="keyfile"
 # Config
 if [ -e "$CONFIG" ] ; then
   source $CONFIG
+else
+  fatal "No such config $CONFIG"
 fi
 
 # Setting this, so the repo does not need to be given on the commandline:
-export BORG_REPO="ssh://$SSH_SERVER:$SSH_PORT//var/backups/users/$USER/borg"
-
-# Some helpers and error handling:
-info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
-fatal() { info $*; exit 1; }
-trap 'info $( date ) Backup interrupted >&2; exit 2' INT TERM
+if [ -z "$BORG_REPO" ]; then
+  export BORG_REPO_DIR="/var/backups/users/$USER/borg"
+  export BORG_REPO="ssh://$SSH_SERVER:$SSH_PORT/$BORG_REPO_DIR"
+fi
 
 # Check
-if [ "$1" == "--check" ]; then
+if [ "$2" == "--check" ]; then
   borg list
   exit $?
 fi
 
+# Our trap
+trap 'info $( date ) Backup interrupted >&2; exit 2' INT TERM
+
 # Initialize
-if ! ssh $SSH_SERVER -p $SSH_PORT test -f /var/backups/users/$USER/borg/config; then
-  info "Initializing borg repository at ssh://$SSH_SERVER:$SSH_PORT//var/backups/users/$USER/borg..."
-  borg init --encryption=$encryption ssh://$SSH_SERVER:$SSH_PORT//var/backups/users/$USER/borg
+if ! ssh $SSH_SERVER -p $SSH_PORT test -f $BORG_REPO_DIR/config; then
+  info "Initializing borg repository at $BORG_REPO..."
+  borg init --encryption=$encryption $BORG_REPO
 
   init_exit=$?
 
@@ -97,7 +129,6 @@ info "Pruning repository..."
 
 borg prune                      \
   --list                        \
-  --prefix '{hostname}-'        \
   --show-rc                     \
   --keep-daily    $keepdaily    \
   --keep-weekly   $keepweekly   \