diff --git a/README.md b/README.md index 86cc81c..5b0afbc 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # role-postgresql-backup Ansible role to automaticcally backup your postgresql database you created with ansible + +WÖRK IN PROGRESS, obviously! diff --git a/files/postgresql-backup-script.sh b/files/postgresql-backup-script.sh new file mode 100644 index 0000000..5484c34 --- /dev/null +++ b/files/postgresql-backup-script.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash +# this file is managed by ansible! + +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' ) }}" +PG_HOSTNAME +PG_USERNAME +PG_PORT +PG_DATABASE + +create_backup_dir() { + local backup_dir="${BACKUP_DIR_BASE%/}/$(date "+$DATE_FORMAT")" + mkdir -p "$backup_dir" + echo "$backup_dir" +} + + +backup_databases() { + local filename="${PG_DATABASE}.psql" + if (umask 077 && pg_dump -F c -h "$PG_HOSTNAME" -U "$PG_USERNAME" -p "$PG_PORT" "$PG_DATABASE" -f "${filename}.in_progress"); then + mv "${filename}.in_progress" "$filename" + else + return 1 + fi; + done <<< "$databases" + 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 diff --git a/vars/main.yml b/vars/main.yml index c928123..2e50eaa 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1,6 +1,6 @@ --- # versionscheck -playbook_version_number: 12 # should be int +playbook_version_number: 13 # should be int playbook_version_path: 'role-postgresql-backup_roles-ansible_github.com.version' # unique string _postgresql_backup: