mirror of
https://github.com/roles-ansible/role-postgresql-backup.git
synced 2024-08-16 10:19:49 +02:00
Rewrite and copy backup script
This commit is contained in:
parent
86da6ff9af
commit
7a57a3ccf5
4 changed files with 25 additions and 10 deletions
|
@ -7,3 +7,10 @@ postgresql_backup:
|
||||||
- postgresql-client
|
- postgresql-client
|
||||||
backup_dir: /var/backup/postgresql
|
backup_dir: /var/backup/postgresql
|
||||||
date_format: '%Y-%m-%d_%H-%M'
|
date_format: '%Y-%m-%d_%H-%M'
|
||||||
|
user: root
|
||||||
|
databases: []
|
||||||
|
# - name: psql_database_name
|
||||||
|
# user: psql_database_user
|
||||||
|
# password: Topf_Secret1
|
||||||
|
# host: localhost # optional
|
||||||
|
# port: 5432 # optional
|
||||||
|
|
|
@ -7,3 +7,12 @@
|
||||||
when: submodules_versioncheck|bool
|
when: submodules_versioncheck|bool
|
||||||
|
|
||||||
- include_tasks: packages.yml
|
- include_tasks: packages.yml
|
||||||
|
|
||||||
|
- name: generate backup script
|
||||||
|
become: true
|
||||||
|
template:
|
||||||
|
src: templates/postgresql-backup-script.sh
|
||||||
|
dest: /etc/postgresql-backup-script.sh
|
||||||
|
owner: "{{ postgresql_backup.user }}"
|
||||||
|
backup: true
|
||||||
|
mode: 0755
|
||||||
|
|
|
@ -1,15 +1,11 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# this file is managed by ansible!
|
# {{ ansible_managed }}
|
||||||
|
|
||||||
set -o pipefail
|
set -o pipefail
|
||||||
|
|
||||||
|
|
||||||
BACKUP_DIR_BASE="{{ postgresql_backup.backup_dir | default( '/var/backup/postgresql/' ) }}"
|
BACKUP_DIR_BASE="{{ postgresql_backup.backup_dir | default( '/var/backup/postgresql/' ) }}"
|
||||||
DATE_FORMAT="{{ postgresql_backup.date_format | default( '%Y-%m-%d_%H-%M' ) }}"
|
DATE_FORMAT="{{ postgresql_backup.date_format | default( '%Y-%m-%d_%H-%M' ) }}"
|
||||||
PG_HOSTNAME
|
|
||||||
PG_USERNAME
|
|
||||||
PG_PORT
|
|
||||||
PG_DATABASE
|
|
||||||
|
|
||||||
create_backup_dir() {
|
create_backup_dir() {
|
||||||
local backup_dir="${BACKUP_DIR_BASE%/}/$(date "+$DATE_FORMAT")"
|
local backup_dir="${BACKUP_DIR_BASE%/}/$(date "+$DATE_FORMAT")"
|
||||||
|
@ -17,15 +13,14 @@ create_backup_dir() {
|
||||||
echo "$backup_dir"
|
echo "$backup_dir"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
backup_databases() {
|
backup_databases() {
|
||||||
local filename="${PG_DATABASE}.psql"
|
{% for db in postgresql_backup.databases %}
|
||||||
if (umask 077 && pg_dump -F c -h "$PG_HOSTNAME" -U "$PG_USERNAME" -p "$PG_PORT" "$PG_DATABASE" -f "${filename}.in_progress"); then
|
if (umask 077 && pg_dump -F c -h "{{ db.host | defaul( 'localhost' ) }}" -U "{{ db.user | default( 'postgres' ) }}" -p "{{ db.port | default( '5432' ) }}" "{{ db.name }}" -f "{{ db.name }}.in_progress.psql"); then
|
||||||
mv "${filename}.in_progress" "$filename"
|
mv "{{ db.name }}.in_progress.psql" "{{ db.name }}.psql"
|
||||||
else
|
else
|
||||||
return 1
|
return 1
|
||||||
fi;
|
fi;
|
||||||
done <<< "$databases"
|
{% endfor %}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,3 +6,7 @@ playbook_version_path: 'role-postgresql-backup_roles-ansible_github.com.version'
|
||||||
_postgresql_backup:
|
_postgresql_backup:
|
||||||
required_packages:
|
required_packages:
|
||||||
- postgresql-client
|
- postgresql-client
|
||||||
|
backup_dir: /var/backup/postgresql
|
||||||
|
date_format: '%Y-%m-%d_%H-%M'
|
||||||
|
user: root
|
||||||
|
databases: []
|
||||||
|
|
Loading…
Reference in a new issue