From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2.migadu.com ([2001:41d0:403:4876::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms13.migadu.com with LMTPS id 8B+pDzBxaWfchgAAe85BDQ:P1 (envelope-from ) for ; Mon, 23 Dec 2024 14:18:24 +0000 Received: from aspmx1.migadu.com ([2001:41d0:403:4876::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2.migadu.com with LMTPS id 8B+pDzBxaWfchgAAe85BDQ (envelope-from ) for ; Mon, 23 Dec 2024 15:18:24 +0100 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=om2VxuL0; dkim=fail ("headers rsa verify failed") header.d=autistici.org header.s=stigmate header.b=XUNG0jtb; 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"; dmarc=pass (policy=none) header.from=gnu.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1734963504; 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=34HVHqduCm+/2K5JxrJLwyP9CveFW3NN119T1uXcqLQ=; b=DN9ltgNNYM5HkxDrcblH1tDvNWV6Y7AW2ceG/d03VpikXitz+8CIX/TTV4+2qPERRmO9A7 NkGSf6ZZhQHFiGt5CKw/NfaOKxdvZtkgkQ1DyVJre+dSRR4nGRFE4FXYw/ydcFVX66YQ2U 076M8a0RuSyDfS0BPPM64Bncq65zU+oKtgC10owdXJFs8xCzjb2S45BiZbwOMVQfGeVT7I 2pHCYfb9hM3NmmUoPQcNIT7QhRKEAuK9Yxl92l/xj9Poq4xqMUGMq2tvqzd7ecqDI730zl b613iC37BvhCuvt870YP+EWF/qz9W+cUzizwlUSHgc98HDYLFFEi0lP6U4+sLA== 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=om2VxuL0; dkim=fail ("headers rsa verify failed") header.d=autistici.org header.s=stigmate header.b=XUNG0jtb; 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"; dmarc=pass (policy=none) header.from=gnu.org ARC-Seal: i=1; s=key1; d=yhetil.org; t=1734963504; a=rsa-sha256; cv=none; b=uHg7W+AdmW8FzTae7tDZsgVAFEExU3w9ON7iM5WgsybjpyP+uNev+rpXjIdwp+NVyImj9W xKUs2F8Z3VWyBbJvBC2GkTjjn1kxG+leSrC7plogfRV05863nl0xHarZaOcb9AwDbvlDDd 8OAi2r54WjeYIQm11WOJgiZML7EwubsVLe79CyP7oZDzJTML7KLBe6HDklLwao4xCs+kN3 4sw2ooASBTTYKevI952cLHKBVo7NwUaCfeW2JizisKj1NkKVCJTXfKxf9vly8w35JuKmBn 4UWOcwrOdat3W+ddYT6pM3C4qmKVlP4OVSTy1BHWDmSc4j3lViUrgiEKPG+QrQ== 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 00E8C1AE20 for ; Mon, 23 Dec 2024 15:18:23 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tPjG2-0005dl-H7; Mon, 23 Dec 2024 09:18:07 -0500 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 1tPjFz-0005dU-8S for guix-patches@gnu.org; Mon, 23 Dec 2024 09:18:03 -0500 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 1tPjFy-0005vh-6Z; Mon, 23 Dec 2024 09:18:02 -0500 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=34HVHqduCm+/2K5JxrJLwyP9CveFW3NN119T1uXcqLQ=; b=om2VxuL0MY0m3b3qa3M8QS+L3j65jRBmuDwJ40eTGFUgdzcBC3JZdrhonazSIjODlT/5MG9Gz6smNiEOqv/qISvcgxI0uzzcPyHLcMxamEqFV6vvRbsNuCFhlpFpjV6B5fobEKPc3TDhEPaqWPMw/KxcIXsJMRbrkPZhiSK9ybp7dQAt6RNElsB++H90e+n0BIVDTvwSOdwjhZAcfVvmNPpEBoJeFX9nlTTCav+4bCgax6ShOAPQ3yF5uPhG7qcBVioxaj1+yKngoBLVuhQaqxwXgHNjAyWr2MlnrwFggienX5M9xYT1wXJVhnzteg+kX3jVaC6dDnoRse8p66/G+g==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tPjFx-0007ik-R6; Mon, 23 Dec 2024 09:18:01 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#72803] [PATCH v4] 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: ludo@gnu.org, maxim.cournoyer@gmail.com, guix-patches@gnu.org Resent-Date: Mon, 23 Dec 2024 14:18:01 +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 , Ludovic =?UTF-8?Q?Court=C3=A8s?= , Maxim Cournoyer X-Debbugs-Original-Xcc: Ludovic =?UTF-8?Q?Court=C3=A8s?= , Maxim Cournoyer Received: via spool by 72803-submit@debbugs.gnu.org id=B72803.173496346029637 (code B ref 72803); Mon, 23 Dec 2024 14:18:01 +0000 Received: (at 72803) by debbugs.gnu.org; 23 Dec 2024 14:17:40 +0000 Received: from localhost ([127.0.0.1]:53808 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tPjFb-0007hw-MQ for submit@debbugs.gnu.org; Mon, 23 Dec 2024 09:17:40 -0500 Received: from confino.investici.org ([93.190.126.19]:20769) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tPjFY-0007hi-Nl for 72803@debbugs.gnu.org; Mon, 23 Dec 2024 09:17:38 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=autistici.org; s=stigmate; t=1734963446; bh=34HVHqduCm+/2K5JxrJLwyP9CveFW3NN119T1uXcqLQ=; h=From:To:Cc:Subject:Date:From; b=XUNG0jtbQZr2i7tA+J14slY3QpDCG7h37VXgnJoDXgLUCvKb07w9cbcBYMkizblon 5d0fY0TIl2xbkua8jgavEzWo+0rV4N1bezSDMaTiSqCvDXZE3aPZYwepB8gAHDC0ax AJKMknTK848L3G99QbeqpIsumCtcsbrehFm9xfqk= Received: from mx1.investici.org (unknown [127.0.0.1]) by confino.investici.org (Postfix) with ESMTP id 4YH0Tf5rYfz10xL; Mon, 23 Dec 2024 14:17:26 +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 4YH0Tf4TQxz10wM; Mon, 23 Dec 2024 14:17:26 +0000 (UTC) Date: Mon, 23 Dec 2024 15:17:18 +0100 Message-ID: X-Mailer: git-send-email 2.46.0 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-Country: US X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -4.31 X-Spam-Score: -4.31 X-Migadu-Queue-Id: 00E8C1AE20 X-Migadu-Scanner: mx10.migadu.com X-TUID: sTn6zEk/e2JY 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 | 20 ++++++- gnu/services/backup.scm | 129 ++++++++++++++++++++++++++++------------ 2 files changed, 109 insertions(+), 40 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index f77b765933..aca87c7274 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -41999,6 +41999,23 @@ Miscellaneous Services sudo herd trigger remote-ftp-job @end example +The @code{restic-backup-service-type} installs as well @code{restic-guix} +to the system profile, a @code{restic} utility wrapper that allows for easier +interaction with the Guix configured backup jobs. For example the following +could be used to list all the shapshots available on a given job's repository: + +@example +restic-guix snapshots 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 @deftp {Data Type} restic-backup-configuration @@ -42071,8 +42088,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 fc8934873b..5c693660e3 100644 --- a/gnu/services/backup.scm +++ b/gnu/services/backup.scm @@ -52,11 +52,12 @@ (define-module (gnu services backup) restic-backup-configuration-fields restic-backup-configuration-jobs - restic-backup-job-program - restic-backup-job->mcron-job + restic-action-program + restic-backup-job->shepherd-service restic-guix restic-guix-wrapper-package restic-backup-service-profile + restic-backup-service-activation restic-backup-service-type)) (define (gexp-or-string? value) @@ -128,7 +129,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?)) @@ -138,15 +139,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" "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 @@ -154,55 +167,90 @@ (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))) + 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 (get-program 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)) + (define (get-program action name) + (assoc-table name (assoc-table action action-table))) (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 action job-name)) + (rest (if (> argc 3) + (take-right args (- argc 3)) + '()))) + (values program 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))))) @@ -216,6 +264,10 @@ (define (restic-job-log-file job) (define (restic-backup-job->shepherd-service config) (let ((schedule (restic-backup-job-schedule config)) (name (restic-backup-job-name config)) + (files (string-join + (map (lambda (f) (string-append "'" f "'")) + (restic-backup-job-files config)) + " ")) (user (restic-backup-job-user config)) (group (restic-backup-job-group config)) (max-duration (restic-backup-job-max-duration config)) @@ -238,7 +290,8 @@ (define (restic-backup-job->shepherd-service config) (list (string-append #+bash-minimal "/bin/bash") "-l" "-c" - (string-append "restic-guix backup " #$name)) + (string-append + "restic-guix backup " #$name " " #$files)) #:user #$user #:group #$group #:environment-variables @@ -283,7 +336,7 @@ (define restic-backup-service-profile (restic-guix-wrapper-package jobs)) '()))) -(define (restic-backup-activation config) +(define (restic-backup-service-activation config) #~(for-each (lambda (log-file) (mkdir-p (dirname log-file))) @@ -295,7 +348,7 @@ (define restic-backup-service-type (extensions (list (service-extension activation-service-type - restic-backup-activation) + restic-backup-service-activation) (service-extension profile-service-type restic-backup-service-profile) (service-extension shepherd-root-service-type base-commit: f52cde358b609d18f43bf62f1dfe63835c1a57b9 -- 2.46.0