-
Notifications
You must be signed in to change notification settings - Fork 0
postgres: add support for restoring backups #59
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
- 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 }}" | ||
# pg_tables is a system view that is implicitly created in every | ||
# database. The information in is local to that particular database. | ||
query: "SELECT * FROM pg_catalog.pg_tables WHERE tableowner = %s" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if the user we have owns multiple databases? Is there any way to check if there are any tables in the database instead of checking owned tables number across the server? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, it's grand: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... and we connect to the database above. |
||
positional_args: | ||
- "{{ item.username }}" | ||
register: existing_tables | ||
|
||
- name: Copy and restore a database backup (only if the database is empty) | ||
when: existing_tables.rowcount == 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this to prevent restoration when the database is not empty? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct! Let me add a comment. |
||
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" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dark magic. 🪄 Apparently this is a shell builtin, and in
fish
it's different.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah... I was just struggling to find a different way how to pass that many variables to ansible w/o creating a temporary YAML file :( We could do that instead to make this portable across different shells.