Add restic commands to the restic-guix package

  • Open
  • quality assurance status badge
Details
3 participants
  • paul
  • Ludovic Courtès
  • Fabio Natali
Owner
unassigned
Submitted by
paul
Severity
normal
Blocked by

Debbugs page

paul wrote 7 months ago
(address . guix-patches@gnu.org)
db336bf4-14d8-e969-b998-dd5f98108066@autistici.org
Dear all,

I'm sending a patch adding some more restic commands to the restic-guix
package provided by the restic-backup-service-type. It allows for
commands like the following:

restic-guix restore remote-ftp -t `pwd`/restored -i .config/guix/channels.scm latest


Thank you for your work,

giacomo
Attachment: file
Giacomo Leidi wrote 7 months ago
[PATCH] services: restic-backup: Add more restic commands to the restic-guix package.
(address . 72803@debbugs.gnu.org)(name . Giacomo Leidi)(address . goodoldpaul@autistici.org)
1084765da10bf285803cbb7457997f73f785983d.1724594201.git.goodoldpaul@autistici.org
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(-)

Toggle diff (201 lines)
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
Fabio Natali wrote 6 months ago
Re: Add restic commands to the restic-guix package
(address . 72803@debbugs.gnu.org)(name . Giacomo Leidi)(address . goodoldpaul@autistici.org)
8734mhfz6e.fsf@fabionatali.com
Hi Giacomo,

Thanks for the patch and for the Restic service in the first place.

Toggle quote (2 lines)
> diff --git a/doc/guix.texi b/doc/guix.texi

In the manual, consider the "extra-flags" section where we say:

Toggle quote (3 lines)
> A list of values that are lowered to strings. These will be passed as
> command-line arguments to the current job restic backup invokation.

Perhaps this should now read "...the current job restic invokation..."
or "...the current restic invokation...", as the action is no longer
limited to "backup"?

Toggle quote (4 lines)
> (program-file
> - "restic-backup-job.scm"
> + (string-append "restic-" action "-" name "-program.scm")

Should 'name' be slug-ified in any way here? E.g. to avoid spaces,
capital letters, symbols that might be confusing when part of a file
name, etc.

Toggle quote (11 lines)
> + (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)))

Could this be (marginally) simpler if we used two nested association
lists? That way, 'get-program' might simply use 'assoc-ref' (twice) and
'assoc-table' would be redundant?

Everything else looks fine to me. For what it's worth, here's how I've
been testing this.

Initialise a Restic repository as follows (warning: this overwrites
'/some-temporary-folder/password'):

Toggle snippet (6 lines)
mkdir /some-temporary-folder
export RESTIC_PASSWORD=password
restic init --repo=/some-temporary-folder/repository
echo "${RESTIC_PASSWORD}" > /some-temporary-folder/password

Save the following system definition as
'/some-temporary-folder/config.scm'.

Toggle snippet (28 lines)
(use-modules (gnu))
(use-package-modules backup)
(use-service-modules backup)

