From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms13.migadu.com with LMTPS id oPwWFWmLFWe3KQEAe85BDQ:P1 (envelope-from ) for ; Sun, 20 Oct 2024 22:59:53 +0000 Received: from aspmx1.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2.migadu.com with LMTPS id oPwWFWmLFWe3KQEAe85BDQ (envelope-from ) for ; Mon, 21 Oct 2024 00:59:53 +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=bHkfLFAr; dkim=fail ("headers rsa verify failed") header.d=autistici.org header.s=stigmate header.b=OFZN2eQp; 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-Seal: i=1; s=key1; d=yhetil.org; t=1729465193; a=rsa-sha256; cv=none; b=hL09W8175TKw0lIBYCWR1EJCAsmmdYNbiyRNv6QOp5uQh6LfEZNnx6KvuUNCNdgZn0CXpu NQnlCvGf6jcv5MsHx02uDX+Xsoy6vegBcS15gSNQ1xTVI9c6edf9kyF9F4mdpHxa1bMmDV GOsYsTzOD8idX2ngRBhap9YzdBYtc+msvOGwqQUdSSHL+V+8ju59FtoWDQWa+J0Lq2L0/o y0kiGy80QwOXemlMCWUt3B6xxhhzjrebOlCRXzhhvuegnyOGdeIxwxkQOelek0Uvax2QLY lyRAgF+OsyWxQRND7GlhkRiNV0Kuux/7hLKS2eVBGj1uUppgteYEjCHdlBV24A== 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=bHkfLFAr; dkim=fail ("headers rsa verify failed") header.d=autistici.org header.s=stigmate header.b=OFZN2eQp; 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=1729465193; 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=f67Gt87r8aNspn8YlYsFKNef2NKrXCJNKPq2YcLH4bQ=; b=IWPyOOw08DHOOciQHbNdkUBO2C2O6E9qB+zzn3emVdD2hXrTuCBS4NbvjoVnxiSgn2vUW1 5wa0M/sKPKTrDYu58MRq8FN1QI/NhAWro74HPVZZG7qF+nRO8ecHi9GjuFKKVGTobahHds XPbjEFJ/7OJ68rGM4o8jLv195Wl2T5dh51UiCSBKCBtsG7n30LmVJlnM4sJsMw15Vnt/eQ +BrkdRwMsjXs44DJGkrZPl+2mnHz0wQta+ZxYXZkSQVedYSgdwgE8lVj3KP6wyHDGgXg4p IinsKtQ68g93ztrxL6IdYE19jCLbwahLcFHtOnugj57z5McjN77npLnL65UvOA== 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 1DE8981F6F for ; Mon, 21 Oct 2024 00:59:53 +0200 (CEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t2ete-0003Sy-MV; Sun, 20 Oct 2024 18:59:38 -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 1t2etd-0003Sq-Mp for guix-patches@gnu.org; Sun, 20 Oct 2024 18:59:37 -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 1t2etc-0002Gs-Iw; Sun, 20 Oct 2024 18:59:36 -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=f67Gt87r8aNspn8YlYsFKNef2NKrXCJNKPq2YcLH4bQ=; b=bHkfLFArvfT1N9OiNNAogEHqJDn3dPMNZZaVMJa/Eh5b1BY6SPZdRnzlBN5sCED5F1CsXj1GXT+m2DHWKdHx2BOt3pn9mI+6lASKIHFehmTW1IrRVAOntRswKy8GxEE3Fd7fGhHL1VrkLrWSqJvf3DCcbe5rTZO3B/nDCarPgTWgU5FdnnxlwPpjtQOPq+Q/vAvo9nCMmF8CxWe48SI23FKMeHc5N35GffZ8XQIDIrEI7e0r0QURGwNYosDGVNhVFiR3zOMtfwyflrcRP5GBB8QcjRWXrOSydaMM33iBRVpJ4BilyYASpivqh3tNVJPlniSZS+LQoK/fPQqO3bRkkA==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1t2eu2-0002Kz-6R; Sun, 20 Oct 2024 19:00: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: ludo@gnu.org, maxim.cournoyer@gmail.com, guix-patches@gnu.org Resent-Date: Sun, 20 Oct 2024 23:00: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 , 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.17294651498879 (code B ref 72803); Sun, 20 Oct 2024 23:00:02 +0000 Received: (at 72803) by debbugs.gnu.org; 20 Oct 2024 22:59:09 +0000 Received: from localhost ([127.0.0.1]:49271 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1t2etA-0002J8-PA for submit@debbugs.gnu.org; Sun, 20 Oct 2024 18:59:09 -0400 Received: from confino.investici.org ([93.190.126.19]:61213) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1t2et8-0002J0-Qh for 72803@debbugs.gnu.org; Sun, 20 Oct 2024 18:59:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=autistici.org; s=stigmate; t=1729465120; bh=f67Gt87r8aNspn8YlYsFKNef2NKrXCJNKPq2YcLH4bQ=; h=From:To:Cc:Subject:Date:From; b=OFZN2eQpSmCfD90yIilvkxgE/PTtHyHhbZUhXZDwPHjRDjG1XP3vUsP7/PYJwYMOX DrV2WS3u9SlPXhrxOgfgJN95EC/Cjp0qkvs9aWYC9E6upLDnjYFq2N4CovKcVkOPDm FN6SjRLu65sIC+WRTnexT8qV2a4szMUlz10mok6s= Received: from mx1.investici.org (unknown [127.0.0.1]) by confino.investici.org (Postfix) with ESMTP id 4XWv4c4CfKz10y2; Sun, 20 Oct 2024 22:58:40 +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 4XWv4b6J04z10xr; Sun, 20 Oct 2024 22:58:39 +0000 (UTC) Date: Mon, 21 Oct 2024 00:58:31 +0200 Message-ID: <31413c193ddf6caccb7e23dd75796b6d89d5ceb7.1729465111.git.goodoldpaul@autistici.org> 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-Flow: FLOW_IN X-Migadu-Country: US X-Migadu-Spam-Score: 2.50 X-Spam-Score: 2.50 X-Migadu-Queue-Id: 1DE8981F6F X-Migadu-Scanner: mx12.migadu.com X-TUID: zInLDsBgaC37 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(-) diff --git a/doc/guix.texi b/doc/guix.texi index ac3a7adef0..f8a73abdce 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -41711,7 +41711,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 @@ -41767,8 +41775,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) base-commit: 5ab3c4c1e43ebb637551223791db0ea3519986e1 -- 2.46.0