summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbackup.sh71
1 files changed, 71 insertions, 0 deletions
diff --git a/backup.sh b/backup.sh
new file mode 100755
index 0000000..6666527
--- /dev/null
+++ b/backup.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+# backup.sh
+#
+# This script is a template for a backup script which could be installed to
+# /usr/local/bin as a per-machine system backup script. You will have to tweak
+# it to your specific needs.
+#
+# The following dependencies have to be installed:
+#
+# - borg for the backup itself
+# - mariabackup to get a consistent snapshot of all MariaDB databases
+# - go-sendxmpp to notify in case of errors / success
+
+HOST='org.example.host'
+ADMIN='admin@example.org'
+REPO='/srv/borg'
+MARIADB_BACKUP_DIR='/var/local/mariadb_export'
+
+# create full backup for mariadb to ensure consistent state
+rm -rf ${MARIADB_BACKUP_DIR}
+mkdir -p ${MARIADB_BACKUP_DIR}
+chmod 700 ${MARIADB_BACKUP_DIR}
+if ! mariabackup --backup --target-dir=${MARIADB_BACKUP_DIR}/ --user=root
+then
+ echo "❌ Backup: MariaDB backup failed on ${HOST}." | go-sendxmpp "${ADMIN}"
+ exit 1
+fi
+
+# create a BorgBackup archive in the given repository
+if ! borg create \
+ --verbose \
+ --stats \
+ --compression zstd \
+ --exclude 'dev/*' \
+ --exclude 'lost+found/*' \
+ --exclude 'media/*' \
+ --exclude 'mnt/*' \
+ --exclude 'proc/*' \
+ --exclude 'run/*' \
+ --exclude 'sys/*' \
+ --exclude 'tmp/*' \
+ --exclude 'srv/borg/*' \ # change / remove me based on REPO variable
+ "${REPO}::${HOST}_{now:%Y-%m-%d_%H:%M:%S}" \
+ /
+then
+ echo "❌ Backup: borg create failed on ${HOST}." | go-sendxmpp "${ADMIN}"
+ exit 1
+fi
+
+# prune existing archives in the repository to reduce disk usage
+if ! borg prune \
+ --glob-archives "${HOST}*" \
+ --keep-daily 7 \
+ --keep-weekly 4 \
+ --keep-monthly 6 \
+ "${REPO}"
+then
+ echo "❌ Backup: borg prune failed on ${HOST}." | go-sendxmpp "${ADMIN}"
+ exit 1
+fi
+
+# reduce repository disk usage by optimizing the repository storage
+if ! borg compact "${REPO}"
+then
+ echo "❌ Backup: borg compact failed on ${HOST}." | go-sendxmpp "${ADMIN}"
+ exit 1
+fi
+
+# inform admin about success if not exited before
+echo "✅ System backup succeeded on ${HOST}." | go-sendxmpp "${ADMIN}"