Skip to content

Backup strategy using pg-dump #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
---

postgresql_default_version: 10
postgresql_default_version: 13
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@natefoo any issue with this?

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
Expand Down Expand Up @@ -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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
postgresql_pgdump_cronspec: "00 18 * * 1-7"
postgresql_pgdump_cron_month: "*"
postgresql_pgdump_cron_weekday: "*"
# postgresql_pgdump_cron_day:
postgresql_pgdump_cron_minute: 0
postgresql_pgdump_cron_hour: 18

postgresql_pgdump_filespec: 'full.daily.`/bin/date +"\%u"`.sql'

Copy link
Member

@hexylena hexylena Nov 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# postgresql_pgdump_dir:

I think this needs to be documented somewhere, maybe in the readme?

3 changes: 3 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
Expand Down
18 changes: 18 additions & 0 deletions tasks/pgdump.yml
Original file line number Diff line number Diff line change
@@ -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
Comment on lines +12 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should do this via the cron module to be a bit more standard

Suggested change
template:
src: local-pgdump.crontab
dest: /etc/cron.d/local-pgdump
owner: root
group: root
mode: 0644
- name: Creates a cron file under /etc/cron.d
ansible.builtin.cron:
name: Regular pgdump
month: "{{ postgresql_pgdump_cron_month | default(omit) }}"
weekday: "{{ postgresql_pgdump_cron_weekday | default(omit) }}"
day: "{{ postgresql_pgdump_cron_day | default(omit) }}"
minute: "{{ postgresql_pgdump_cron_minute | default(omit) }}"
hour: "{{ postgresql_pgdump_cron_hour | default(omit) }}"
user: "{{ postgresql_user_name }}"
job: "{{ postgresql_inst_dir_default }}/bin/pg_dumpall -c -f {{ postgresql_pgdump_dir }}/{{ postgresql_pgdump_filespec }}"
cron_file: ansible_local-pgdump


21 changes: 20 additions & 1 deletion tasks/redhat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: >-
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions templates/local-pgdump.crontab
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ postgresql_pgdump_cronspec }} {{ postgresql_user_name }} {{ postgresql_inst_dir_default }}/bin/pg_dumpall -c -f {{ postgresql_pgdump_dir }}/{{ postgresql_pgdump_filespec }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now unnecessary

1 change: 1 addition & 0 deletions vars/debian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions vars/redhat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}