diff --git a/README.md b/README.md index 123720c..140f83a 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Role Variables - `postgresql_user_name`: System username to be used for PostgreSQL (default: `postgres`). - `postgresql_version`: PostgreSQL version to install. On Debian-based platforms, the default is whatever version is - pointed to by the `postgresql` metapackage). On RedHat-based platforms, the default is `10`. + pointed to by the `postgresql` metapackage). On RedHat-based platforms, the default is `13`. - `postgresql_flavor`: On Debian-based platforms, this specifies whether you want to use PostgreSQL packages from pgdg or the distribution's apt repositories. Possible values: `apt`, `pgdg` (default: `apt`). @@ -99,6 +99,16 @@ Role Variables - `postgresql_backup_post_command`: Arbitrary command to run after successful completion of a scheduled backup. +### Backups via pg_dumpall(1) ### + +This is an alternative backup strategy that creates full dumps of the whole database cluster using pg_dumpall(1). + +- `postgresql_pgdump_dir`: If set, enables pg_dumpall(1)-backups. Set this to the directory where the dumps should be written. There is no distinction between local and archive directories; it is assumed that this directory is "safe enough" and has enough storage space available. + +- `postgresql_pgdump_cronspec`: This specifies the first 5 fields of the crontab-entry that will be govern the pgdump-schedule. Defaults to "00 18 * * 1-7", which means "start the dump each day at 18:00". + +- `postgresql_pgdump_filespec`: Controls the names of the dump files created. The default creates a file for each day of the week that will be overwritten after seven days. + Additional options pertaining to backups can be found in the [defaults file](defaults/main.yml). Dependencies diff --git a/defaults/main.yml b/defaults/main.yml index a6e5b04..f900fe6 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,6 +1,9 @@ --- -postgresql_default_version: 10 +postgresql_default_version: 13 +postgresql_backup_active_dir: "{{ postgresql_backup_local_dir }}/active" +postgresql_backup_mail_recipient: postgres +postgresql_backup_rotate: true postgresql_user_name: postgres # Point-In-Time Recovery (PITR) backup options @@ -30,3 +33,7 @@ postgresql_backup_command: >- --keep {{ postgresql_backup_keep | quote }} {{ '--pg-bin-dir ' ~ __postgresql_pgdg_bin_dir if ansible_os_family == 'RedHat' else '' }} --backup --clean-archive {{ postgresql_backup_dir | quote }} + +postgresql_pgdump_cronspec: "00 18 * * 1-7" +postgresql_pgdump_filespec: 'full.daily.`/bin/date +"\%u"`.sql' + diff --git a/tasks/main.yml b/tasks/main.yml index 757dd38..75035af 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -92,6 +92,9 @@ - include_tasks: backup.yml when: postgresql_backup_dir is defined +- include_tasks: pgdump.yml + when: postgresql_pgdump_dir is defined + - name: Ensure PostgreSQL is running service: name: "{{ postgresql_service_name }}" diff --git a/tasks/pgdump.yml b/tasks/pgdump.yml new file mode 100644 index 0000000..0a27a6e --- /dev/null +++ b/tasks/pgdump.yml @@ -0,0 +1,18 @@ +--- + +- name: Create backup directory for pgdump + file: + owner: postgres + group: postgres + mode: 0750 + state: directory + path: "{{ postgresql_pgdump_dir }}" + +- name: Schedule pgdump backups via cron(8) + template: + src: local-pgdump.crontab + dest: /etc/cron.d/local-pgdump + owner: root + group: root + mode: 0644 + diff --git a/tasks/redhat.yml b/tasks/redhat.yml index 00e7ed4..bd0c9d9 100644 --- a/tasks/redhat.yml +++ b/tasks/redhat.yml @@ -13,6 +13,16 @@ register: __postgresql_repo_pkg_installed_result ignore_errors: true +- name: Install repository key (PostgreSQL v10 and below) + rpm_key: + key: https://download.postgresql.org/pub/repos/yum/keys/RPM-GPG-KEY-PGDG + state: present + +- name: Install repository key (PostgreSQL v11 and above) + rpm_key: + key: https://download.postgresql.org/pub/repos/yum/keys/PGDG-RPM-GPG-KEY-RHEL + state: present + - name: Install pgdg repository package (RedHat) yum: name: >- @@ -42,9 +52,18 @@ # # here but alas there is no `startswith` test # loop: "{{ __postgresql_yum_repolist_result.results }}" -- name: Install PostgreSQL (RedHat) + +- name: Install PostgreSQL (RedHat < 8) + yum: + name: postgresql{{ __postgresql_version_dotless }}-server + when: ansible_distribution_major_version is version(8, '<') + +# on RH-8 for installing the pgdg-version, the Appstream-repo must be disabled +- name: Install PostgreSQL (RedHat >= 8) yum: name: postgresql{{ __postgresql_version_dotless }}-server + disablerepo: AppStream + when: ansible_distribution_major_version is version(8, '>=') - name: Check for pgdata directory stat: diff --git a/templates/local-pgdump.crontab b/templates/local-pgdump.crontab new file mode 100644 index 0000000..125a177 --- /dev/null +++ b/templates/local-pgdump.crontab @@ -0,0 +1 @@ +{{ postgresql_pgdump_cronspec }} {{ postgresql_user_name }} {{ postgresql_inst_dir_default }}/bin/pg_dumpall -c -f {{ postgresql_pgdump_dir }}/{{ postgresql_pgdump_filespec }} diff --git a/vars/debian.yml b/vars/debian.yml index 492965b..bcd2ac6 100644 --- a/vars/debian.yml +++ b/vars/debian.yml @@ -2,4 +2,5 @@ postgresql_pgdata_default: /var/lib/postgresql/{{ postgresql_version }}/main postgresql_conf_dir_default: /etc/postgresql/{{ postgresql_version }}/main +postgresql_inst_dir_default: /usr/lib/postgresql/{{ postgresql_version }} postgresql_service_name: postgresql diff --git a/vars/redhat.yml b/vars/redhat.yml index 2eb570b..7f27874 100644 --- a/vars/redhat.yml +++ b/vars/redhat.yml @@ -2,4 +2,5 @@ postgresql_pgdata_default: /var/lib/pgsql/{{ postgresql_version }}/data postgresql_conf_dir_default: /var/lib/pgsql/{{ postgresql_version }}/data +postgresql_inst_dir_default: /usr/pgsql-{{ postgresql_version }} postgresql_service_name: postgresql-{{ postgresql_version }}