From patchwork Wed Sep 4 22:29:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Giacomo Leidi X-Patchwork-Id: 67619 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 05D2B27BBEA; Wed, 4 Sep 2024 23:30:30 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-6.4 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_VALIDITY_CERTIFIED, RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE,SPF_HELO_PASS, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 8819927BBE2 for ; Wed, 4 Sep 2024 23:30:28 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1slyVo-0002bx-1j; Wed, 04 Sep 2024 18:30:04 -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 1slyVl-0002aa-5R for guix-patches@gnu.org; Wed, 04 Sep 2024 18:30:01 -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 1slyVk-0000Jz-Iq; Wed, 04 Sep 2024 18:30:00 -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=7HwOFB1BF1Uq8kEoj3V/SDn9X2A6eD4zAo9AVcKztT0=; b=kQ65OveKWMCxELWWFw5U0n0KwHCEeryega+GCOOJqOwArLUg2N0x8xOuckwhEZ85r48dkosEbsMGCgFGSV4xzeXNKY9rgopLKWod2V1bUi83C9QUis14z29uW3KcyplvpPHG46ILpjio+jr29YysVhXN8RUQzXxCAOCwivkb3YC42DKBrLG4TwEO2dBixMK/N98VknyC3+OP0LvIMKVXvHSfEJMoYVjdozCrVOwN7HK4/zCPSP/3NSCInYxiN8AOttr8BNQI8s8WL2KtHgJ8/6WEzsapOkHL/1aa5BKQ6IYshtQK/9NTE5NwTSrX/c9oJkudC7N0BjLozX6yqbZSTQ==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1slyWk-0003eL-GV; Wed, 04 Sep 2024 18:31:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#72803] [PATCH v2] 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, maxim.cournoyer@gmail.com, guix-patches@gnu.org Resent-Date: Wed, 04 Sep 2024 22:31: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?= , Maxim Cournoyer X-Debbugs-Original-Xcc: Florian Pelz , Ludovic =?utf-8?q?Court=C3=A8s?= , Maxim Cournoyer Received: via spool by 72803-submit@debbugs.gnu.org id=B72803.172548903213980 (code B ref 72803); Wed, 04 Sep 2024 22:31:02 +0000 Received: (at 72803) by debbugs.gnu.org; 4 Sep 2024 22:30:32 +0000 Received: from localhost ([127.0.0.1]:35678 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1slyWF-0003dP-If for submit@debbugs.gnu.org; Wed, 04 Sep 2024 18:30:32 -0400 Received: from confino.investici.org ([93.190.126.19]:60313) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1slyWE-0003dI-Dr for 72803@debbugs.gnu.org; Wed, 04 Sep 2024 18:30:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=autistici.org; s=stigmate; t=1725488958; bh=7HwOFB1BF1Uq8kEoj3V/SDn9X2A6eD4zAo9AVcKztT0=; h=From:To:Cc:Subject:Date:From; b=Lwhvgbn1Kv+TkuMHHf8v7zrbmxdcBMqwbrc/br/23Xs10l/EByEjCQ5+rPSOqLnBs kedMmywa9YWIVoadZ7JN2aLA/dY+lSHRPlcbpgSDPzotAlXCNCn+ZHpJQ6o+xtVEWk 7b5y2/GcKakl/RvTuftGciWoz/TqOJHIz7xNwH10= Received: from mx1.investici.org (unknown [127.0.0.1]) by confino.investici.org (Postfix) with ESMTP id 4Wzcby3hwQz115w; Wed, 4 Sep 2024 22:29:18 +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 4Wzcby2wR3z114S; Wed, 4 Sep 2024 22:29:18 +0000 (UTC) Date: Thu, 5 Sep 2024 00:29:04 +0200 Message-ID: X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 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 X-Patchwork-Original-From: Giacomo Leidi via Guix-patches via From: Giacomo Leidi Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches 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 | 13 +++- gnu/services/backup.scm | 127 +++++++++++++++++++++++++++++----------- 2 files changed, 104 insertions(+), 36 deletions(-) base-commit: 9a03ab25ba889be27b34d5cebea05d5ac3b0a033 diff --git a/doc/guix.texi b/doc/guix.texi index 16c697586a..8e3ecb80c2 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -41611,7 +41611,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 @@ -41667,8 +41675,7 @@ Miscellaneous Services @item @code{extra-flags} (default: @code{'()}) (type: list-of-lowerables) A list of values that are lowered to strings. These will be passed as -command-line arguments to the current job @command{restic backup} -invokation. +command-line arguments to the current @command{restic} invokation. @end table diff --git a/gnu/services/backup.scm b/gnu/services/backup.scm index 555e9fc959..83d388143e 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 @@ -97,7 +97,7 @@ (define-configuration/no-serialization restic-backup-job (extra-flags (list-of-lowerables '()) "A list of values that are lowered to strings. These will be passed as -command-line arguments to the current job @command{restic backup} invokation.")) +command-line arguments to the current @command{restic} invokation.")) (define list-of-restic-backup-jobs? (list-of restic-backup-job?)) @@ -107,15 +107,27 @@ (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" "snapshots" "unlock")) + +(define* (restic-action-program config action) + (define (format name) + ;; Remove from NAME characters that cannot be used in the store. + (string-map (lambda (chr) + (if (and (char-set-contains? char-set:ascii chr) + (char-set-contains? char-set:graphic chr) + (not (memv chr '(#\. #\/ #\space)))) + chr + #\-)) + name)) (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 +135,104 @@ (define (restic-backup-job-program config) '("--verbose") '()))) (program-file - "restic-backup-job.scm" + (string-append "restic-" action "-" (format 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 +244,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)