From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id QHCJG9x/s2IPBgAAbAwnHQ (envelope-from ) for ; Wed, 22 Jun 2022 22:47:24 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id OE6BG9x/s2IIYwAAauVa8A (envelope-from ) for ; Wed, 22 Jun 2022 22:47:24 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 10674E12C for ; Wed, 22 Jun 2022 22:47:23 +0200 (CEST) Received: from localhost ([::1]:42316 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o47Fy-00020Y-GD for larch@yhetil.org; Wed, 22 Jun 2022 16:47:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:48702) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o47Ff-0001tU-LA for guix-patches@gnu.org; Wed, 22 Jun 2022 16:47:06 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:42825) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1o47Fe-00041r-C0 for guix-patches@gnu.org; Wed, 22 Jun 2022 16:47:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1o47Fe-0008Ve-Ag for guix-patches@gnu.org; Wed, 22 Jun 2022 16:47:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#56045] [PATCH] Back up and restore PostgreSQL databases with Shepherd Resent-From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Wed, 22 Jun 2022 20:47:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 56045 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: Marius Bakke Cc: 56045@debbugs.gnu.org Received: via spool by 56045-submit@debbugs.gnu.org id=B56045.165593080632686 (code B ref 56045); Wed, 22 Jun 2022 20:47:02 +0000 Received: (at 56045) by debbugs.gnu.org; 22 Jun 2022 20:46:46 +0000 Received: from localhost ([127.0.0.1]:36722 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1o47FO-0008V6-6z for submit@debbugs.gnu.org; Wed, 22 Jun 2022 16:46:46 -0400 Received: from eggs.gnu.org ([209.51.188.92]:35434) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1o47FM-0008Uv-Vc for 56045@debbugs.gnu.org; Wed, 22 Jun 2022 16:46:46 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]:52088) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o47FH-0003yY-Jn for 56045@debbugs.gnu.org; Wed, 22 Jun 2022 16:46:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:In-Reply-To:Date:References:Subject:To: From; bh=Srl7FLUDCSbmI+hXLg2wC+gEuZjZhZi9YPX4NLJsFqY=; b=eUHAkEu/zGZdWTL3YNpe zd+BV3pPlqwm09RqfTvn8o3c5yMhzgbjcxiaMuDjBEL7wXZsfhAGHpH7cJmkpzwpcG0AMAS6rRKkH qDICzR7v+rFJH7Fx/3pE1CN+7KkffgOu2rPuyaEKJKZomYCXHQCtfDcIAUTeEtqEM3FvbqnNdcZyo 9VHWPo+8nfZp6Tp95HC3hD5ewieh/H3vVmFUUMeEYdRbZubGBEUeEWI+unZPiJPv14XLN+LZGDwNX P+07KSEXAIvk8bE2r5uu2uOESrbDXR4hmJcjjwCL4FNGWWkK/0PVwinisuhdW+fhLkRdq48cOfWQe 7zdTH7IzKM9HmQ==; Received: from 91-160-117-201.subs.proxad.net ([91.160.117.201]:53889 helo=ribbon) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o47FG-0004fj-F0; Wed, 22 Jun 2022 16:46:38 -0400 From: Ludovic =?UTF-8?Q?Court=C3=A8s?= References: <87zgibuh5w.fsf@gnu.org> Date: Wed, 22 Jun 2022 22:46:36 +0200 In-Reply-To: <87zgibuh5w.fsf@gnu.org> (Marius Bakke's message of "Fri, 17 Jun 2022 23:14:03 +0200") Message-ID: <87v8ss1l5f.fsf@gnu.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Migadu-Flow: FLOW_IN X-Migadu-To: larch@yhetil.org X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1655930844; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=Srl7FLUDCSbmI+hXLg2wC+gEuZjZhZi9YPX4NLJsFqY=; b=UJfE9PW1r63wZAMkAuNrT2tuvmytRTR1BN0/O6ALPGrqETFUfRpu9N52viGq/5BJjdlSql MfAmD6zeXRHuTs83l2JjkMTXg/zd7cBHaAbTwqw3Cg+M00EuqVL9PglJGAt2FVIJCoaUxi 0GU6TsKbWfytgFlmOgEcD0exTl7e1Z7tvU/tFdVIxxBuv+oFBfuvZVSvc/JXyAQplCm610 0XBsS/x7lsiW+o8BCQvhra/qXJvnOOgCV3X4ie+H2rHDnSgqppetPvbbTaOBjIsb16lnRt HE0RH1ECs4nqcBNWu2YfSrXcPjsO+gHUTXkjTcSaCo8HJ+0f32eco7bKVAr4dA== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1655930844; a=rsa-sha256; cv=none; b=Byyj7BH0ymUJDkRt6tslvK5Qwt/mrEvtdcTf9SKFen2L+H5xP7jBk/Pmkuj71wDN2Bfiwz 7m/ZS40rXouo4rTwiRdNC2me0g6Z0m/+6Y6ut092jo/wSHZlWkHOK3/YdFR8pvufZRn/VW lgebJuIXoNNHUMkksj+mSV7Nc4MaB+Eh6tq6dMxU/LBjZjijarF4ksMR/i0UvdA7WTnz0q /VnhXYjB5CGL1D15ldzOJGm1GOBx+vSmOuYiM1+yptdDpwBFWGgSfVKARNnCelVib9bjoB FbqvWziIJ+jOYBjsH8SlCoaEEd1/Q8XRUd7WdQGa9hEKeVoQdzsbRs5V+NLbmA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gnu.org header.s=fencepost-gnu-org header.b="eUHAkEu/"; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: -3.86 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gnu.org header.s=fencepost-gnu-org header.b="eUHAkEu/"; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: 10674E12C X-Spam-Score: -3.86 X-Migadu-Scanner: scn0.migadu.com X-TUID: j00HVZPKpRBQ Hello! Marius Bakke skribis: > 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=3D# > > Pretty cool, no? :-) Indeed! :-) > 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! > From edc8a2e5ae3c89b78fb837d4351f0ddfab8fe474 Mon Sep 17 00:00:00 2001 > From: Marius Bakke > Date: Thu, 16 Jun 2022 22:46:01 +0200 > Subject: [PATCH] services: Shepherd can backup and restore PostgreSQL > databases. > > * gnu/services/databases.scm ()[backup-director= y]: > 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. A= dd > "insert test data", "backup database", "list backups", "drop database", > "restore database", "update test data", "restore again", and "verify rest= ore" > tests. Not being a database person, I=E2=80=99ll comment on the code: > (match-lambda > (($ postgresql port locale config-file > - log-directory data-directory > + log-directory data-directory backup-d= irectory > extension-packages) Time to use =E2=80=98match-record=E2=80=99! > +(define (postgresql-backup-action postgresql backup-directory) Please add a docstring (and on other top-level procedures). > + (procedure > + #~(lambda* (pid #:optional database #:rest rest) > + (use-modules (guix build utils) > + (ice-9 match) > + (srfi srfi-19)) Non-top-level =E2=80=98use-modules=E2=80=99 should be avoided; it=E2=80=99s= not really supposed to work. If you have these three modules in the =E2=80=98modules=E2=80=99 = field of the parent record, that=E2=80=99s enough (I know, it=E2=80= =99s not pretty). > + ;; 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 =E2=80=98fork+exec-command=E2=80=99 to do all this? I= t=E2=80=99d be great if we could avoid the boilerplate. > +(define (postgresql-list-backups-action backup-directory) Docstring. :-) > + (match (primitive-fork) > + (0 > + (dynamic-wind > + (const #t) > + (lambda () > + (setgid (passwd:gid user)) > + (setuid (passwd:uid user)) > + (let* ((backup-file (string-append #$backup-directo= ry > + "/" 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-f= ile > + "-d" (if create? "postgres"= database) > + options)) Same here: =E2=80=98fork+exec-command=E2=80=99? Overall I find it nice and convenient, but I wonder how far we should go with our services. After all, it=E2=80=99s just one way to make backups, t= here are probably other ways, so should we have this particular method hardwired? Thanks, Ludo=E2=80=99.