# {{ ansible_managed }}

#=====================================================================
# Set the following variables to your system needs
# (Detailed instructions below variables)
#=====================================================================

# By default, on Debian systems, only 'postgres' user
# is allowed to access PostgreSQL databases without password.
# In order to dump databases we need to run pg_dump/psql
# commands as 'postgres' with su.
#
# The following setting has been added to workraound this issue.
# (if it is set to empty, 'su' usage will be disabled)
SU_USERNAME="{{ item.user | d(postgresql_server__user) }}"

# Username to access the PostgreSQL server e.g. dbuser
USERNAME="{{ item.user | d(postgresql_server__user) }}"

# Host name (or IP address) of PostgreSQL server e.g localhost
DBHOST="localhost"

# List of DBNAMES for Daily/Weekly Backup e.g. "DB1 DB2 DB3".
DBNAMES="all"

# Backup directory location e.g /backups
BACKUPDIR="{{ item.auto_backup_dir | d(postgresql_server__auto_backup_dir) }}/$DBCLUSTER"

# Mail setup
# What would you like to be mailed to you?
# - log   : send only log file
# - files : send log file and sql files as attachments (see docs)
# - stdout : will simply output the log to the screen if run manually.
# - quiet : Only send logs if an error occurs to the MAILADDR.
MAILCONTENT="{{ item.auto_backup_mail | d(postgresql_server__auto_backup_mail) }}"

# Set the maximum allowed email size in k. (4000 = approx 5MB email [see docs])
MAXATTSIZE="{{ item.auto_backup_mail_size | d(postgresql_server__auto_backup_mail_size) }}"

# Email Address to send mail to? (user@domain.com)
MAILADDR="{{ item.auto_backup_mail_to | d(postgresql_server__auto_backup_mail_to) }}"

# ============================================================
# === ADVANCED OPTIONS ( Read the doc's below for details )===
#=============================================================

# List of DBBNAMES for Monthly Backups.
MDBNAMES="template1 all"

# List of DBNAMES to EXCLUDE if DBNAMES are set to all (must be in "" quotes).
DBEXCLUDE=""

# Include CREATE DATABASE in backup?
CREATE_DATABASE={{ "yes" if ((item.auto_backup_create_database | d(postgresql_server__auto_backup_create_database)) | bool) else "no" }}

# Separate backup directory and file for each DB? (yes or no)
SEPDIR={{ "yes" if ((item.auto_backup_isolate_databases | d(postgresql_server__auto_backup_isolate_databases)) | bool) else "no" }}

# Which day do you want weekly backups? (1 to 7 where 1 is Monday)
DOWEEKLY={{ item.auto_backup_weekly | d(postgresql_server__auto_backup_weekly) }}

# Which day do you want monthly backups?
DOMONTHLY={{ item.auto_backup_monthly | d(postgresql_server__auto_backup_monthly) }}

{% set auto_backup_compression = item.auto_backup_compression
                                 | d(postgresql_server__auto_backup_compression) %}
# Choose Compression type. (gzip or bzip2)
COMP="{{ auto_backup_compression }}"

# Compress backup on the fly with pg_dump (instead of afterwards with gzip or bzip2)
# set compression level from 0 to 9 (0 means no compression)
COMMCOMP={{ item.auto_backup_compression_level
            | default((auto_backup_compression == "pg_dump") | ternary(6,0)) }}

# Additionally keep a copy of the most recent backup in a seperate directory.
LATEST=no

# Backup files extension
EXT="sql{{ (auto_backup_compression == "pg_dump") | ternary(".gz", "") }}"

# Encyrption settings
# (inspired by http://blog.altudov.com/2010/09/27/using-openssl-for-asymmetric-encryption-of-backups/)
#
# Once the backup done, each SQL dump will be encrypted and the original file
# will be deleted (if encryption was successful).
# It is recommended to backup into a staging directory, and then use the
# POSTBACKUP script to sync the encrypted files to the desired location.
#
# Encryption uses private/public keys. You can generate the key pairs like the following:
# openssl req -x509 -nodes -days 100000 -newkey rsa:2048 -keyout backup.key -out backup.crt -subj '/'
#
# Decryption:
# openssl smime -decrypt -in backup.sql.gz.enc -binary -inform DEM -inkey backup.key -out backup.sql.gz

# Enable encryption
ENCRYPTION={{ "yes" if ((item.auto_backup_encryption | d(postgresql_server__auto_backup_encryption)) | bool) else "no" }}

# Encryption public key
ENCRYPTION_PUBLIC_KEY="{{ item.auto_backup_encryption_key | d(postgresql_server__auto_backup_encryption_key) }}"

# Encryption Cipher (see enc manpage)
ENCRYPTION_CIPHER="{{ item.auto_backup_encryption_cipher | d(postgresql_server__auto_backup_encryption_cipher) }}"

# Suffix for encyrpted files
ENCRYPTION_SUFFIX="{{ item.auto_backup_encryption_suffix | d(postgresql_server__auto_backup_encryption_suffix) }}"

# Command to run before backups
PREBACKUP="{{ item.auto_backup_pre_script | d(postgresql_server__auto_backup_pre_script) }}"

# Command to run after backups
POSTBACKUP="{{ item.auto_backup_post_script | d(postgresql_server__auto_backup_post_script) }}"

