summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xbackup.sh81
1 files changed, 53 insertions, 28 deletions
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 <<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