[bug#76169,v7] home: Add home-restic-backup service.

Message ID db3dd3414e09ae715bdeb84efe3beef39939c5c6.1745886178.git.goodoldpaul@autistici.org
State New
Headers
Series [bug#76169,v7] home: Add home-restic-backup service. |

Commit Message

Giacomo Leidi April 29, 2025, 12:22 a.m. UTC
  * gnu/services/backup.scm (restic-backup-configuration): Reimplement
with (guix records);
(restic-backup-job-{logfile,command,requirement,modules}): Add new
procedures and add support for Guix Home environments;
(restic-backup-job->shepherd-service): Add support for Guix Home
environments;
(restic-backup-service-activation): Drop procedure as now the Shepherd
takes care of creating directories for timers logs.
* gnu/home/services/backup.scm: New file.
* gnu/local.mk: Add this.
* doc/guix.texi: Document this.

Change-Id: Ied1c0a5756b715fba176a0e42ea154246089e6be
---
 doc/guix.texi                | 73 ++++++++++++++++++++++++++
 gnu/home/services/backup.scm | 38 ++++++++++++++
 gnu/local.mk                 |  1 +
 gnu/services/backup.scm      | 99 ++++++++++++++++++++++--------------
 4 files changed, 173 insertions(+), 38 deletions(-)
 create mode 100644 gnu/home/services/backup.scm


base-commit: 25dac960ce99c32cf53cdf010351b7355f072fa1
  

Patch

diff --git a/doc/guix.texi b/doc/guix.texi
index 7b418a40892..84e2c9f0970 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -463,6 +463,7 @@  Top
 * GPG: GNU Privacy Guard.       Setting up GPG and related tools.
 * Desktop: Desktop Home Services.  Services for graphical environments.
 * Guix: Guix Home Services.     Services for Guix.
+* Backup: Backup Home Services.   Services for backing up User's files.
 * Fonts: Fonts Home Services.   Services for managing User's fonts.
 * Sound: Sound Home Services.   Dealing with audio.
 * Mail: Mail Home Services.     Services for managing mail.
@@ -48677,6 +48678,7 @@  Home Services
 * GPG: GNU Privacy Guard.       Setting up GPG and related tools.
 * Desktop: Desktop Home Services.  Services for graphical environments.
 * Guix: Guix Home Services.     Services for Guix.
+* Backup: Backup Home Services.   Services for backing up User's files.
 * Fonts: Fonts Home Services.   Services for managing User's fonts.
 * Sound: Sound Home Services.   Dealing with audio.
 * Mail: Mail Home Services.     Services for managing mail.
@@ -50267,6 +50269,77 @@  Guix Home Services
 @end lisp
 @end defvar
 
+@node Backup Home Services
+@subsection Backup Services
+
+The @code{(gnu home services backup)} module offers services for backing up
+file system trees.  For now, it provides the @code{home-restic-backup-service-type}.
+
+With @code{home-restic-backup-service-type}, you can periodically back up
+directories and files with @uref{https://restic.net/, Restic}, which
+supports end-to-end encryption and deduplication.  Consider the
+following configuration:
+
+@lisp
+(use-modules (gnu home services backup) ;for 'restic-backup-job', 'home-restic-backup-service-type'
+             (gnu packages sync)        ;for 'rclone'
+             @dots{})
+
+(home-environment
+
+  (packages (list rclone    ;for use by restic
+                  @dots{}))
+  (services
+    (list
+      @dots{}
+      (simple-service 'backup-jobs
+                      home-restic-backup-service-type
+                      (list (restic-backup-job
+                              (name "remote-ftp")
+                              (repository "rclone:remote-ftp:backup/restic")
+                              (password-file "/home/alice/.restic")
+                              ;; Every day at 23.
+                              (schedule "0 23 * * *")
+                              (files '("/home/alice/.restic"
+                                       "/home/alice/.config/rclone"
+                                       "/home/alice/Pictures"))))))))
+@end lisp
+
+In general it is preferrable to extend the @code{home-restic-backup-service-type},
+as shown in the example above.  This is because it takes care of wrapping everything
+with @code{for-home}, which enables the @code{home-restic-backup-service-type} and
+@code{restic-backup-service-type} to share the same codebase.
+
+For a custom configuration, wrap your @code{restic-backup-configuration} in
+@code{for-home}, as in this example:
+
+@lisp
+(use-modules (gnu services)             ;for 'for-home'
+             (gnu services backup)      ;for 'restic-backup-job' and 'restic-backup-configuration'
+             (gnu home services backup) ;for 'home-restic-backup-service-type'
+             (gnu packages sync)        ;for 'rclone'
+             @dots{})
+
+(home-environment
+
+  (packages (list rclone    ;for use by restic
+                  @dots{}))
+  (services
+    (list
+      @dots{}
+      (service home-restic-backup-service-type
+               (for-home
+                (restic-backup-configuration
+                 (jobs (list @dots{}))))))))
+@end lisp
+
+You can refer to @pxref{Miscellaneous Services,
+@code{restic-backup-service-type}} for details about
+@code{restic-backup-configuration} and @code{restic-backup-job}.
+The only difference is that the @code{home-restic-backup-service-type}
+will ignore the @code{user} and @code{group} field of
+@code{restic-backup-job}.
+
 @node Fonts Home Services
 @subsection Fonts Home Services
 
diff --git a/gnu/home/services/backup.scm b/gnu/home/services/backup.scm
new file mode 100644
index 00000000000..ac977f835b6
--- /dev/null
+++ b/gnu/home/services/backup.scm
@@ -0,0 +1,38 @@ 
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2025 Giacomo Leidi <goodoldpaul@autistici.org>
+;;;
+;;; This file is part of GNU Guix.
+;;;
+;;; GNU Guix is free software; you can redistribute it and/or modify it
+;;; under the terms of the GNU General Public License as published by
+;;; the Free Software Foundation; either version 3 of the License, or (at
+;;; your option) any later version.
+;;;
+;;; GNU Guix is distributed in the hope that it will be useful, but
+;;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;;; GNU General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
+
+(define-module (gnu home services backup)
+  #:use-module (gnu services)
+  #:use-module (gnu services backup)
+  #:use-module (gnu home services)
+  #:use-module (gnu home services shepherd)
+  #:export (home-restic-backup-service-type)
+  #:re-export (restic-backup-configuration
+               restic-backup-job))
+
+(define home-restic-backup-service-type
+  (service-type
+   (inherit (system->home-service-type restic-backup-service-type))
+   (extend
+    (lambda (config jobs)
+      (for-home
+       (restic-backup-configuration
+        (inherit config)
+        (jobs (append (restic-backup-configuration-jobs config)
+                      jobs))))))
+   (default-value (for-home (restic-backup-configuration)))))
diff --git a/gnu/local.mk b/gnu/local.mk
index cc57222a91b..b57b9fb4fb7 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -103,6 +103,7 @@  GNU_SYSTEM_MODULES =				\
   %D%/home.scm					\
   %D%/home/services.scm			\
   %D%/home/services/admin.scm			\
+  %D%/home/services/backup.scm			\
   %D%/home/services/desktop.scm			\
   %D%/home/services/dict.scm			\
   %D%/home/services/dotfiles.scm		\
diff --git a/gnu/services/backup.scm b/gnu/services/backup.scm
index 4fff815e168..3cc199a23bd 100644
--- a/gnu/services/backup.scm
+++ b/gnu/services/backup.scm
@@ -28,6 +28,7 @@  (define-module (gnu services backup)
                 #:prefix license:)
   #:use-module (guix modules)
   #:use-module (guix packages)
+  #:use-module (guix records)
   #:use-module (srfi srfi-1)
   #:export (restic-backup-job
             restic-backup-job?
@@ -51,7 +52,6 @@  (define-module (gnu services backup)
 
             restic-backup-configuration
             restic-backup-configuration?
-            restic-backup-configuration-fields
             restic-backup-configuration-jobs
 
             restic-program
@@ -134,13 +134,18 @@  (define-configuration/no-serialization restic-backup-job
    "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."))
 
-(define list-of-restic-backup-jobs?
-  (list-of restic-backup-job?))
+;; (for-home (restic-backup-configuration ...)) is not able to replace for-home? with #t,
+;; pk prints #f. Once for-home will be able to work with (gnu services configuration) the
+;; record can be migrated back to define-configuration.
+(define-record-type* <restic-backup-configuration>
+  restic-backup-configuration
+  make-restic-backup-configuration
+  restic-backup-configuration?
+  this-restic-backup-configuration
 
-(define-configuration/no-serialization restic-backup-configuration
-  (jobs
-   (list-of-restic-backup-jobs '())
-   "The list of backup jobs for the current system."))
+  (jobs  restic-backup-configuration-jobs  (default '()))     ; list of restic-backup-job
+  (home-service? restic-backup-configuration-home-service?
+                 (default for-home?) (innate)))
 
 (define (lower-restic-backup-job config)
   (let ((restic
@@ -192,31 +197,41 @@  (define (restic-backup config)
 
        (apply restic-exec `("backup" ,@job)))))
 
-(define (restic-backup-job-log-file job)
+(define* (restic-backup-job-log-file job #:key (home-service? #f))
   (let ((name (restic-backup-job-name job))
         (log-file (restic-backup-job-log-file job)))
     (if (maybe-value-set? log-file)
         log-file
-        (string-append "/var/log/restic-backup/" name ".log"))))
+        (if home-service?
+            #~(string-append %user-log-dir "/restic-backup/" #$name ".log")
+            (string-append "/var/log/restic-backup/" name ".log")))))
 
-(define (restic-backup-job-command config)
-  ;; We go through bash, instead of executing
-  ;; restic-guix directly, because the login shell
-  ;; gives us the correct user environment that some
-  ;; backends require, such as rclone.
-  #~(list
-     (string-append #$bash-minimal "/bin/bash")
-     "-l" "-c"
-     (string-join (list #$(restic-backup config))
-                  " ")))
+(define* (restic-backup-job-command config #:key (home-service? #f))
+  (if home-service?
+      ;; Home Shepherd sets the right environment, so no
+      ;; bash is needed
+      #~(list #$(restic-backup config))
+      ;; We go through bash, instead of executing
+      ;; restic-guix directly, because the login shell
+      ;; gives us the correct user environment that some
+      ;; backends require, such as rclone.
+      #~(list
+         (string-append #$bash-minimal "/bin/bash")
+         "-l" "-c" #$(restic-backup config))))
 
-(define (restic-backup-job-requirement requirement)
-  (append '(user-processes file-systems) requirement))
+(define* (restic-backup-job-requirement requirement #:key (home-service? #f))
+  (if home-service?
+      requirement
+      (append '(user-processes file-systems) requirement)))
 
-(define (restic-backup-job-modules)
- `((shepherd service timer)))
+(define* (restic-backup-job-modules #:key (home-service? #f))
+ `((shepherd service timer)
+   ,@(if home-service?
+         ;;for %user-log-dir
+         '((shepherd support))
+         '())))
 
-(define (restic-backup-job->shepherd-service config)
+(define* (restic-backup-job->shepherd-service config #:key (home-service? #f))
   (let ((schedule (restic-backup-job-schedule config))
         (name (restic-backup-job-name config))
         (files (restic-backup-job-files config))
@@ -224,28 +239,34 @@  (define (restic-backup-job->shepherd-service config)
         (group (restic-backup-job-group config))
         (max-duration (restic-backup-job-max-duration config))
         (wait-for-termination? (restic-backup-job-wait-for-termination? config))
-        (log-file (restic-backup-job-log-file config))
+        (log-file (restic-backup-job-log-file
+                   config #:home-service? home-service?))
         (requirement
          (restic-backup-job-requirement
-          (restic-backup-job-requirement config))))
+          (restic-backup-job-requirement config)
+          #:home-service? home-service?)))
     (shepherd-service (provision `(,(string->symbol name)))
                       (requirement requirement)
                       (documentation
                        "Run restic backed backups on a regular basis.")
-                      (modules (restic-backup-job-modules))
+                      (modules (restic-backup-job-modules
+                                #:home-service? home-service?))
                       (start
                        #~(make-timer-constructor
                           (if (string? #$schedule)
                               (cron-string->calendar-event #$schedule)
                               #$schedule)
                           (command
-                           #$(restic-backup-job-command config)
-                           #:user #$user
-                           #:group #$group
-                           #:environment-variables
-                           (list
-                            (string-append
-                             "HOME=" (passwd:dir (getpwnam #$user)))))
+                           #$(restic-backup-job-command
+                              config #:home-service? home-service?)
+                           #$@(if home-service? '() (list #:user user))
+                           #$@(if home-service? '() (list #:group group))
+                           #$@(if home-service? '()
+                                  (list
+                                   #:environment-variables
+                                   #~(list
+                                      (string-append
+                                       "HOME=" (passwd:dir (getpwnam #$user)))))))
                           #:log-file #$log-file
                           #:wait-for-termination? #$wait-for-termination?
                           #:max-duration #$(and (maybe-value-set? max-duration)
@@ -263,10 +284,12 @@  (define restic-backup-service-type
                 (extensions
                  (list
                   (service-extension shepherd-root-service-type
-                                     (lambda (config)
-                                       (map restic-backup-job->shepherd-service
-                                            (restic-backup-configuration-jobs
-                                             config))))))
+                                     (match-record-lambda <restic-backup-configuration>
+                                         (jobs home-service?)
+                                       (map (lambda (job)
+                                              (restic-backup-job->shepherd-service
+                                               job #:home-service? home-service?))
+                                            jobs)))))
                 (compose concatenate)
                 (extend
                  (lambda (config jobs)