(operating-system
(host-name "host")
(bootloader (bootloader-configuration
(bootloader grub-bootloader)
(targets '("/dev/vda"))))
(file-systems (cons (file-system
(device "/dev/vda1")
(mount-point "/")
(type "ext4"))
%base-file-systems))
(packages (cons* restic %base-packages))
(services (cons*
(service restic-backup-service-type
(restic-backup-configuration
(jobs
(list (restic-backup-job
(name "test")
(repository "/restic/repository")
(password-file "/restic/password")
(schedule "* * * * *")
(files '("/root")))))))
%base-services)))

From a Guix checkout where this patch has been applied, launch a test VM
as follows:

Toggle snippet (7 lines)
$(./pre-inst-env guix system vm \
--no-graphic \
--share=/some-temporary-folder=/restic \
/tmp/config.scm) \
-m 2048 -smp 2

Log in as root, then check that the cron schedule is correctly defined
with 'herd schedule mcron', backup jobs should be scheduled every
minute.

Mount the Restic repository to see that snapshots have been actually
created every minute since boot. This can be done either on the guest or
on the host system. E.g. on the guest:

Toggle snippet (6 lines)
restic mount \
--password-file=/restic/password \
--repo=/restic/repository \
/mnt

Unfortunately I don't have commit access to push this, but hopefully
someone else will have a second look and push it soon.

It'd be nice to have a little test suite for this, but in case this can
be part of a future patch.

HTH, thanks, Fabio.
Fabio Natali wrote 6 months ago
(address . 72803@debbugs.gnu.org)(name . Giacomo Leidi)(address . goodoldpaul@autistici.org)(name . Fabio Natali)(address . me@fabionatali.com)
87zfopek2m.fsf@fabionatali.com
Toggle quote (3 lines)
> Mount the Restic repository to see that snapshots have been actually
> created every minute since boot.

Ha, sorry, I should have mentioned the revised 'restic-guix' script too,
which I tested with various commands and that also seemed to be working
fine.

Thanks, cheers, Fabio.
paul wrote 6 months ago
(name . Fabio Natali)(address . me@fabionatali.com)(address . 72803@debbugs.gnu.org)
2d83c75b-b750-b80d-5d7c-b4a6c89b4434@autistici.org
Hi Fabio,

thank you very much for your detailed testing and review.

On 9/3/24 00:50, Fabio Natali wrote:
Toggle quote (4 lines)
> Perhaps this should now read "...the current job restic invokation..."
> or "...the current restic invokation...", as the action is no longer
> limited to "backup"?

Definitely, good catch.

Toggle quote (6 lines)
>> (program-file
>> - "restic-backup-job.scm"
>> + (string-append "restic-" action "-" name "-program.scm")
> Should 'name' be slug-ified in any way here? E.g. to avoid spaces,
> capital letters, symbols that might be confusing when part of a file
> name, etc.
It should, right. I'll use the same approach used for the
home-dotfiles-service-type (i.e. replacing illegal characters with "-").
Toggle quote (13 lines)
>> + (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)))
> Could this be (marginally) simpler if we used two nested association
> lists? That way, 'get-program' might simply use 'assoc-ref' (twice) and
> 'assoc-table' would be redundant?
I thought that as well, in fact my first implementation was with Guile's
vhashes but it appears that neither alists nor vhashesh can be correctly
ungexped, or at least I didn't find a way to do so. This is why I'm
using plain lists and I need assoc-table. If you have some pointer where
I could look how to lower alists it would be very helpful.
Toggle quote (3 lines)
> It'd be nice to have a little test suite for this, but in case this can
> be part of a future patch.

There are already some tests Richard made at #71639 , once they get in
I'll make sure to expand them for additional restic-guix subcommands.

Thank your for your review Fabio, I'm sending a patchset addressing your
comments.

giacomo
Giacomo Leidi wrote 6 months ago
[PATCH v2] services: restic-backup: Add more restic commands to the restic-guix package.
(address . 72803@debbugs.gnu.org)(name . Giacomo Leidi)(address . goodoldpaul@autistici.org)
cba87a67ff4b6dfdc7620b45c9fec4d1ee0321bc.1725488944.git.goodoldpaul@autistici.org
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(-)

Toggle diff (229 lines)
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)

base-commit: 9a03ab25ba889be27b34d5cebea05d5ac3b0a033
--
2.45.2
paul wrote 5 months ago
Re: Add restic commands to the restic-guix package
(address . 72803@debbugs.gnu.org)
dd908cb9-d4b7-4c5e-84f8-d9a7d6e625c9@autistici.org
Hi Guix , this is a friendly ping. I'm sending a patchset rebased on
current master.

Thank you for your work,

giacomo
Attachment: file
Giacomo Leidi wrote 5 months ago
[PATCH v2] services: restic-backup: Add more restic commands to the restic-guix package.
(address . 72803@debbugs.gnu.org)(name . Giacomo Leidi)(address . goodoldpaul@autistici.org)
31413c193ddf6caccb7e23dd75796b6d89d5ceb7.1729465111.git.goodoldpaul@autistici.org
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(-)

Toggle diff (229 lines)
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
paul wrote 3 months ago
Re: Add restic commands to the restic-guix package
(address . 72803@debbugs.gnu.org)
c5977a15-dedd-4396-8327-fa9a52710f80@autistici.org
Hi,


I'm sending a v3 based on issue#75045.


Thank you for your work,

cheers

giacomo
Giacomo Leidi wrote 3 months ago
[PATCH v3] services: restic-backup: Add more restic commands to the restic-guix package.
(address . 72803@debbugs.gnu.org)(name . Giacomo Leidi)(address . goodoldpaul@autistici.org)
9e1eec4968529a6f8bdcf6b4a14c91ce1595d0ad.1734953075.git.goodoldpaul@autistici.org
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 | 125 ++++++++++++++++++++++++++++++----------
2 files changed, 111 insertions(+), 34 deletions(-)

Toggle diff (227 lines)
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..8d1959f9bf 100644
--- a/gnu/services/backup.scm
+++ b/gnu/services/backup.scm
@@ -52,7 +52,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
@@ -128,7 +128,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 +138,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
@@ -154,55 +166,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 name)
+ (define (get-program action name)
+ (assoc-table name (assoc-table action action-table)))
+
+ (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)))))

base-commit: f52cde358b609d18f43bf62f1dfe63835c1a57b9
--
2.46.0
paul wrote 3 months ago
block 72803 by 75045
(address . control@debbugs.gnu.org)
581fb166-b3b6-4eab-a349-ff7a75798873@autistici.org
block 72803 by 75045
paul wrote 3 months ago
Re: Add restic commands to the restic-guix package
(address . 72803@debbugs.gnu.org)
08b873f4-26da-478a-bd61-91a03da51fb5@autistici.org
Actually I'm sending a v4 dropping the "run" subcommand from
restic-guix, since now we can use


sudo herd trigger job-name


Apologies for the noise,

giacomo
Giacomo Leidi wrote 3 months ago
[PATCH v4] services: restic-backup: Add more restic commands to the restic-guix package.
(address . 72803@debbugs.gnu.org)(name . Giacomo Leidi)(address . goodoldpaul@autistici.org)
e78157d5ce65108ac29f44f612ca68664b0eab6d.1734963438.git.goodoldpaul@autistici.org
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(-)

Toggle diff (260 lines)
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
paul wrote 2 months ago
Re: Add restic commands to the restic-guix package
(address . 72803@debbugs.gnu.org)
b6841b0b-2715-48e2-8928-837764e5df96@autistici.org
Hi Guix, I'm sending a v5 rebased on current master, thank you for your
time!
Giacomo Leidi wrote 2 months ago
[PATCH v5] services: restic-backup: Add more restic commands to the restic-guix package.
(address . 72803@debbugs.gnu.org)(name . Giacomo Leidi)(address . goodoldpaul@autistici.org)
757f3b471a0b994c34fd6c8371aa19072ed04431.1737762473.git.goodoldpaul@autistici.org
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(-)

Toggle diff (260 lines)
diff --git a/doc/guix.texi b/doc/guix.texi
index 9a53bdcd374..716dd312cd2 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -42722,6 +42722,23 @@ Miscellaneous Services
sudo herd trigger remote-ftp
@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
@@ -42795,8 +42812,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 99a79ff5fbe..49655d1d930 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)
@@ -129,7 +130,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?))
@@ -139,15 +140,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
@@ -155,55 +168,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)))))
@@ -217,6 +265,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))
@@ -242,7 +294,8 @@ (define (restic-backup-job->shepherd-service config)
;; backends require, such as rclone.
(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
@@ -287,7 +340,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)))
@@ -299,7 +352,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: 646202bf73f90de4f9b7cc66248b8f8e6e381014
--
2.47.1
paul wrote 1 months ago
Re: Add restic commands to the restic-guix package
(address . 72803@debbugs.gnu.org)
de6259a5-5c42-4158-a407-8c12d24036bd@autistici.org
Hi Guix,

