backup: backup each database separately

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2022-12-08 12:45:05 +01:00
parent 9951f6a33d
commit 8c10ac4c08
No known key found for this signature in database
GPG Key ID: FE65CD384D5BF7B4
2 changed files with 23 additions and 8 deletions

View File

@ -9,11 +9,4 @@
systemd_timer_timeout_sec: '{{ postgres_ha_backup_timeout }}'
systemd_timer_after_extra: 'docker.service'
systemd_timer_start_on_creation: false
systemd_timer_script_content: |
#!/usr/bin/env bash
BKP_DIR="{{ postgres_ha_cont_backup_vol }}"
rm -vfr "${BKP_DIR}"
/usr/bin/docker exec -i {{ postgres_ha_cont_name }} \
pg_dump -F directory -f "/backup/{{ postgres_ha_db_name }}" \
-U {{ postgres_ha_admin_user }} {{ postgres_ha_db_name }}
chmod 750 -R "${BKP_DIR}"
systemd_timer_script_content: '{{ lookup("template", "backup.sh.j2") }}'

22
templates/backup.sh.j2 Normal file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env bash
# vim: ft=sh
set -e
BKP_DIR="{{ postgres_ha_cont_backup_vol }}"
DATABASES=(
"{{ postgres_ha_db_name }}"
{% for db in postgres_ha_databases %}
"{{ db.get("name", db.user) }}"
{% endfor %}
)
for DB in "${DATABASES[@]}"; do
DB_DUMP_DIR="${BKP_DIR}/${DB}"
echo "pg_dump: ${DB} > ${DB_DUMP_DIR}"
[[ -d "${DB_DUMP_DIR}" ]] && rm -r "${DB_DUMP_DIR}"
docker exec -i {{ postgres_ha_cont_name }} \
pg_dump \
-F directory \
-f "/backup/${DB}" \
-U {{ postgres_ha_admin_user }} \
"${DB}"
done
chmod 750 -R "${BKP_DIR}"