diff options
author | Patrick Spek <p.spek@tyil.nl> | 2022-05-23 13:12:20 +0200 |
---|---|---|
committer | Patrick Spek <p.spek@tyil.nl> | 2022-05-23 13:12:20 +0200 |
commit | 85d5e68b4afcc3d804eb798fcda97f337ca081be (patch) | |
tree | 313ddd5aaa39af8893d8f153ec5b4b0bf9742c9f /playbooks.d/backup-borg/share | |
parent | 6ad534125b9f3aeb05ad23c5e2663503e07f4a3c (diff) |
Add playbook for borg
Diffstat (limited to 'playbooks.d/backup-borg/share')
-rw-r--r-- | playbooks.d/backup-borg/share/backup.bash | 59 |
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 "$@" |