Skip to content

Instantly share code, notes, and snippets.

@leoberry
Forked from maethor/borg
Last active March 29, 2017 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leoberry/b6431b0fdb8f04693977d86dbfe8ca77 to your computer and use it in GitHub Desktop.
Save leoberry/b6431b0fdb8f04693977d86dbfe8ca77 to your computer and use it in GitHub Desktop.
BorgBackup handler script for backupninja
# -*- 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
#
# 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.
#
#
export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes
### GET CONFIG ###
getconf testconnect yes
getconf nicelevel 0
setsection source
getconf keepdaily 7
getconf keepweekly 4
getconf keepmonthly -1
getconf include
getconf exclude
setsection dest
getconf user
getconf host
getconf directory
# strip trailing /
directory=${directory%/}
getconf archive {now:%Y-%m-%d}
getconf compression lz4
getconf encryption none
getconf encryptkey
getconf bandwidthlimit 0
### CHECK CONFIG ###
execstr_precmd=
# check the connection at the source and destination
[ -n "$test" ] || test=0
if [ "$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 host is alive'`
if echo $ret | grep "host is alive"; then
debug "Connected to $host as $user successfully"
else
fatal "Can't connect to $host as $user."
fi
fi
## Check bandwidthlimit not egal 0
if [ "$bandwidthlimit" != 0 ]; then
execstr_precmd="trickle -s -d $bandwidthlimit -u $bandwidthlimit"
debug "Bandwidthlimit equal to $bandwidthlimit KB/s"
fi
# destination specific checks
[ "$directory" != "" ] || fatal "Destination directory not set"
execstr_repository="$user@$host:$directory"
execstr_archive="$archive"
## Check encryption
if [ "$encryption" != 'none' ]; then
if [ -z "$encryptkey" ]; then
fatal "The encryptkey option must be set when encryption's different of none"
else
export BORG_PASSPHRASE=$encryptkey
encrypt_option='--encryption=repokey'
fi
else
encrypt_option='--encryption=none'
fi
## TODO : encryption with keyfile
### INIT IF NEEDED ###
initstr="$execstr_precmd borg init $encrypt_option $execstr_repository"
debug "$initstr"
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
### EXECUTE ###
execstr="$execstr_precmd 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
# include client-part and server-part
execstr="${execstr} ${excludes} $execstr_repository::$execstr_archive ${includes}"
debug "$execstr"
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
prunestr="$exectstr_precmd borg prune --keep-daily $keepdaily --keep-weekly $keepweekly --keep-monthly $keepmonthly $execstr_repository"
debug "$prunestr"
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
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment