1
0
Fork 0
mirror of https://github.com/roles-ansible/role-postgresql-backup.git synced 2024-08-16 10:19:49 +02:00
role-postgresql-backup/templates/postgresql-backup-script.sh

55 lines
1.2 KiB
Bash
Raw Normal View History

2020-07-08 15:46:00 +02:00
#!/usr/bin/env bash
2020-07-08 17:22:41 +02:00
# {{ ansible_managed }}
2020-07-08 15:46:00 +02:00
set -o pipefail
BACKUP_DIR_BASE="{{ postgresql_backup.backup_dir | default( '/var/backup/postgresql/' ) }}"
DATE_FORMAT="{{ postgresql_backup.date_format | default( '%Y-%m-%d_%H-%M' ) }}"
create_backup_dir() {
local backup_dir="${BACKUP_DIR_BASE%/}/$(date "+$DATE_FORMAT")"
mkdir -p "$backup_dir"
echo "$backup_dir"
}
backup_databases() {
2020-07-08 17:22:41 +02:00
{% for db in postgresql_backup.databases %}
2020-07-08 18:07:19 +02:00
{% if db.password %}
export PGPASSWORD="{{ db.password }}"
2020-07-08 17:56:15 +02:00
{% endif %}
2020-07-08 17:46:14 +02:00
if (umask 077 && pg_dump -F c -h "{{ db.host | default( 'localhost' ) }}" -U "{{ db.user | default( 'postgres' ) }}" -p "{{ db.port | default( '5432' ) }}" "{{ db.name }}" -f "{{ db.name }}.in_progress.psql"); then
2020-07-08 17:22:41 +02:00
mv "{{ db.name }}.in_progress.psql" "{{ db.name }}.psql"
2020-07-09 10:45:12 +02:00
echo "backup of {{ db.name }} successful"
2020-07-08 15:46:00 +02:00
else
2020-07-09 10:45:12 +02:00
echo "failed to export {{ db.name }}"
2020-07-08 15:46:00 +02:00
return 1
fi;
2020-07-08 17:22:41 +02:00
{% endfor %}
2020-07-08 15:46:00 +02:00
return 0
}
main() {
backup_dir="$(create_backup_dir)"
echo "Created backup directory \"${backup_dir}\"."
pushd . >/dev/null
cd "$backup_dir"
echo "Starting databases backup."
if backup_databases; then
echo "Databases backup is done."
else
echo "Databases backup failed. Exiting."
exit 1;
fi;
popd >/dev/null
}
main