Skip to content

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

Merged
merged 1 commit into from
Nov 21, 2024
Merged

Conversation

malor
Copy link
Member

@malor malor commented Oct 3, 2024

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.

@malor malor added the WIP label Oct 3, 2024
@malor malor force-pushed the restore-backups branch 7 times, most recently from c6588f6 to 9e0efb0 Compare October 6, 2024 11:52
@malor malor changed the title Restore backups postgres: add support for restoring backups Oct 6, 2024
@malor malor requested a review from ikalnytskyi October 6, 2024 11:55
@malor malor removed the WIP label Oct 6, 2024
@@ -96,8 +104,28 @@ jobs:

- name: Run the playbook
run: |
read -r -d '' extra_vars << 'EOF' || true
Copy link
Member

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.

Copy link
Member Author

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.

register: existing_tables

- name: Copy and restore a database backup
when: existing_tables.rowcount == 0
Copy link
Member

Choose a reason for hiding this comment

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

Is this to prevent restoration when the database is not empty?

Copy link
Member Author

Choose a reason for hiding this comment

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

Correct! Let me add a comment.

- 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"
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, it's grand: pg_catalog.pg_tables is a magical system view that exists in all databases, and all the information is local to a particular database.

Copy link
Member Author

Choose a reason for hiding this comment

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

... and we connect to the database above.

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.
@malor malor merged commit 106e24b into master Nov 21, 2024
4 checks passed
@malor malor deleted the restore-backups branch November 21, 2024 07:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants