summaryrefslogtreecommitdiff
path: root/playbooks.d/backup-borg/share/backup.bash
diff options
context:
space:
mode:
Diffstat (limited to 'playbooks.d/backup-borg/share/backup.bash')
-rw-r--r--playbooks.d/backup-borg/share/backup.bash59
1 files changed, 59 insertions, 0 deletions
diff --git a/playbooks.d/backup-borg/share/backup.bash b/playbooks.d/backup-borg/share/backup.bash
new file mode 100644
index 0000000..0f9d5b7
--- /dev/null
+++ b/playbooks.d/backup-borg/share/backup.bash
@@ -0,0 +1,59 @@
+#!/usr/bin/env bash
+
+main() {
+ BORG_PASSPHRASE="$(bashtard var backup.key | awk -F= '{ print $NF }')"
+
+ if [[ -z "$BORG_PASSPHRASE" ]]
+ then
+ return 3
+ fi
+
+ export BORG_PASSPHRASE
+
+ backup_filesystem
+ backup_database_postgresql
+}
+
+backup_filesystem() {
+ if ! command -v "borg" > /dev/null 2>&1
+ then
+ return 4
+ fi
+
+ if ! command -v "borgmatic" > /dev/null 2>&1
+ then
+ return 4
+ fi
+
+ borgmatic
+}
+
+backup_database_postgresql() {
+ if ! command -v "psql" > /dev/null 2>&1
+ then
+ return 4
+ fi
+
+ if ! command -v "pg_dump" > /dev/null 2>&1
+ then
+ return 4
+ fi
+
+ local remote
+
+ remote="$(bashtard var backup.db.postgresql.remote_base)"
+ PGUSER="$(bashtard var backup.db.postgresql.user | awk -F= '{ print $NF }')"
+
+ export PGUSER
+
+ while read -r database
+ do
+ [[ $database == "postgres" ]] && continue
+ [[ $database =~ template* ]] && continue
+
+ pg_dump "$database" \
+ | borg create "$remote-$database::$(date -u +%FT%TZ)"
+ done < <(psql -AXt -d template1 -c "SELECT datname FROM pg_database")
+}
+
+main "$@"