I'm sending a v6. Compared to v5 it has the nice property of greatly
simplifying the restic-guix script. Now there are no more multiple guile
entrypoints (it used to be number of jobs * number of supported actions,
so quite bad), there is only a single script which has embedded the
information of all jobs, implements multiple actions and should be
easily extendible to include more actions (like the init one proposed in


Please let me know your thoughts on this!


Thank you all for your work,

giacomo
Giacomo Leidi wrote 1 months ago
[PATCH v6] services: restic-backup: Add more restic commands to the restic-guix package.
(address . 72803@debbugs.gnu.org)(name . Giacomo Leidi)(address . goodoldpaul@autistici.org)
a1fac696c4ef5e20d0412ec22ae6a0d77ea26682.1739142079.git.goodoldpaul@autistici.org
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-program;
(restic-guix): allow for multiple actions.

* doc/guix.texi: Document it.

Change-Id: Ib2b5d74bebc51e35f1ae6e1aa32cedee0da59697
---
doc/guix.texi | 20 +++++-
gnu/services/backup.scm | 138 ++++++++++++++++++++++++++--------------
2 files changed, 108 insertions(+), 50 deletions(-)

Toggle diff (274 lines)
diff --git a/doc/guix.texi b/doc/guix.texi
index ce780682ed0..86582fb4785 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -42837,6 +42837,23 @@ Miscellaneous Services
sudo herd trigger remote-ftp
@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
@@ -42910,8 +42927,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 99a79ff5fbe..dcbed890e13 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-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)
@@ -129,7 +130,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?))
@@ -139,71 +140,107 @@ (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-backup-job->kv config)
(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
+ (verbose?
(if (restic-backup-job-verbose? config)
'("--verbose")
'())))
- (program-file
- "restic-backup-job.scm"
- #~(begin
- (use-modules (ice-9 popen)
- (ice-9 rdelim))
- (setenv "RESTIC_PASSWORD"
- (with-input-from-file #$password-file read-line))
+ #~(list #$name (list #$restic #$repository #$password-file
+ (list #$@verbose?) (list #$@extra-flags)))))
+
+(define (restic-program config)
+ #~(lambda* (action action-args job-restic repository password-file verbose? extra-flags)
+ (use-modules (ice-9 format)
+ (ice-9 popen)
+ (ice-9 rdelim))
+ ;; This can be extended later, i.e. to have a
+ ;; centrally defined restic package.
+ ;; See https://issues.guix.gnu.org/71639
+ (define restic job-restic)
+
+ (define command
+ `(,restic ,@verbose?
+ "-r" ,repository
+ ,@extra-flags
+ ,action ,@action-args))
+
+ (setenv "RESTIC_PASSWORD"
+ (with-input-from-file password-file read-line))
- (execlp #$restic #$restic #$@verbose
- "-r" #$repository
- #$@extra-flags
- "backup" #$@files)))))
+ (when (> (length verbose?) 0)
+ (format #t "Running~{ ~a~}~%" command))
-(define (restic-guix jobs)
+ (apply execlp `(,restic ,@command))))
+
+(define* (restic-guix config #:key (supported-actions
+ %restic-guix-supported-actions))
(program-file
"restic-guix"
#~(begin
(use-modules (ice-9 match)
(srfi srfi-1))
- (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 jobs
+ (list
+ #$@(map restic-backup-job->kv
+ (restic-backup-configuration-jobs config))))
+ (define names (map first jobs))
+ (define (get-job key)
+ (first
+ (filter-map
+ (match-lambda
+ ((k v)
+ (and (string=? key k) v)))
+ jobs)))
- (define (backup args)
- (define name (third args))
- (define program (get-program name))
- (execlp program program))
+ (define restic-exec
+ #$(restic-program config))
(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 ((job (get-job job-name))
+ (action-args
+ (if (> argc 3)
+ (take-right args (- argc 3))
+ '())))
+ (values job action-args)))
(define (main args)
(validate-args args)
(define action (second args))
- (match action
- ("backup"
- (backup args))
- (_
- (error (string-append "Unknown action: " action)))))
+ (define-values (job action-args) (validate-action-args action args))
+ (apply restic-exec `(,action ,action-args ,@job)))
(main (command-line)))))
@@ -217,6 +254,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))
@@ -242,7 +283,8 @@ (define (restic-backup-job->shepherd-service config)
;; backends require, such as rclone.
(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
@@ -261,11 +303,11 @@ (define (restic-backup-job->shepherd-service config)
without waiting for the scheduled time.")
(procedure #~trigger-timer)))))))
-(define (restic-guix-wrapper-package jobs)
+(define (restic-guix-wrapper-package config)
(package
(name "restic-backup-service-wrapper")
(version "0.0.0")
- (source (restic-guix jobs))
+ (source (restic-guix config))
(build-system copy-build-system)
(arguments
(list #:install-plan #~'(("./" "/bin"))))
@@ -284,10 +326,10 @@ (define restic-backup-service-profile
(define jobs (restic-backup-configuration-jobs config))
(if (> (length jobs) 0)
(list
- (restic-guix-wrapper-package jobs))
+ (restic-guix-wrapper-package config))
'())))
-(define (restic-backup-activation config)
+(define (restic-backup-service-activation config)
#~(for-each
(lambda (log-file)
(mkdir-p (dirname log-file)))
@@ -299,7 +341,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: e27e63e6fe72d3a6cb6a8755f290ec710d339a9a
--
2.48.1
paul wrote 1 months ago
block 76169 by 72803
(address . control@debbugs.gnu.org)
ed23b5b6-b369-4c19-84a5-502c3d566742@autistici.org
block 76169 by 72803
paul wrote 3 weeks ago
Re: Add restic commands to the restic-guix package
(address . 72803@debbugs.gnu.org)
cc6cdfdc-d6e0-42f3-9526-307a39f96d39@autistici.org
Hi,

I'm sending a v6 rebased on current master. Compared to v5 it adds
support for the restic ls an restic list commands.

cheers,


giacomo
paul wrote 3 weeks ago
(address . 72803@debbugs.gnu.org)
3b9b96ec-5f67-4ca0-b10c-67518d18d642@autistici.org
Hi again, sorry for the noise, I meant v7 :)

Thank you for your work!

giacomo
Giacomo Leidi wrote 3 weeks ago
[PATCH v7] services: restic-backup: Add more restic commands to the restic-guix package.
(address . 72803@debbugs.gnu.org)(name . Giacomo Leidi)(address . goodoldpaul@autistici.org)
f87b0019e7a43e5f74d338278fc807aac68a3a01.1740329004.git.goodoldpaul@autistici.org
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-program;
(restic-guix): allow for multiple actions.

* doc/guix.texi: Document it.

Change-Id: Ib2b5d74bebc51e35f1ae6e1aa32cedee0da59697
---
doc/guix.texi | 20 +++++-
gnu/services/backup.scm | 138 ++++++++++++++++++++++++++--------------
2 files changed, 108 insertions(+), 50 deletions(-)

Toggle diff (274 lines)
diff --git a/doc/guix.texi b/doc/guix.texi
index 83ba0f32923..5c99521f8e6 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -43271,6 +43271,23 @@ Miscellaneous Services
sudo herd trigger remote-ftp
@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
@@ -43344,8 +43361,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}
-invocation.
+command-line arguments to the current @command{restic} invocation.
@end table
diff --git a/gnu/services/backup.scm b/gnu/services/backup.scm
index 4d8cf167f04..dc095593432 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-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)
@@ -129,7 +130,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} invocation."))
+command-line arguments to the current @command{restic} invocation."))
(define list-of-restic-backup-jobs?
(list-of restic-backup-job?))
@@ -139,71 +140,107 @@ (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" "list" "ls" "mount" "prune" "restore" "snapshots" "unlock"))
+
+(define (restic-backup-job->kv config)
(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
+ (verbose?
(if (restic-backup-job-verbose? config)
'("--verbose")
'())))
- (program-file
- "restic-backup-job.scm"
- #~(begin
- (use-modules (ice-9 popen)
- (ice-9 rdelim))
- (setenv "RESTIC_PASSWORD"
- (with-input-from-file #$password-file read-line))
+ #~(list #$name (list #$restic #$repository #$password-file
+ (list #$@verbose?) (list #$@extra-flags)))))
+
+(define (restic-program config)
+ #~(lambda* (action action-args job-restic repository password-file verbose? extra-flags)
+ (use-modules (ice-9 format)
+ (ice-9 popen)
+ (ice-9 rdelim))
+ ;; This can be extended later, i.e. to have a
+ ;; centrally defined restic package.
+ ;; See https://issues.guix.gnu.org/71639
+ (define restic job-restic)
+
+ (define command
+ `(,restic ,@verbose?
+ "-r" ,repository
+ ,@extra-flags
+ ,action ,@action-args))
+
+ (setenv "RESTIC_PASSWORD"
+ (with-input-from-file password-file read-line))
- (execlp #$restic #$restic #$@verbose
- "-r" #$repository
- #$@extra-flags
- "backup" #$@files)))))
+ (when (> (length verbose?) 0)
+ (format #t "Running~{ ~a~}~%" command))
-(define (restic-guix jobs)
+ (apply execlp `(,restic ,@command))))
+
+(define* (restic-guix config #:key (supported-actions
+ %restic-guix-supported-actions))
(program-file
"restic-guix"
#~(begin
(use-modules (ice-9 match)
(srfi srfi-1))
- (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 jobs
+ (list
+ #$@(map restic-backup-job->kv
+ (restic-backup-configuration-jobs config))))
+ (define names (map first jobs))
+ (define (get-job key)
+ (first
+ (filter-map
+ (match-lambda
+ ((k v)
+ (and (string=? key k) v)))
+ jobs)))
- (define (backup args)
- (define name (third args))
- (define program (get-program name))
- (execlp program program))
+ (define restic-exec
+ #$(restic-program config))
(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 ((job (get-job job-name))
+ (action-args
+ (if (> argc 3)
+ (take-right args (- argc 3))
+ '())))
+ (values job action-args)))
(define (main args)
(validate-args args)
(define action (second args))
- (match action
- ("backup"
- (backup args))
- (_
- (error (string-append "Unknown action: " action)))))
+ (define-values (job action-args) (validate-action-args action args))
+ (apply restic-exec `(,action ,action-args ,@job)))
(main (command-line)))))
@@ -217,6 +254,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))
@@ -242,7 +283,8 @@ (define (restic-backup-job->shepherd-service config)
;; backends require, such as rclone.
(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
@@ -261,11 +303,11 @@ (define (restic-backup-job->shepherd-service config)
without waiting for the scheduled time.")
(procedure #~trigger-timer)))))))
-(define (restic-guix-wrapper-package jobs)
+(define (restic-guix-wrapper-package config)
(package
(name "restic-backup-service-wrapper")
(version "0.0.0")
- (source (restic-guix jobs))
+ (source (restic-guix config))
(build-system copy-build-system)
(arguments
(list #:install-plan #~'(("./" "/bin"))))
@@ -284,10 +326,10 @@ (define restic-backup-service-profile
(define jobs (restic-backup-configuration-jobs config))
(if (> (length jobs) 0)
(list
- (restic-guix-wrapper-package jobs))
+ (restic-guix-wrapper-package config))
'())))
-(define (restic-backup-activation config)
+(define (restic-backup-service-activation config)
#~(for-each
(lambda (log-file)
(mkdir-p (dirname log-file)))
@@ -299,7 +341,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: 23b068c036223e70bdea9d7d579850a1cffc02a7
--
2.48.1
paul wrote 1 weeks ago
Re: Add restic commands to the restic-guix package
(address . 72803@debbugs.gnu.org)
d87fdaba-b639-4bd5-9352-52a96e4f08ce@autistici.org
Hi, I'm sending a v8 rebased on current master.


thank you for your work
Giacomo Leidi wrote 1 weeks ago
[PATCH v8] services: restic-backup: Add more restic commands to the restic-guix package.
(address . 72803@debbugs.gnu.org)(name . Giacomo Leidi)(address . goodoldpaul@autistici.org)
eacc927e82941220e5d4bd1f8737e7edbe5b712f.1741214687.git.goodoldpaul@autistici.org
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-program;
(restic-guix): allow for multiple actions.

* doc/guix.texi: Document it.

Change-Id: Ib2b5d74bebc51e35f1ae6e1aa32cedee0da59697
---
doc/guix.texi | 20 +++++-
gnu/services/backup.scm | 138 ++++++++++++++++++++++++++--------------
2 files changed, 108 insertions(+), 50 deletions(-)

Toggle diff (274 lines)
diff --git a/doc/guix.texi b/doc/guix.texi
index 6844470ce2f..e4c5c86e91f 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -43709,6 +43709,23 @@ Miscellaneous Services
sudo herd trigger remote-ftp
@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
@@ -43782,8 +43799,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}
-invocation.
+command-line arguments to the current @command{restic} invocation.
@end table
diff --git a/gnu/services/backup.scm b/gnu/services/backup.scm
index 4d8cf167f04..dc095593432 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-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)
@@ -129,7 +130,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} invocation."))
+command-line arguments to the current @command{restic} invocation."))
(define list-of-restic-backup-jobs?
(list-of restic-backup-job?))
@@ -139,71 +140,107 @@ (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" "list" "ls" "mount" "prune" "restore" "snapshots" "unlock"))
+
+(define (restic-backup-job->kv config)
(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
+ (verbose?
(if (restic-backup-job-verbose? config)
'("--verbose")
'())))
- (program-file
- "restic-backup-job.scm"
- #~(begin
- (use-modules (ice-9 popen)
- (ice-9 rdelim))
- (setenv "RESTIC_PASSWORD"
- (with-input-from-file #$password-file read-line))
+ #~(list #$name (list #$restic #$repository #$password-file
+ (list #$@verbose?) (list #$@extra-flags)))))
+
+(define (restic-program config)
+ #~(lambda* (action action-args job-restic repository password-file verbose? extra-flags)
+ (use-modules (ice-9 format)
+ (ice-9 popen)
+ (ice-9 rdelim))
+ ;; This can be extended later, i.e. to have a
+ ;; centrally defined restic package.
+ ;; See https://issues.guix.gnu.org/71639
+ (define restic job-restic)
+
+ (define command
+ `(,restic ,@verbose?
+ "-r" ,repository
+ ,@extra-flags
+ ,action ,@action-args))
+
+ (setenv "RESTIC_PASSWORD"
+ (with-input-from-file password-file read-line))
- (execlp #$restic #$restic #$@verbose
- "-r" #$repository
- #$@extra-flags
- "backup" #$@files)))))
+ (when (> (length verbose?) 0)
+ (format #t "Running~{ ~a~}~%" command))
-(define (restic-guix jobs)
+ (apply execlp `(,restic ,@command))))
+
+(define* (restic-guix config #:key (supported-actions
+ %restic-guix-supported-actions))
(program-file
"restic-guix"
#~(begin
(use-modules (ice-9 match)
(srfi srfi-1))
- (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 jobs
+ (list
+ #$@(map restic-backup-job->kv
+ (restic-backup-configuration-jobs config))))
+ (define names (map first jobs))
+ (define (get-job key)
+ (first
+ (filter-map
+ (match-lambda
+ ((k v)
+ (and (string=? key k) v)))
+ jobs)))
- (define (backup args)
- (define name (third args))
- (define program (get-program name))
- (execlp program program))
+ (define restic-exec
+ #$(restic-program config))
(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 ((job (get-job job-name))
+ (action-args
+ (if (> argc 3)
+ (take-right args (- argc 3))
+ '())))
+ (values job action-args)))
(define (main args)
(validate-args args)
(define action (second args))
- (match action
- ("backup"
- (backup args))
- (_
- (error (string-append "Unknown action: " action)))))
+ (define-values (job action-args) (validate-action-args action args))
+ (apply restic-exec `(,action ,action-args ,@job)))
(main (command-line)))))
@@ -217,6 +254,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))
@@ -242,7 +283,8 @@ (define (restic-backup-job->shepherd-service config)
;; backends require, such as rclone.
(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
@@ -261,11 +303,11 @@ (define (restic-backup-job->shepherd-service config)
without waiting for the scheduled time.")
(procedure #~trigger-timer)))))))
-(define (restic-guix-wrapper-package jobs)
+(define (restic-guix-wrapper-package config)
(package
(name "restic-backup-service-wrapper")
(version "0.0.0")
- (source (restic-guix jobs))
+ (source (restic-guix config))
(build-system copy-build-system)
(arguments
(list #:install-plan #~'(("./" "/bin"))))
@@ -284,10 +326,10 @@ (define restic-backup-service-profile
(define jobs (restic-backup-configuration-jobs config))
(if (> (length jobs) 0)
(list
- (restic-guix-wrapper-package jobs))
+ (restic-guix-wrapper-package config))
'())))
-(define (restic-backup-activation config)
+(define (restic-backup-service-activation config)
#~(for-each
(lambda (log-file)
(mkdir-p (dirname log-file)))
@@ -299,7 +341,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: 310adf4ce70cbb864859274fcc7842bd519bbddc
--
2.48.1
Ludovic Courtès wrote 3 days ago
Re: bug#72803: Add restic commands to the restic-guix package
(name . Giacomo Leidi)(address . goodoldpaul@autistici.org)(address . 72803@debbugs.gnu.org)(name . Maxim Cournoyer)(address . maxim.cournoyer@gmail.com)
87msdtq9gf.fsf_-_@gnu.org
Hi,

Giacomo Leidi <goodoldpaul@autistici.org> skribis:

Toggle quote (17 lines)
> 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-program;
> (restic-guix): allow for multiple actions.
>
> * doc/guix.texi: Document it.
>
> Change-Id: Ib2b5d74bebc51e35f1ae6e1aa32cedee0da59697
> +(define (restic-program config)
> + #~(lambda* (action action-args job-restic repository password-file verbose? extra-flags)
> + (use-modules (ice-9 format)
> + (ice-9 popen)
> + (ice-9 rdelim))

Please move ‘use-modules’ to the top-level; that’s the only guaranteed
way to use it.

Toggle quote (80 lines)
> +(define* (restic-guix config #:key (supported-actions
> + %restic-guix-supported-actions))
> (program-file
> "restic-guix"
> #~(begin
> (use-modules (ice-9 match)
> (srfi srfi-1))
>
> - (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 jobs
> + (list
> + #$@(map restic-backup-job->kv
> + (restic-backup-configuration-jobs config))))
> + (define names (map first jobs))
> + (define (get-job key)
> + (first
> + (filter-map
> + (match-lambda
> + ((k v)
> + (and (string=? key k) v)))
> + jobs)))
>
> - (define (backup args)
> - (define name (third args))
> - (define program (get-program name))
> - (execlp program program))
> + (define restic-exec
> + #$(restic-program config))
>
> (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 ((job (get-job job-name))
> + (action-args
> + (if (> argc 3)
> + (take-right args (- argc 3))
> + '())))
> + (values job action-args)))
>
> (define (main args)
> (validate-args args)
> (define action (second args))
> - (match action
> - ("backup"
> - (backup args))
> - (_
> - (error (string-append "Unknown action: " action)))))
> + (define-values (job action-args) (validate-action-args action args))
> + (apply restic-exec `(,action ,action-args ,@job)))

I see two issues here:

1. This is stepping on the toes of upstream: why are we providing a
non-trivial program like this downstream?

2. There are stylistic issues: use of ‘first’ & co. (info "(guix) Data
Types and Pattern Matching"), use of ‘error’ (it is too generic and
user-unfriendly), custom argument parsing procedure.

Thanks,
Ludo’.
paul wrote 3 days ago
(name . Ludovic Courtès)(address . ludo@gnu.org)(address . 72803@debbugs.gnu.org)(name . Maxim Cournoyer)(address . maxim.cournoyer@gmail.com)
783760a3-1fc0-4803-8ea6-009e49cf760a@autistici.org
Hi Ludo’ ,

first thanks a lot for your review,

On 3/10/25 14:52, Ludovic Courtès wrote:
Toggle quote (5 lines)
> I see two issues here:
>
> 1. This is stepping on the toes of upstream: why are we providing a
> non-trivial program like this downstream?

In my understanding this is the fundamental issue, which could be a
shipstopper. Please correct me if I'm wrong. We kind of are obviously
even if restic-guix is already in the master branch ( ) . In my opinion
the way forward should be: a. In this scenario we merge the current
72803 (after addressing your other comments) and we take this risk

b. In this scenario we remove completely the current incomplete
restic-guix command implementation from master, as it makes not much
sense to have it incomplete as it is right now.

I view scenario a and scenario b as mutually exclusive but I may be
missing some implication, what is your opinion on this?

Toggle quote (4 lines)
> 2. There are stylistic issues: use of ‘first’ & co. (info "(guix) Data
> Types and Pattern Matching"), use of ‘error’ (it is too generic and
> user-unfriendly), custom argument parsing procedure.

I will address these comments only if we decide to go forward with
scenario a.


Thank you so much for your work,


cheers

giacomo
Attachment: file
paul wrote 3 days ago
(name . Ludovic Courtès)(address . ludo@gnu.org)(address . 72803@debbugs.gnu.org)(name . Maxim Cournoyer)(address . maxim.cournoyer@gmail.com)
e348e09d-937e-4ab3-8d01-25a0a8c655d6@autistici.org
Ah I hit return too soon :/ Apologies for the noise. This is what I
wanted to add to my previous message:


... We kind of are obviously even if restic-guix is already in the
master branch (
), which suffers from many of the shortcomings you noticed in your
review, such as: use of ‘first & co., use of ‘error’, custom argument
parsing procedure. In my opinion ...

thank you again for your review,

giacomo
Attachment: file
Ludovic Courtès wrote 3 days ago
(name . paul)(address . goodoldpaul@autistici.org)(address . 72803@debbugs.gnu.org)(name . Maxim Cournoyer)(address . maxim.cournoyer@gmail.com)
87wmcwmumf.fsf@gnu.org
Hi,

paul <goodoldpaul@autistici.org> skribis:

Toggle quote (20 lines)
> On 3/10/25 14:52, Ludovic Courtès wrote:
>> I see two issues here:
>>
>> 1. This is stepping on the toes of upstream: why are we providing a
>> non-trivial program like this downstream?
>
> In my understanding this is the fundamental issue, which could be a
> shipstopper. Please correct me if I'm wrong. We kind of are obviously
> even if restic-guix is already in the master branch ( ) . In my
> opinion the way forward should be: a. In this scenario we merge the
> current 72803 (after addressing your other comments) and we take this
> risk
>
> b. In this scenario we remove completely the current incomplete
> restic-guix command implementation from master, as it makes not much
> sense to have it incomplete as it is right now.
>
> I view scenario a and scenario b as mutually exclusive but I may be
> missing some implication, what is your opinion on this?

I’m not a restic user so I’m not super qualified, but I think the
general direction should be to stick to our role of downstream users; if
we go too far in terms of tooling around the software, then that
suggests something’s missing from what upstream provides, and perhaps
more importantly that’s a maintenance burden on us.

From that perspective, I lean towards scenario b, but I trust you can
find the right middle ground.

WDYT?

Ludo’.
?
Your comment

Commenting via the web interface is currently disabled.

To comment on this conversation send an email to 72803@debbugs.gnu.org

To respond to this issue using the mumi CLI, first switch to it
mumi current 72803
Then, you may apply the latest patchset in this issue (with sign off)
mumi am -- -s
Or, compose a reply to this issue
mumi compose
Or, send patches to this issue
mumi send-email *.patch
You may also tag this issue. See list of standard tags. For example, to set the confirmed and easy tags
mumi command -t +confirmed -t +easy
Or, remove the moreinfo tag and set the help tag
mumi command -t -moreinfo -t +help