Database dump hooks for PostgreSQL #225

Closed
opened 2019-10-19 21:47:45 +00:00 by witten · 6 comments
Owner

What I'm trying to do and why

If you want to run borgmatic on a system with a database daemon, best practices dictate that you dump the database to file and back that up instead of trying to backup the running database. That way, you get a consistent snapshot.

Today, you can already do that with borgmatic via hand-written before_backup/after_backup hooks. But what if it could be even easier / more built-in, such that you didn't have to write custom hooks with dump commands?

See #228 for a variant of this ticket for MariaDB.

Implementation ideas

Consider a feature that might be configured as follows:

hooks:
     postgresql:
         databases:
             - orders
             - invoices
         format: tar
         dump_path: /var/lib/backups

(This is in part inspired by backupninja's database support.)

The idea is that with this high-level configuration in place, borgmatic would automatically pg_dump your databases prior to each backup, and rm the dump afterwards. And then you could include the dump directory /var/lib/backups in your source directories (or maybe that'd get injected automatically), and all your database dumps would get backed up.

For a real killer feature, this new feature could also trigger during borgmatic restore. Specifically, it would either optionally or automatically restore your databases from dumps.. not just your files! So if you're restoring on a completely fresh system, you could get up and running relatively quickly—including your databases—without no manual work.

#### What I'm trying to do and why If you want to run borgmatic on a system with a database daemon, best practices dictate that you dump the database to file and back *that* up instead of trying to backup the running database. That way, you get a consistent snapshot. Today, you can already do that with borgmatic via hand-written `before_backup`/`after_backup` [hooks](https://torsion.org/borgmatic/docs/how-to/add-preparation-and-cleanup-steps-to-backups/). But what if it could be even easier / more built-in, such that you didn't have to write custom hooks with dump commands? See #228 for a variant of this ticket for MariaDB. #### Implementation ideas Consider a feature that might be configured as follows: ```yaml hooks: postgresql: databases: - orders - invoices format: tar dump_path: /var/lib/backups ``` (This is in part inspired by [backupninja](https://www.0xacab.org/riseuplabs/backupninja)'s [database support](https://www.0xacab.org/riseuplabs/backupninja/blob/master/examples/example.pgsql).) The idea is that with this high-level configuration in place, borgmatic would automatically `pg_dump` your databases prior to each backup, and `rm` the dump afterwards. And then you could include the dump directory `/var/lib/backups` in your source directories (or maybe that'd get injected automatically), and all your database dumps would get backed up. For a real killer feature, this new feature could also trigger during `borgmatic restore`. Specifically, it would either optionally or automatically restore your databases from dumps.. not just your files! So if you're restoring on a completely fresh system, you could get up and running relatively quickly—including your databases—without no manual work.
Author
Owner

It occurs to me that this configuration would probably need optional support for connecting to the database with a given username, password, and database host. It's not always the case that borgmatic would have direct access to pg_dump as the postgres superuser.

For instance, consider the case where borgmatic is running in a Docker container separate from the database container. In that scenario, in order to initiate a database dump, borgmatic would have to connect to the Docker hostname corresponding to the database container, and use the (probably non-superuser) database credentials to connect.

If this is part of the requirements, then that suggests the sample config above is insufficient, as the user may need to specify credentials per database.

It occurs to me that this configuration would probably need optional support for connecting to the database with a given username, password, and database host. It's not always the case that borgmatic would have direct access to `pg_dump` as the `postgres` superuser. For instance, consider the case where borgmatic is running in a Docker container separate from the database container. In that scenario, in order to initiate a database dump, borgmatic would have to connect to the Docker hostname corresponding to the database container, and use the (probably non-superuser) database credentials to connect. If this is part of the requirements, then that suggests the sample config above is insufficient, as the user may need to specify credentials *per database*.
Author
Owner

Take two on a proposed config to deal with some of the above requirements:

hooks:
     postgresql:
         - database: orders
           hostname: dbhost
           username: dbuser
           password: trustsome1
           format: tar
           dump_path: /var/lib/backups

         - database: invoices
           format: tar
Take two on a proposed config to deal with some of the above requirements: ```yaml hooks: postgresql: - database: orders hostname: dbhost username: dbuser password: trustsome1 format: tar dump_path: /var/lib/backups - database: invoices format: tar ```
Contributor

Take two on a proposed config

Looking very sharp indeed!

So, we could intially aim for support for:

  • postgresql
  • mariadb

Continuing on this line of thought, the idea of "ninjahelper" (see https://0xacab.org/riseuplabs/backupninja#ninjahelper) is also quite appealing and might be useful after this functionality goes in. Ncurses magic!

> Take two on a proposed config Looking very sharp indeed! So, we could intially aim for support for: * postgresql * mariadb Continuing on this line of thought, the idea of "ninjahelper" (see https://0xacab.org/riseuplabs/backupninja#ninjahelper) is also quite appealing and might be useful after this functionality goes in. Ncurses magic!
Author
Owner

PostgreSQL and MariaDB make sense as a place to start. I can look into PostgreSQL first if you want to take a stab at MariaDB either during or after (#228). I'll convert this ticket into PostgreSQL only to make it more scoped.

Interesting that you mention ninjahelper. I had been thinking vaguely of similar functionality in generate-borgmatic-config. There are also less ambitious variants than a full ncurses wizard:

  • A prompt-based wizard, or:
  • No wizard at all, but just some nice command-line flags that optionally alter the generated config based on your needs.

Might want to open a separate ticket for this general class of ideas?

PostgreSQL and MariaDB make sense as a place to start. I can look into PostgreSQL first if you want to take a stab at MariaDB either during or after (#228). I'll convert this ticket into PostgreSQL only to make it more scoped. Interesting that you mention ninjahelper. I had been thinking vaguely of similar functionality in `generate-borgmatic-config`. There are also less ambitious variants than a full ncurses wizard: * A prompt-based wizard, or: * No wizard at all, but just some nice command-line flags that optionally alter the generated config based on your needs. Might want to open a separate ticket for this general class of ideas?
witten changed title from Database dump/restore hooks to Database dump/restore hooks for PostgreSQL 2019-10-21 00:03:55 +00:00
witten changed title from Database dump/restore hooks for PostgreSQL to Database dump hooks for PostgreSQL 2019-10-22 23:19:04 +00:00
Author
Owner

Split off the restore portion to #229 to keep this ticket somewhat scoped down.

Split off the restore portion to #229 to keep this ticket somewhat scoped down.
Author
Owner

Just released in 1.4.0! Docs here: https://torsion.org/borgmatic/docs/how-to/backup-your-databases/

Feedback welcome as always. And we can tweak the config format if necessary.

Just released in 1.4.0! Docs here: https://torsion.org/borgmatic/docs/how-to/backup-your-databases/ Feedback welcome as always. And we can tweak the config format if necessary.
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: borgmatic-collective/borgmatic#225
No description provided.