From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms13.migadu.com with LMTPS id 4PxIEUw4y2YmOgEAe85BDQ:P1 (envelope-from ) for ; Sun, 25 Aug 2024 13:57:32 +0000 Received: from aspmx1.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2.migadu.com with LMTPS id 4PxIEUw4y2YmOgEAe85BDQ (envelope-from ) for ; Sun, 25 Aug 2024 15:57:32 +0200 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=debbugs.gnu.org header.s=debbugs-gnu-org header.b=KEVsb7gR; dkim=fail ("headers rsa verify failed") header.d=autistici.org header.s=stigmate header.b=ouY7Tyr3; 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" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1724594252; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: 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=obTm9VPEfy4bu0nhnzP6EaZ233KgHgSRRgaVBiUO5MA=; b=SNhU2MBAe7JWQIcA8G/IXlv5fJrUCEzr5z389w04VD4LX0rrG7F+3YvFpMI54sSYxilqEI rz8I15CPqhYREIa1DVmEz2Bc/lIzenTc6+BpegkpP5MqXdXNOQODvg2hRzURVCv31KF6+6 e4wWGGlMEe1otH0FQufnXCGMeETlMsDMOeaTBAPy7LCXceGTG7ejkLxSIXfrag0TX8qo/V ZxDGJGQoYX/d9GtQY1xBdE6QMuL+/gdWUbtTw6Ntm9vAEPkDETQ3BqgJG4l5ykbfsjnarD nPZ1ph2iJEJOIS+J0SZRTfCGlcDJDCbneK4fxgO+mtBVTqmWTDSYj3FqFLfEVw== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1724594252; a=rsa-sha256; cv=none; b=KkaKsaiJ5XdLFIJ4xs4e5MD0nn8c9LfVCrZsp9NkwQckzTA1aNw1JyJS5aEhtilBUECUVx kNJk2Wr26AzxRT7d7a0qbv4UQJkfPUDa0AU3i6alSTVCJ8/n/VU+7oHXKOM55JnA4chYQu 2GNsENYVWWxnvQOCTYNsBNMvn1G3FHbgkk+0foJEua0xvVCnCHkSt5iwaf0akynm4OV/S8 F6JDPVgeb9loe96/IvLfjFE4Wr9M0r71CnV6Z72Sz7QaS8nUsChE8+cwiZW0BrgeYSGZSo 6y9z5AcEcAkT7Y0OMB2tjCFbTobMCmsCUAR+1G1SdT+V32JARYcyQ5NDlA1p4g== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=debbugs.gnu.org header.s=debbugs-gnu-org header.b=KEVsb7gR; dkim=fail ("headers rsa verify failed") header.d=autistici.org header.s=stigmate header.b=ouY7Tyr3; 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" 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 0B10F74F53 for ; Sun, 25 Aug 2024 15:57:32 +0200 (CEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1siDk6-00009o-7s; Sun, 25 Aug 2024 09:57:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1siDk3-00008u-II for guix-patches@gnu.org; Sun, 25 Aug 2024 09:57:16 -0400 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1siDk2-0006jY-CT; Sun, 25 Aug 2024 09:57:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:From:To:In-Reply-To:References:Subject; bh=obTm9VPEfy4bu0nhnzP6EaZ233KgHgSRRgaVBiUO5MA=; b=KEVsb7gR5zBRympIz/L/6njCGorfIXPWMTy6cgDQHi3ritBHPRpOGXcT5cQEzl+O7NZT3RunzdFNyAl+s0D7zn4WwGu5z/4EOxPMqCXNaWIB4D4eN7u4SikIGbyOrmW2yTQ9Lle2ZOSxBR91EDDJVHETO/Un9FGDy2aA8zJiE9f5SaNRw5VY80WAJfTaZ+kZsp3GJKb9bqkDjebx46G5iA3WaWE4XSxkNfZ5GuuYdM6pqAx/fqb/qPDqfchU8StKf1HNU7LH0HUoxxOVkpaCqCm2xZr2tO1+XMUSEYXDw82hBiJlu4wO4GaL2+FhGQrlpXm9tgHRf714LRZBBtmEYQ==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1siDko-00077q-4p; Sun, 25 Aug 2024 09:58:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#72803] [PATCH] services: restic-backup: Add more restic commands to the restic-guix package. References: In-Reply-To: Resent-From: Giacomo Leidi Original-Sender: "Debbugs-submit" Resent-CC: pelzflorian@pelzflorian.de, ludo@gnu.org, matt@excalamus.com, maxim.cournoyer@gmail.com, guix-patches@gnu.org Resent-Date: Sun, 25 Aug 2024 13:58:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 72803 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: To: 72803@debbugs.gnu.org Cc: Giacomo Leidi , Florian Pelz , Ludovic =?UTF-8?Q?Court=C3=A8s?= , Matthew Trzcinski , Maxim Cournoyer X-Debbugs-Original-Xcc: Florian Pelz , Ludovic =?UTF-8?Q?Court=C3=A8s?= , Matthew Trzcinski , Maxim Cournoyer Received: via spool by 72803-submit@debbugs.gnu.org id=B72803.172459426927369 (code B ref 72803); Sun, 25 Aug 2024 13:58:02 +0000 Received: (at 72803) by debbugs.gnu.org; 25 Aug 2024 13:57:49 +0000 Received: from localhost ([127.0.0.1]:43091 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1siDkb-00077M-6v for submit@debbugs.gnu.org; Sun, 25 Aug 2024 09:57:49 -0400 Received: from confino.investici.org ([93.190.126.19]:57469) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1siDkZ-00077C-61 for 72803@debbugs.gnu.org; Sun, 25 Aug 2024 09:57:47 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=autistici.org; s=stigmate; t=1724594216; bh=obTm9VPEfy4bu0nhnzP6EaZ233KgHgSRRgaVBiUO5MA=; h=From:To:Cc:Subject:Date:From; b=ouY7Tyr3LBJqhFa4edtamjqvXWkZao4rIEn3T0B3/VrrhDSrY9X0RtDWLAD5M4eGI 2sjyxLUDZDHDc95ESeEtCZe+zPfC6XlhvN749vc+7HCEw3FX7f615VW5+Fp5LISwY3 KGXDLgm1v8d12tWqaqxBP/rnZm11DLxPDng6cGdA= Received: from mx1.investici.org (unknown [127.0.0.1]) by confino.investici.org (Postfix) with ESMTP id 4WsFjN1CB1z112m; Sun, 25 Aug 2024 13:56:56 +0000 (UTC) Received: from [93.190.126.19] (mx1.investici.org [93.190.126.19]) (Authenticated sender: goodoldpaul@autistici.org) by localhost (Postfix) with ESMTPSA id 4WsFjN0Ry3z112c; Sun, 25 Aug 2024 13:56:56 +0000 (UTC) Date: Sun, 25 Aug 2024 15:56:41 +0200 Message-ID: <1084765da10bf285803cbb7457997f73f785983d.1724594201.git.goodoldpaul@autistici.org> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: , Reply-to: Giacomo Leidi X-ACL-Warn: , Giacomo Leidi via Guix-patches From: Giacomo Leidi via Guix-patches via Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: guix-patches-bounces+larch=yhetil.org@gnu.org X-Migadu-Flow: FLOW_IN X-Migadu-Country: US X-Migadu-Spam-Score: -2.08 X-Spam-Score: -2.08 X-Migadu-Queue-Id: 0B10F74F53 X-Migadu-Scanner: mx11.migadu.com X-TUID: KdZ8iVP8iksx This patch refactors the way restic commands can be added to the restic-guix package with a more general approach. This way new subcommands for restic-guix can be added more easily. * gnu/services/backup.scm (restic-backup-job-program): Generalize to restic-action-program; (restic-guix): allow for multiple actions. * doc/guix.texi: Document it. Change-Id: Ib2b5d74bebc51e35f1ae6e1aa32cedee0da59697 --- doc/guix.texi | 10 +++- gnu/services/backup.scm | 116 +++++++++++++++++++++++++++++----------- 2 files changed, 93 insertions(+), 33 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index fcaf6b3fbb..9bbc2694ec 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -41589,7 +41589,15 @@ Miscellaneous Services configuration, without waiting for the scheduled job: @example -restic-guix backup remote-ftp +restic-guix run remote-ftp +@end example + +All arguments passed after the job name will be passed to the underlying +@code{restic} command, together with the @code{extra-flags} field from the +@code{restic-backup-job} record: + +@example +restic-guix restore remote-ftp -t `pwd`/restored -i .config/guix/channels.scm latest @end example @c %start of fragment diff --git a/gnu/services/backup.scm b/gnu/services/backup.scm index 555e9fc959..f304361263 100644 --- a/gnu/services/backup.scm +++ b/gnu/services/backup.scm @@ -46,7 +46,7 @@ (define-module (gnu services backup) restic-backup-configuration-fields restic-backup-configuration-jobs - restic-backup-job-program + restic-action-program restic-backup-job->mcron-job restic-guix restic-guix-wrapper-package @@ -107,15 +107,18 @@ (define-configuration/no-serialization restic-backup-configuration (list-of-restic-backup-jobs '()) "The list of backup jobs for the current system.")) -(define (restic-backup-job-program config) +(define %restic-guix-supported-actions + '("backup" "mount" "prune" "restore" "run" "unlock")) + +(define* (restic-action-program config action) (let ((restic (file-append (restic-backup-job-restic config) "/bin/restic")) + (name + (restic-backup-job-name config)) (repository (restic-backup-job-repository config)) (password-file (restic-backup-job-password-file config)) - (files - (restic-backup-job-files config)) (extra-flags (restic-backup-job-extra-flags config)) (verbose @@ -123,55 +126,104 @@ (define (restic-backup-job-program config) '("--verbose") '()))) (program-file - "restic-backup-job.scm" + (string-append "restic-" action "-" name "-program.scm") #~(begin (use-modules (ice-9 popen) - (ice-9 rdelim)) + (ice-9 rdelim) + (srfi srfi-1)) + + (define cli-arguments + (let* ((cl (command-line)) + (argc (length cl))) + (if (> argc 1) + (take-right cl (- argc 1)) + '()))) + (setenv "RESTIC_PASSWORD" (with-input-from-file #$password-file read-line)) - (execlp #$restic #$restic #$@verbose - "-r" #$repository - #$@extra-flags - "backup" #$@files))))) + (apply execlp `(#$restic #$restic #$@verbose + "-r" #$repository + #$@extra-flags + #$action ,@cli-arguments)))))) + +(define* (restic-guix jobs #:key (supported-actions + %restic-guix-supported-actions)) + (define action-table + (map + (lambda (action) + (list action + (map (lambda (job) + (list (restic-backup-job-name job) + (restic-action-program job action))) + jobs))) + ;; run is an alias for backup + (filter (lambda (a) (not (string=? a "run"))) supported-actions))) -(define (restic-guix jobs) (program-file "restic-guix" #~(begin (use-modules (ice-9 match) (srfi srfi-1)) + (define action-table '#$action-table) + (define (assoc-table key table) + (first + (filter-map + (match-lambda + ((k v) + (and (string=? key k) v))) + table))) (define names '#$(map restic-backup-job-name jobs)) - (define programs '#$(map restic-backup-job-program jobs)) + (define backup-files + '#$(map restic-backup-job-files jobs)) + + (define (get-program action name) + (assoc-table name (assoc-table action action-table))) - (define (get-program name) + (define (get-backup-files name) (define idx (list-index (lambda (n) (string=? n name)) names)) - (unless idx - (error (string-append "Unknown job name " name "\n\n" - "Possible job names are: " - (string-join names " ")))) - (list-ref programs idx)) - - (define (backup args) - (define name (third args)) - (define program (get-program name)) - (execlp program program)) + (list-ref backup-files idx)) (define (validate-args args) - (when (not (>= (length args) 3)) - (error (string-append "Usage: " (basename (car args)) - " backup NAME")))) + (unless (>= (length args) 2) + (error (string-append "Usage: " (basename (first args)) + " ACTION [ARGS]\n\nSupported actions are: " + #$(string-join supported-actions ", ") "."))) + (unless (member (second args) '#$supported-actions) + (error (string-append "Unknown action: " (second args) ". Supported" + "actions are: " + #$(string-join supported-actions ", ") ".")))) + + (define (validate-action-args action args) + (define argc (length args)) + (when (not (>= argc 3)) + (error (string-append "Usage: " (basename (first args)) + " " action " JOB_NAME [ARGS]\n\nPossible job " + "names are: " (string-join names ", ") "."))) + (define job-name (third args)) + (unless (member job-name names) + (error (string-append "Unknown job name: " job-name ". Possible job " + "names are: " (string-join names ", ") "."))) + (let ((program + (get-program + ;; run is just backup called with restic-backup-job-files + (if (string=? action "run") "backup" action) + job-name)) + (rest (if (> argc 3) + (take-right args (- argc 3)) + '()))) + (values program + (if (string=? action "run") + (append rest (get-backup-files job-name)) + rest)))) (define (main args) (validate-args args) (define action (second args)) - (match action - ("backup" - (backup args)) - (_ - (error (string-append "Unknown action: " action))))) + (define-values (program action-args) (validate-action-args action args)) + (apply execlp (append (list program program) action-args))) (main (command-line))))) @@ -183,7 +235,7 @@ (define (restic-backup-job->mcron-job config) (name (restic-backup-job-name config))) #~(job #$schedule - #$(string-append "restic-guix backup " name) + #$(string-append "restic-guix run " name) #:user #$user))) (define (restic-guix-wrapper-package jobs) base-commit: d48af5cca84914d44b032d0bf0820640ebbe7a4b -- 2.45.2