From 8de34c192c04ce7325b3c151ef6e87642500c807 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Mon, 20 Feb 2017 16:01:54 +0100 Subject: backup.sh: Add configuration file support Signed-off-by: Florian Pritz --- backup.sh | 81 +++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 53 insertions(+), 28 deletions(-) (limited to 'backup.sh') diff --git a/backup.sh b/backup.sh index e64c9bd..d0d1274 100755 --- a/backup.sh +++ b/backup.sh @@ -5,7 +5,8 @@ # # Important steps: # - define a host "backup" in root's .ssh/config -# - read the script and adjust to your needs +# - You can override variables from main() as well as the pre_backup() and post_backup() functions in +# $XDG_CONFIG_HOME/backup-sh-conf.sh or /etc/backup-sh-conf.sh # - As root run `borg init -v --encryption=keyfile backup:borg-$HOSTNAME` # (note that zsh uses $HOST instead of $HOSTNAME) # - If you want, increase the max_segment_size in @@ -38,38 +39,52 @@ main() { TMPDIR="$(mktemp -d "/tmp/${0##*/}.XXXXXX")" trap "rm -rf '${TMPDIR}'" EXIT TERM - # these mountpoints will be excluded. mountpoints not listed in either this + # The backup repository used by borg + borg_repo="backup:borg-$HOSTNAME" + + # These mountpoints will be excluded. Mountpoints not listed in either this # or the includeMountpoints variable below will throw an error excludeMountpoints=( - /tmp - /sys - /dev - /proc - /run - /mnt/levant/nfs - /media + /tmp + /sys + /dev + /proc + /run + /media ) - # these mountpoints will be included + # These mountpoints will be included includeMountpoints=( - / - /boot - /home - /mnt/data + / + /boot + /home + /mnt/data ) - # list of patterns that should be excluded. This supports shell globbing as + # List of patterns that should be excluded. This supports shell globbing as # well as regex pattern. Refer to man borg for details. - IFS='' read -r -d '' excludeList_borg < "$TMPDIR/exclude-list-borg" + echo "$excludeList" > "$TMPDIR/exclude-list-borg" + run_if_exists pre_backup + backup_borg / "$borg_repo" + run_if_exists post_backup +} + +# This is called before creating the backup +pre_backup() { # save some data that's useful for restores local backupDataDir=/root/backup-data/ mkdir -p "$backupDataDir" @@ -80,12 +95,16 @@ EOF df -a > "$backupDataDir/df" findmnt -l > "$backupDataDir/findmnt" - # TODO: create snapshots here if possible. Sadly that's really difficult so - # it will probably not be implemented + # If you wish to use snapshots, create them here - local backupdir="borg-$HOSTNAME" - backup_borg / "backup:$backupdir" - ssh backup "touch $backupdir/last-backup-timestamp" + return +} + +# This is called after backup creation +post_backup() { + # If you need to perform any cleanup do so here + + return } backup_borg() { @@ -108,6 +127,12 @@ backup_borg() { ### support functions below ### +run_if_exists() { + if declare -F $1 &> /dev/null; then + $1 "${@:2}" + fi +} + ## # usage : in_array( $needle, $haystack ) # return : 0 - found @@ -136,7 +161,7 @@ exclude_mountpoints() { local error=0 for fs in "${excludeMountpoints[@]}"; do - excludeList_borg+="sh:$fs/*"$'\n' + excludeList+="sh:$fs/*"$'\n' done while read line; do -- cgit v1.2.3-24-g4f1b