diff --git a/examples/example.borg b/examples/example.borg new file mode 100644 index 0000000000000000000000000000000000000000..4abc2c0a91575c33dbf1c06ed7719fee376f87f8 --- /dev/null +++ b/examples/example.borg @@ -0,0 +1,121 @@ +## +## This is an example borgbackup configuration file. +## +## Here you can find all the possible borgbackup options, details of +## what the options provide and possible settings. The defaults are set +## as the commented out option, uncomment and change when +## necessary. Options which are uncommented in this example do not have +## defaults, and the settings provided are recommended. +## +## The defaults are useful in most cases, just make sure to configure the +## destination host and user. +## + +## default is 0, but set to 19 if you want to lower the priority. +## an example setting would be: +## nicelevel = 19 +## +## Default +# nicelevel = 0 + +## default is yes. set to no to skip the test if the remote host is alive +## +## Default: +# testconnect = no + +###################################################### +## source section +## (where the files to be backed up are coming from) + +[source] + +## how many days, weeks and months of data to keep +## for more info see : borg prune -h +## +## Default: +# keepdaily = 7 +# keepweekly = 4 +# keepmonthly = -1 + +## A few notes about includes and excludes: +## 1. include, exclude statements support globbing with '*' +## 2. Symlinks are not dereferenced. Moreover, an include line whose path +## contains, at any level, a symlink to a directory, will only have the +## symlink backed-up, not the target directory's content. Yes, you have to +## dereference yourself the symlinks, or to use 'mount --bind' instead. +## Example: let's say /home is a symlink to /mnt/crypt/home ; the following +## line will only backup a "/home" symlink ; neither /home/user nor +## /home/user/Mail will be backed-up : +## include = /home/user/Mail +## A workaround is to 'mount --bind /mnt/crypt/home /home' ; another one is to +## write : +## include = /mnt/crypt/home/user/Mail +## 3. All the excludes come after all the includes. The order is not otherwise +## taken into account. + +## files to include in the backup +include = /opt +include = /srv +include = /etc +include = /root +include = /home +include = /usr/local + +## files to exclude from the backup +include = /var +exclude = /var/lock +exclude = /var/run +exclude = /var/cache +exclude = /var/tmp +exclude = /var/lib/mongodb/journal +exclude = /var/lib/clamav +exclude = /var/lib/mlocate +exclude = /var/lib/postgresql +exclude = /var/lib/mysql + +###################################################### +## destination section +## (where the files are copied to) + +[dest] + +## put the backups under this directory, this must be set! +## an example setting would be: +## directory = /backups +## +## Default: +# directory = + +## the machine which will receive the backups. +## an example setting would be: +## host = backuphost +## +## Default +# host = + +## make the files owned by this user. you must be able to +## `su -c "ssh backupuser@backhost"` without specifying a password. +## an example setting would be: +## user = backupuser +## +## Default: +# user = + +## archive name, should be unique every day. +## Format tags available : +## {now}, {utcnow}, {fqdn}, {hostname}, {user}, {pid} +## for more info see : borg create -h +## +## Default: +# archive = {now:%Y-%m-%d} + +## compression algorithm +## can be "none", "lz4", "zlib", "zlib,0..zlib,9", "lzma", "lzma,0..lzma,9". +## - "none" stands for "no compression" +## - "lz4" is super fast, but low compression +## - "zlib" is less fast, but higher compression +## - "lzma" is even slower, even higher compression +## for more info see : borg create -h +## +## Default: +# compression = lz4 \ No newline at end of file diff --git a/handlers/borg b/handlers/borg new file mode 100644 index 0000000000000000000000000000000000000000..9e2fb2d835dce017172cfd867c6b4df858064d5c --- /dev/null +++ b/handlers/borg @@ -0,0 +1,120 @@ +# -*- 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 +# + +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 + +### CHECK CONFIG ### + +# 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 + +# destination specific checks +[ "$directory" != "" ] || fatal "Destination directory not set" +execstr_repository="$user@$host:$directory" +execstr_archive="$archive" + +### INIT IF NEEDED ### + +# :TODO:maethor:160509: Manage encryption + +initstr="borg init --encryption=none $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="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="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 \ No newline at end of file