-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
postgres: add support for restoring backups
Only the "custom" format of pg_restore is supported, and the backup file must be located on the host where the ansible playbook is executed. The intended use cases are: * Migrating to new PostgreSQL/Ubuntu versions in production (the former notoriously requires downtime and either restoring a database backup, or running the migration tool to update the storage format offline). * Testing of database backups that we create today.
- Loading branch information
Showing
5 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
- name: Restore database backups if necessary | ||
become: true | ||
become_user: postgres | ||
block: | ||
- name: Check if we need to restore a database backup | ||
community.postgresql.postgresql_query: | ||
db: "{{ item.database }}" | ||
query: "SELECT * FROM pg_catalog.pg_tables WHERE tableowner = %s" | ||
positional_args: | ||
- "{{ item.username }}" | ||
register: existing_tables | ||
|
||
- name: Copy and restore a database backup | ||
when: existing_tables.rowcount == 0 | ||
block: | ||
- name: Create a temporary backup directory | ||
ansible.builtin.tempfile: | ||
state: directory | ||
suffix: backup | ||
register: backup_tmp_dir | ||
|
||
- name: Copy the database backup | ||
ansible.builtin.copy: | ||
src: "{{ item.backup_restore }}" | ||
dest: "{{ [backup_tmp_dir.path, item.backup_restore | basename] | path_join }}" | ||
mode: 'u=rw,g=r,o=' | ||
|
||
- name: Restore the database backup | ||
community.postgresql.postgresql_db: | ||
name: "{{ item.database }}" | ||
state: "restore" | ||
target: "{{ [backup_tmp_dir.path, item.backup_restore | basename] | path_join }}" | ||
target_opts: "--single-transaction --exit-on-error" |
Binary file not shown.