diff options
author | Florian Pritz <bluewind@xinu.at> | 2017-02-20 16:01:54 +0100 |
---|---|---|
committer | Florian Pritz <bluewind@xinu.at> | 2017-02-20 16:01:54 +0100 |
commit | 8de34c192c04ce7325b3c151ef6e87642500c807 (patch) | |
tree | 0a01fd1345ab027d03faebe7c0dc8f22fcc1e254 | |
parent | a9b1bb5f0a639d51345e2735a9e376bbf18ee3ad (diff) | |
download | bin-8de34c192c04ce7325b3c151ef6e87642500c807.tar.gz bin-8de34c192c04ce7325b3c151ef6e87642500c807.tar.xz |
backup.sh: Add configuration file support
Signed-off-by: Florian Pritz <bluewind@xinu.at>
-rwxr-xr-x | backup.sh | 81 |
1 files changed, 53 insertions, 28 deletions
@@ -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 <<EOF || true -sh:/home/flo/tmp/* -sh:/home/*/.cache/* -sh:/root/.cache/* -sh:/var/cache/pacman/pkg/* -EOF + IFS='' read -r -d '' excludeList <<-EOF || true + sh:/home/*/.cache/* + sh:/root/.cache/* + sh:/var/cache/pacman/pkg/* + EOF + + for configfile in "${XDG_CONFIG_HOME:-$HOME/.config}/backup-sh-conf.sh" /etc/backup-sh-conf.sh; do + if [[ -e "$configfile" ]]; then + source "$configfile" + fi + done exclude_mountpoints - echo "$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 |