[PATCH] Back up and restore PostgreSQL databases with Shepherd

  • Open
  • quality assurance status badge
Details
2 participants
  • Ludovic Courtès
  • Marius Bakke
Owner
unassigned
Submitted by
Marius Bakke
Severity
normal
M
M
Marius Bakke wrote on 17 Jun 2022 23:14
(address . guix-patches@gnu.org)
87zgibuh5w.fsf@gnu.org
Hello Guix!

The attached patch adds backup and restore mechanisms to the PostgreSQL
Shepherd service. It looks like this (here with a db named 'mreg'):

$ sudo herd backup postgres mreg
$ sudo -u postgres psql -c 'drop database mreg' # whoops ...
DROP DATABASE
$ sudo herd list-backups postgres mreg
mreg@2022-06-16_21-55-07
mreg@2022-06-16_22-48-59
$ sudo herd restore postgres mreg@2022-06-16_22-48-59
$ sudo -u postgres psql mreg
mreg=#

Pretty cool, no? :-)

The restore command is "smart": if the database already exists, it
restores in a single transaction; otherwise, it will be created from
scratch (these scenarios require mutually exclusive options to
'pg_restore').

With this patch you can 'herd backup' each database, stop postgres,
_delete_ /var/lib/postgresql/data, reconfigure with a newer version, and
'herd restore' them again -- but you'll lose any role passwords (and
roles not declared by postgresql-role-service-type).

Not sure what to about roles, maybe a backup-roles command?

There is no Scheme API yet, but it would be nice to define per-database
settings (i.e. --jobs or --format) in the configuration. And also a
scheduled backup service. These tasks are up for grabs. :-)

The quest here is to provide a smooth upgrade path for end users (and
eventually bump the old 'postgresql-10' service default).

Feedback and/or testing welcome!
-----BEGIN PGP SIGNATURE-----

iIUEARYKAC0WIQRNTknu3zbaMQ2ddzTocYulkRQQdwUCYqzumw8cbWFyaXVzQGdu
dS5vcmcACgkQ6HGLpZEUEHdqGQD/RhKGfvcFZYr4Eo8ico0Y2xt18IUymNgfGq7U
oJJsZJgBAM4/gLnUqpPq/NDoTSjmMAE50Ss6rbefVrtGXFX0DMkM
=qMDU
-----END PGP SIGNATURE-----

L
L
Ludovic Courtès wrote on 22 Jun 2022 22:46
(name . Marius Bakke)(address . marius@gnu.org)(address . 56045@debbugs.gnu.org)
87v8ss1l5f.fsf@gnu.org
Hello!

Marius Bakke <marius@gnu.org> skribis:

Toggle quote (15 lines)
> The attached patch adds backup and restore mechanisms to the PostgreSQL
> Shepherd service. It looks like this (here with a db named 'mreg'):
>
> $ sudo herd backup postgres mreg
> $ sudo -u postgres psql -c 'drop database mreg' # whoops ...
> DROP DATABASE
> $ sudo herd list-backups postgres mreg
> mreg@2022-06-16_21-55-07
> mreg@2022-06-16_22-48-59
> $ sudo herd restore postgres mreg@2022-06-16_22-48-59
> $ sudo -u postgres psql mreg
> mreg=#
>
> Pretty cool, no? :-)

Indeed! :-)

Toggle quote (7 lines)
> With this patch you can 'herd backup' each database, stop postgres,
> _delete_ /var/lib/postgresql/data, reconfigure with a newer version, and
> 'herd restore' them again -- but you'll lose any role passwords (and
> roles not declared by postgresql-role-service-type).
>
> Not sure what to about roles, maybe a backup-roles command?

No idea, we need input from PG practitioners!

Toggle quote (18 lines)
> From edc8a2e5ae3c89b78fb837d4351f0ddfab8fe474 Mon Sep 17 00:00:00 2001
> From: Marius Bakke <marius@gnu.org>
> Date: Thu, 16 Jun 2022 22:46:01 +0200
> Subject: [PATCH] services: Shepherd can backup and restore PostgreSQL
> databases.
>
> * gnu/services/databases.scm (<postgresql-configuration>)[backup-directory]:
> New field.
> (postgresql-activation): Create it.
> (postgresql-backup-action, postgresql-list-backups-action,
> postgresql-restore-action): New variables.
> (postgresql-shepherd-service)[actions]: Register them.
> * gnu/tests/databases.scm (%postgresql-backup-directory): New variable.
> (run-postgresql-test): Trim unused module imports from existing tests. Add
> "insert test data", "backup database", "list backups", "drop database",
> "restore database", "update test data", "restore again", and "verify restore"
> tests.

Not being a database person, I’ll comment on the code:

Toggle quote (6 lines)
> (match-lambda
> (($ <postgresql-configuration> postgresql port locale config-file
> - log-directory data-directory
> + log-directory data-directory backup-directory
> extension-packages)

Time to use ‘match-record’!

Toggle quote (2 lines)
> +(define (postgresql-backup-action postgresql backup-directory)

Please add a docstring (and on other top-level procedures).

Toggle quote (6 lines)
> + (procedure
> + #~(lambda* (pid #:optional database #:rest rest)
> + (use-modules (guix build utils)
> + (ice-9 match)
> + (srfi srfi-19))

Non-top-level ‘use-modules’ should be avoided; it’s not really supposed
to work. If you have these three modules in the ‘modules’ field of the
parent <shepherd-service> record, that’s enough (I know, it’s not pretty).

Toggle quote (19 lines)
> + ;; Fork so we can drop privileges.
> + (match (primitive-fork)
> + (0
> + ;; Exit with a non-zero status code if an exception is thrown.
> + (dynamic-wind
> + (const #t)
> + (lambda ()
> + (setgid (passwd:gid user))
> + (setuid (passwd:uid user))
> + (umask #o027)
> + (format (current-output-port)
> + "postgres: creating backup ~a.~%"
> + (basename file-name))
> + (mkdir-p (dirname file-name))
> + (let* ((result (apply system* pg_dump database
> + "-f" file-name
> + options))
> + (exit-value (status:exit-val result)))

Would it work to use ‘fork+exec-command’ to do all this? It’d be great
if we could avoid the boilerplate.

Toggle quote (2 lines)
> +(define (postgresql-list-backups-action backup-directory)

Docstring. :-)

Toggle quote (22 lines)
> + (match (primitive-fork)
> + (0
> + (dynamic-wind
> + (const #t)
> + (lambda ()
> + (setgid (passwd:gid user))
> + (setuid (passwd:uid user))
> + (let* ((backup-file (string-append #$backup-directory
> + "/" file))
> + (database (match (string-split file #\@)
> + ((name date) name)))
> + (create? (not (database-exists? database)))
> + (options (list "--clean" "--if-exists"
> + (if create?
> + "--create"
> + "--single-transaction"))))
> + (format (current-output-port)
> + "postgres: restoring ~a.~%" file)
> + (let* ((result (apply system* pg_restore backup-file
> + "-d" (if create? "postgres" database)
> + options))

Same here: ‘fork+exec-command’?

Overall I find it nice and convenient, but I wonder how far we should go
with our services. After all, it’s just one way to make backups, there
are probably other ways, so should we have this particular method
hardwired?

Thanks,
Ludo’.
L
L
Ludovic Courtès wrote on 4 Aug 2022 11:10
control message for bug #56045
(address . control@debbugs.gnu.org)
8735ecpe5b.fsf@gnu.org
tags 56045 + moreinfo
quit
?