2016-07-03 09:29:44 +02:00
|
|
|
# -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
|
|
|
|
# vim: set filetype=sh sw=3 sts=3 expandtab autoindent:
|
|
|
|
#
|
|
|
|
# borg handler script for backupninja
|
|
|
|
# requires borgbackup
|
|
|
|
#
|
2016-09-13 20:15:38 +02:00
|
|
|
# Guillaume Subiron, Sysnove, 2016
|
|
|
|
#
|
|
|
|
# Copyright 2016 Guillaume Subiron <guillaume@sysnove.fr>
|
|
|
|
#
|
|
|
|
# This work is free. You can redistribute it and/or modify it under the
|
|
|
|
# terms of the Do What The Fuck You Want To Public License, Version 2,
|
|
|
|
# as published by Sam Hocevar. See the http://www.wtfpl.net/ file for more details.
|
|
|
|
#
|
2016-07-03 09:29:44 +02:00
|
|
|
#
|
|
|
|
|
2021-01-03 20:30:20 +01:00
|
|
|
debug "export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes"
|
2016-07-03 09:29:44 +02:00
|
|
|
export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes
|
2021-01-03 20:30:20 +01:00
|
|
|
|
|
|
|
debug "export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=yes"
|
2018-01-26 17:32:04 +01:00
|
|
|
export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=yes
|
2016-07-03 09:29:44 +02:00
|
|
|
|
|
|
|
### GET CONFIG ###
|
|
|
|
|
|
|
|
getconf testconnect yes
|
|
|
|
getconf nicelevel 0
|
2020-05-16 18:02:43 +02:00
|
|
|
getconf ionicelevel
|
2018-01-19 21:02:54 +01:00
|
|
|
getconf bwlimit
|
2016-07-03 09:29:44 +02:00
|
|
|
|
|
|
|
setsection source
|
2018-01-26 15:53:54 +01:00
|
|
|
getconf init yes
|
2016-07-03 09:29:44 +02:00
|
|
|
getconf include
|
|
|
|
getconf exclude
|
2018-01-26 17:47:47 +01:00
|
|
|
getconf create_options
|
2018-01-26 17:26:37 +01:00
|
|
|
getconf prune yes
|
|
|
|
getconf keep 30d
|
|
|
|
getconf prune_options
|
2020-08-14 14:46:34 +02:00
|
|
|
getconf cache_directory
|
2016-07-03 09:29:44 +02:00
|
|
|
|
|
|
|
setsection dest
|
|
|
|
getconf user
|
|
|
|
getconf host
|
2020-07-16 10:37:28 +02:00
|
|
|
getconf port 22
|
2016-07-03 09:29:44 +02:00
|
|
|
getconf directory
|
|
|
|
# strip trailing /
|
|
|
|
directory=${directory%/}
|
2018-01-15 00:31:42 +01:00
|
|
|
getconf archive {now:%Y-%m-%dT%H:%M:%S}
|
2016-07-03 09:29:44 +02:00
|
|
|
getconf compression lz4
|
2018-01-15 00:14:08 +01:00
|
|
|
getconf encryption none
|
2017-07-30 10:39:01 +02:00
|
|
|
getconf passphrase
|
|
|
|
|
2021-01-03 20:30:20 +01:00
|
|
|
debug "export BORG_PASSPHRASE=\"<redacted>\""
|
2017-09-10 11:15:37 +02:00
|
|
|
export BORG_PASSPHRASE="$passphrase"
|
2016-07-03 09:29:44 +02:00
|
|
|
|
|
|
|
### CHECK CONFIG ###
|
|
|
|
|
2021-01-03 18:18:46 +01:00
|
|
|
# source includes at least one path
|
|
|
|
[ -n "$include" ] || fatal "No source includes specified"
|
|
|
|
|
2016-07-03 09:29:44 +02:00
|
|
|
# destination specific checks
|
2021-01-03 20:31:12 +01:00
|
|
|
[ -n "$directory" ] || fatal "Destination directory not set"
|
2016-07-03 09:29:44 +02:00
|
|
|
if [ "$host" != "localhost" ]; then
|
2021-01-03 20:31:12 +01:00
|
|
|
[ -n "$user" ] || fatal "Destination user not set"
|
|
|
|
[ -n "$host" ] || fatal "Destination host not set"
|
2020-07-16 10:37:28 +02:00
|
|
|
execstr_repository="ssh://${user}@${host}:${port}${directory}"
|
2016-07-03 09:29:44 +02:00
|
|
|
else
|
|
|
|
execstr_repository="$directory"
|
|
|
|
fi
|
|
|
|
execstr_archive="$archive"
|
|
|
|
|
2020-08-14 14:46:34 +02:00
|
|
|
if [ -n "$cache_directory" ]; then
|
|
|
|
cache_parent_dir=$(dirname "$(readlink -f "$cache_directory")")
|
|
|
|
[ -d "$cache_parent_dir" ] || fatal "Cache directory parent dir '$cache_parent_dir' is absent or is not a directory."
|
2021-01-03 20:30:20 +01:00
|
|
|
debug "export BORG_CACHE_DIR=\"${cache_directory}\""
|
|
|
|
export BORG_CACHE_DIR="$cache_directory"
|
2020-08-14 14:46:34 +02:00
|
|
|
else
|
|
|
|
# Cache dir not set, let's clear out the environment variable to avoid
|
|
|
|
# having this directory be pointed to a random destination.
|
|
|
|
# Also apparently if we set the variable to an empty string, borg uses the
|
|
|
|
# empty string as though it was some path we specified and backup runs
|
|
|
|
# error out, so we need to unset the variable completely.
|
2021-01-03 20:30:20 +01:00
|
|
|
debug "unset BORG_CACHE_DIR"
|
2020-08-14 14:46:34 +02:00
|
|
|
unset BORG_CACHE_DIR
|
|
|
|
fi
|
|
|
|
|
2020-05-16 18:02:43 +02:00
|
|
|
# Check that the ionicelevel is valid
|
|
|
|
if [ -n "$ionicelevel" ] && echo "$ionicelevel" | grep -vq "^[0-7]$"; then
|
|
|
|
fatal "The value of ionicelevel is expected to be either empty or an integer from 0 to 7. Got: $ionicelevel"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Only use ionice if ionicelevel is not empty
|
|
|
|
nice="nice -n $nicelevel"
|
|
|
|
if [ -n "$ionicelevel" ]; then
|
|
|
|
nice="ionice -c2 -n $ionicelevel $nice"
|
|
|
|
fi
|
|
|
|
|
2017-09-10 11:15:37 +02:00
|
|
|
# check the connection at the source and destination
|
|
|
|
[ -n "$test" ] || test=0
|
|
|
|
if [ "$host" != "localhost" ] && ([ "$testconnect" = "yes" ] || [ "${test}" -eq 1 ]); then
|
2020-07-16 10:37:28 +02:00
|
|
|
debug "ssh -o PasswordAuthentication=no $host -p $port -l $user 'echo -n 1'"
|
|
|
|
local ret=`ssh -o PasswordAuthentication=no $host -p $port -l $user 'echo -n 1'`
|
2017-09-10 11:15:37 +02:00
|
|
|
if [ "$ret" = 1 ]; then
|
|
|
|
debug "Connected to $host as $user successfully"
|
|
|
|
else
|
|
|
|
teststr="borg list --show-rc -v $execstr_repository"
|
|
|
|
debug "$teststr"
|
|
|
|
output=`su -c "$teststr" 2>&1`
|
|
|
|
if echo "$output" | grep "terminating with success status" ; then
|
|
|
|
debug "Connected to $host as $user successfully (forced command)"
|
|
|
|
else
|
|
|
|
if echo "$output" | grep -E "Repository.+does not exist" ; then
|
|
|
|
debug "Connected to $host as $user successfully (forced command)"
|
|
|
|
else
|
|
|
|
fatal "Can't connect to $host as $user."
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2016-07-03 09:29:44 +02:00
|
|
|
### INIT IF NEEDED ###
|
|
|
|
|
2018-01-26 15:53:54 +01:00
|
|
|
if [ "$init" == "yes" ]; then
|
|
|
|
initstr="borg init --encryption=$encryption $execstr_repository"
|
2021-01-04 00:00:36 +01:00
|
|
|
debug "executing borg init"
|
2018-01-26 15:53:54 +01:00
|
|
|
debug "$initstr"
|
|
|
|
if [ $test = 0 ]; then
|
|
|
|
output="`su -c "$initstr" 2>&1`"
|
|
|
|
if [ $? = 2 ]; then
|
|
|
|
debug $output
|
|
|
|
info "Repository was already initialized"
|
|
|
|
else
|
|
|
|
warning $output
|
|
|
|
warning "Repository has been initialized"
|
|
|
|
fi
|
2018-01-14 21:24:07 +01:00
|
|
|
fi
|
2016-07-03 09:29:44 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
### EXECUTE ###
|
|
|
|
|
|
|
|
execstr="borg create --stats --compression $compression"
|
|
|
|
|
|
|
|
set -o noglob
|
|
|
|
|
|
|
|
# includes
|
|
|
|
SAVEIFS=$IFS
|
|
|
|
IFS=$(echo -en "\n\b")
|
|
|
|
for i in $include; do
|
|
|
|
includes="${includes} '$i'"
|
|
|
|
done
|
|
|
|
IFS=$SAVEIFS
|
|
|
|
|
|
|
|
# excludes
|
|
|
|
SAVEIFS=$IFS
|
|
|
|
IFS=$(echo -en "\n\b")
|
|
|
|
for i in $exclude; do
|
|
|
|
excludes="${excludes} --exclude '$i'"
|
|
|
|
done
|
|
|
|
IFS=$SAVEIFS
|
|
|
|
|
|
|
|
set +o noglob
|
|
|
|
|
2018-01-19 21:02:54 +01:00
|
|
|
if [ ! -z $bwlimit ]; then
|
|
|
|
execstr="${execstr} --remote-ratelimit=${bwlimit}"
|
|
|
|
fi
|
|
|
|
|
2020-09-08 12:08:04 +02:00
|
|
|
if [ ! -z "$create_options" ]; then
|
2018-01-26 17:47:47 +01:00
|
|
|
execstr="${execstr} ${create_options}"
|
2018-01-19 21:09:03 +01:00
|
|
|
fi
|
|
|
|
|
2016-07-03 09:29:44 +02:00
|
|
|
# include client-part and server-part
|
|
|
|
execstr="${execstr} ${excludes} $execstr_repository::$execstr_archive ${includes}"
|
2021-01-04 00:00:36 +01:00
|
|
|
|
|
|
|
debug "executing borg create"
|
2020-05-16 18:02:43 +02:00
|
|
|
debug "$nice $execstr"
|
2018-01-12 23:51:58 +01:00
|
|
|
|
2016-07-03 09:29:44 +02:00
|
|
|
if [ $test = 0 ]; then
|
2020-05-16 18:02:43 +02:00
|
|
|
output=`$nice su -c "$execstr" 2>&1`
|
2016-07-03 09:29:44 +02:00
|
|
|
if [ $? = 0 ]; then
|
|
|
|
debug $output
|
2021-01-02 20:59:58 +01:00
|
|
|
info "Successfully finished backing up source."
|
2016-07-03 09:29:44 +02:00
|
|
|
else
|
|
|
|
error $output
|
2021-01-02 20:59:58 +01:00
|
|
|
fatal "Failed backing up source."
|
2016-07-03 09:29:44 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
### REMOVE OLD BACKUPS ###
|
|
|
|
|
|
|
|
# borg prune
|
2018-01-19 23:19:31 +01:00
|
|
|
if [ "$prune" == "yes" ]; then
|
2018-01-26 17:37:04 +01:00
|
|
|
if [ ! "$keep" == "0" ]; then
|
2018-01-26 17:26:37 +01:00
|
|
|
prune_options="${prune_options} --keep-within=${keep}"
|
|
|
|
fi
|
|
|
|
prunestr="borg prune $prune_options $execstr_repository"
|
2021-01-04 00:00:36 +01:00
|
|
|
debug "executing borg prune"
|
2018-01-26 17:26:37 +01:00
|
|
|
debug "$prunestr"
|
2018-01-19 21:11:50 +01:00
|
|
|
if [ $test = 0 ]; then
|
|
|
|
output="`su -c "$prunestr" 2>&1`"
|
|
|
|
if [ $? = 0 ]; then
|
|
|
|
debug $output
|
|
|
|
info "Removing old backups succeeded."
|
|
|
|
else
|
|
|
|
warning $output
|
|
|
|
warning "Failed removing old backups."
|
|
|
|
fi
|
2018-01-12 23:51:58 +01:00
|
|
|
fi
|
2016-07-03 09:29:44 +02:00
|
|
|
fi
|
|
|
|
|
2021-01-03 20:30:20 +01:00
|
|
|
debug "unset BORG_PASSPHRASE"
|
2017-07-30 10:39:01 +02:00
|
|
|
unset BORG_PASSPHRASE
|
|
|
|
|
2016-07-03 09:29:44 +02:00
|
|
|
return 0
|