From 8a23cbc153a0532b5bdb76636742cddfbc8e5654 Mon Sep 17 00:00:00 2001 From: Andrea Dell'Amico Date: Mon, 11 Jan 2016 17:43:50 +0100 Subject: [PATCH] library/roles/postgresql: Give the possibility to disable the postgresql backups. Create the list of dbs that need a backup automatically, if one is not provided. --- postgresql/defaults/main.yml | 6 ++++-- postgresql/files/postgresql-backup.cron | 21 +++++++++++++-------- postgresql/templates/pg_backup-default.j2 | 9 +++++++++ 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/postgresql/defaults/main.yml b/postgresql/defaults/main.yml index 59e9204..1e67d00 100644 --- a/postgresql/defaults/main.yml +++ b/postgresql/defaults/main.yml @@ -45,18 +45,20 @@ psql_db_user: db_user psql_db_pwd: "We cannot save the password into the repository. Use another variable and change pgpass.j2 accordingly. Encrypt the file that contains the variable with ansible-vault" - postgres_install_gis_extensions: False postgres_gis_version: 2.1 postgres_gis_pkgs: - postgresql-'{{ psql_version }}'-postgis-'{{ postgres_gis_version }}' + +pg_backup_enabled: True pg_backup_logdir: /var/log/postgresql pg_backup_bin: /usr/local/sbin/postgresql-backup pg_backup_pgdump_bin: /usr/bin/pg_dump pg_backup_retain_copies: 15 pg_backup_build_db_list: "no" -pg_backup_db_list: '{{ psql_db_name}}' +# Dynamically created from psql_db_data if pg_backup_db_list is not set +#pg_backup_db_list: '{{ psql_db_name}}' pg_backup_destdir: /var/lib/pgsql/backups pg_backup_logdir: /var/log/postgresql pg_backup_logfile: '{{ pg_backup_logdir }}/postgresql-backup.log' diff --git a/postgresql/files/postgresql-backup.cron b/postgresql/files/postgresql-backup.cron index 3acf1c0..fb55084 100755 --- a/postgresql/files/postgresql-backup.cron +++ b/postgresql/files/postgresql-backup.cron @@ -3,6 +3,7 @@ if [ -f /etc/default/pg_backup ] ; then . /etc/default/pg_backup else + PG_BACKUP_ENABLED=True PG_SERVICE=postgresql USE_NAGIOS=no LOG_DIR=/var/log/postgresql @@ -14,16 +15,20 @@ export PATH="/sbin:/usr/sbin:/usr/local/sbin:$PATH" PG_SVC=$( service $PG_SERVICE status >/dev/null ) PG_RUNNING=$? -if [ ! -d $LOG_DIR ] ; then - mkdir -p $LOG_DIR -fi - -if [ "$PG_RUNNING" -ne "0" -a "$PG_RUNNING" -ne "3" ] ; then - echo "postgresql not running" > $LOG_FILE - exit 1 +if [ '$PG_BACKUP_ENABLED' == 'True' ] ; then + if [ "$PG_RUNNING" -ne "0" -a "$PG_RUNNING" -ne "3" ] ; then + echo "The postgresql service is not running" > $LOG_FILE + exit 1 + else + if [ ! -d $LOG_DIR ] ; then + mkdir -p $LOG_DIR + fi + $PG_BCK_BIN > $LOG_FILE 2>&1 + fi else - $PG_BCK_BIN > $LOG_FILE 2>&1 + echo "Postgresql backups administratively disabled" > $LOG_FILE fi + if [ "${USE_NAGIOS}" == "yes" ] ; then N_LOGDIR=/var/log/nagios-checks if [ ! -d $N_LOGDIR ] ; then diff --git a/postgresql/templates/pg_backup-default.j2 b/postgresql/templates/pg_backup-default.j2 index c9264b8..a85ca6e 100644 --- a/postgresql/templates/pg_backup-default.j2 +++ b/postgresql/templates/pg_backup-default.j2 @@ -1,13 +1,22 @@ PG_SERVICE=postgresql +PG_BACKUP_ENABLED={{ pg_backup_enabled }} PG_VERSION={{ psql_version }} PG_DUMP_BIN={{ pg_backup_pgdump_bin }} PG_BCK_BIN={{ pg_backup_bin }} +{% if pg_backup_enabled }} USE_NAGIOS={{ pg_backup_use_nagios }} +{% else %} +USE_NAGIOS=no +{% endif %} LOG_DIR={{ pg_backup_logdir }} LOG_FILE={{ pg_backup_logfile}} N_DAYS_TO_SPARE={{ pg_backup_retain_copies }} BUILD_DBLIST={{ pg_backup_build_db_list }} +{% if pg_backup_db_list is defined %} DB_LIST="{{ pg_backup_db_list }}" +{% else %} +DB_LIST="{% for db in psql_db_data %}{{ db.name }} {% endfor %}" +{% endif %} PG_USE_AUTH={{ pg_backup_use_auth }} PG_PASS_FILE={{ pg_backup_pass_file }} BACKUPDIR={{ pg_backup_destdir }}