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
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes
|
|
|
|
|
|
|
|
|
|
### GET CONFIG ###
|
|
|
|
|
|
|
|
|
|
getconf testconnect yes
|
|
|
|
|
getconf nicelevel 0
|
2018-01-19 21:02:54 +01:00
|
|
|
|
getconf bwlimit
|
2016-07-03 09:29:44 +02:00
|
|
|
|
|
|
|
|
|
setsection source
|
2018-01-15 01:06:34 +01:00
|
|
|
|
getconf prune yes
|
2018-01-13 00:07:04 +01:00
|
|
|
|
getconf keephourly 1
|
2016-07-03 09:29:44 +02:00
|
|
|
|
getconf keepdaily 7
|
|
|
|
|
getconf keepweekly 4
|
|
|
|
|
getconf keepmonthly -1
|
|
|
|
|
getconf include
|
|
|
|
|
getconf exclude
|
2018-01-19 21:09:03 +01:00
|
|
|
|
getconf excludecaches no
|
|
|
|
|
getconf excludenodump no
|
2016-07-03 09:29:44 +02:00
|
|
|
|
|
|
|
|
|
setsection dest
|
|
|
|
|
getconf user
|
|
|
|
|
getconf host
|
|
|
|
|
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
|
|
|
|
|
|
2017-09-10 11:15:37 +02:00
|
|
|
|
export BORG_PASSPHRASE="$passphrase"
|
2016-07-03 09:29:44 +02:00
|
|
|
|
|
|
|
|
|
### CHECK CONFIG ###
|
|
|
|
|
|
|
|
|
|
# destination specific checks
|
|
|
|
|
[ "$directory" != "" ] || fatal "Destination directory not set"
|
|
|
|
|
if [ "$host" != "localhost" ]; then
|
|
|
|
|
execstr_repository="$user@$host:$directory"
|
|
|
|
|
else
|
|
|
|
|
execstr_repository="$directory"
|
|
|
|
|
fi
|
|
|
|
|
execstr_archive="$archive"
|
|
|
|
|
|
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
|
|
|
|
|
debug "ssh -o PasswordAuthentication=no $host -l $user 'echo -n 1'"
|
|
|
|
|
local ret=`ssh -o PasswordAuthentication=no $host -l $user 'echo -n 1'`
|
|
|
|
|
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 ###
|
|
|
|
|
|
2017-07-30 10:39:01 +02:00
|
|
|
|
initstr="borg init --encryption=$encryption $execstr_repository"
|
2016-07-03 09:29:44 +02:00
|
|
|
|
|
|
|
|
|
debug "$initstr"
|
|
|
|
|
|
|
|
|
|
if [ $test = 0 ]; then
|
2018-01-14 21:24:07 +01:00
|
|
|
|
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
|
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
|
|
|
|
|
|
2018-01-19 21:09:03 +01:00
|
|
|
|
if [ "$excludecaches" == "yes" ]; then
|
|
|
|
|
execstr="${execstr} --exclude-caches"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ "$excludenodump" == "yes" ]; then
|
|
|
|
|
execstr="${execstr} --exclude-nodump"
|
|
|
|
|
fi
|
|
|
|
|
|
2016-07-03 09:29:44 +02:00
|
|
|
|
# include client-part and server-part
|
|
|
|
|
execstr="${execstr} ${excludes} $execstr_repository::$execstr_archive ${includes}"
|
|
|
|
|
debug "$execstr"
|
2018-01-12 23:51:58 +01:00
|
|
|
|
|
2016-07-03 09:29:44 +02:00
|
|
|
|
if [ $test = 0 ]; then
|
|
|
|
|
output=`nice -n $nicelevel su -c "$execstr" 2>&1`
|
|
|
|
|
if [ $? = 0 ]; then
|
|
|
|
|
debug $output
|
|
|
|
|
info "Successfully finished backing up source $label"
|
|
|
|
|
else
|
|
|
|
|
error $output
|
|
|
|
|
fatal "Failed backuping up source $label"
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
### REMOVE OLD BACKUPS ###
|
|
|
|
|
|
|
|
|
|
# borg prune
|
2018-01-13 00:07:04 +01:00
|
|
|
|
prunestr="borg prune --keep-hourly $keephourly --keep-daily $keepdaily --keep-weekly $keepweekly --keep-monthly $keepmonthly $execstr_repository"
|
2016-07-03 09:29:44 +02:00
|
|
|
|
debug "$prunestr"
|
2018-01-12 23:51:58 +01:00
|
|
|
|
|
2018-01-15 01:06:34 +01:00
|
|
|
|
if [ $test = 0 ] && [ "$prune" == "yes" ]; then
|
2018-01-12 23:51:58 +01:00
|
|
|
|
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
|
2016-07-03 09:29:44 +02:00
|
|
|
|
fi
|
|
|
|
|
|
2017-07-30 10:39:01 +02:00
|
|
|
|
unset BORG_PASSPHRASE
|
|
|
|
|
|
2016-07-03 09:29:44 +02:00
|
|
|
|
return 0
|