commit d3274367b68e112a193dd06b19b23ac5afd39ead
Author: Marc Weber <marco-oweber@gmx.de>
Date:   Sat Jun 25 18:53:31 2011 +0200

    rsnapshot module

diff --git a/modules/module-list.nix b/modules/module-list.nix
index ffc42eb..b9d5460 100644
--- a/modules/module-list.nix
+++ b/modules/module-list.nix
@@ -50,6 +50,7 @@
   ./services/backup/hydra-mirror.nix
   ./services/backup/mysql-backup.nix
   ./services/backup/postgresql-backup.nix
+  ./services/backup/rsnapshot.nix
   ./services/backup/sitecopy-backup.nix
   ./services/databases/4store-endpoint.nix
   ./services/databases/4store.nix
diff --git a/modules/services/backup/rsnapshot.nix b/modules/services/backup/rsnapshot.nix
new file mode 100644
index 0000000..d9baedd
--- /dev/null
+++ b/modules/services/backup/rsnapshot.nix
@@ -0,0 +1,151 @@
+{ config, pkgs, ... }:
+
+# NixOS configuration for pkgs.rsnapshot
+
+with pkgs.lib;
+
+let
+  inherit (pkgs) coreutils;
+  inherit (builtins) head tail;
+
+  cfg = config.services.rsnapshot;
+
+  ### utility functions
+  # could be using include_conf to share common configuration
+  # mind the tabs!
+  writeConfig = name: amend: pkgs.writeText "rsnapshot-${name}.conf" ''
+    # == nixos default config lines
+    config_version	1.2
+    # all backups will be stored here:
+
+    no_create_root	1
+    cmd_cp	${coreutils}/bin/cp
+    cmd_rm	${coreutils}/bin/rm
+    cmd_rsync	${pkgs.rsync}/bin/rsync
+    cmd_logger	${pkgs.inetutils}/bin/logger
+    cmd_rsnapshot_diff	${pkgs.rsnapshot}/bin/rsnapshot-diff
+
+    # == user lines:
+    ${amend}
+  '';
+
+  processConfig = name: attr:
+    let 
+      retains = (concatMapStrings (i: "interval\t${i.name}\t${builtins.toString i.retain}\n") attr.intervals)
+              + "logfile\t/var/log/rsnapshot-${name}.log\n";
+      config = writeConfig name "${attr.configLines}\n${retains}\n";
+
+
+      # test config
+      configTest = pkgs.runCommand ("rsnapshot-configtest-" + name + ".conf") {}
+        ''
+          ${pkgs.rsnapshot}/bin/rsnapshot -c ${config} configtest
+          echo ok > $out
+        '';
+
+      # dummy derivation forcing evalution of configTest
+      config2 = pkgs.runCommand "${name}-checked.conf" {inherit configTest;} "cp ${config} $out;";
+
+    in 
+      # it probably is a bad idea to run as root (TODO)
+      map (i: "${i.cron} root ${pkgs.rsnapshot}/bin/rsnapshot -c ${config2} ${i.name}") attr.intervals;
+
+  ### option types
+
+  rsnapshotConfigType = {
+    intervals = mkOption {
+      # type = types.listOf (mkOption { });
+      example = [
+        {name = "weekly"; retain= 4; cron = "30 0 1,8,15,22 * *"; }
+        {name = "monthly"; retain = 12; cron = "0 0 1, * * "; }
+       ];
+      description = ''
+        when to run this job and how many snapshots to keep.
+        The setting is a list of intervals along with cronjob stamps.
+
+        [["weekly" 4 "30 0 1,8,15,22 * *"], ["monthly" 12 "0 0 1, * * "]]
+        would rotate 4 backups weekly using the latest weekly as source for the
+        monthly rotation which must be started before the weekly. Thus the 30min.
+      '';
+    };
+    configLines = mkOption {
+
+      description = ''
+        The packup configuration inculding lockfile, snapshot_root, backup and
+        backup_script lines.
+        retain (previously interval) lines will be created for you based on the
+        intervals setting. Note: you must use a tab to separate key value pairs!
+      '';
+
+      example = ''
+        verbose		2
+        loglevel	3
+
+        lockfile	/var/run/rsnapshot.pid
+
+        # snapshot_root /auto-dev/sdb2
+
+        #one_fs		0
+        #include	???
+        #exclude	???
+        #include_file	/path/to/include/file
+        #exclude_file	/path/to/exclude/file
+        link_dest	1
+        use_lazy_deletes	1
+        # cmd_preexec   /bin/sh -c '{date; echo started; } >> /auto-dev/sdb2/backup-log
+        # cmd_postexec  /bin/sh -c '{date; echo stopped; } >> /auto-dev/sdb2/backup-log
+
+        # backup a folder
+        #backup	/foo/bar/	localhost/	one_fs=1, rsync_short_args=-urltvpog
+
+        # backup a database or such:
+        #backup_script		/usr/local/bin/backup_pgsql.sh	localhost/postgres/
+
+        # more examples and info can be found in config sample distributed with rsnapshot
+      '';
+    };
+  };
+
+
+  ### options
+  options = {
+  
+    services.rsnapshot = {
+
+      enable = mkOption {
+        default = false;
+        description = ''
+          Whether to enable rsnapshot backups. rsnapshot is a perl script which
+          uses rsync to backup existing or directories being created temporarely (db dumps)
+        '';
+      };
+
+      # attr namse should be used as description of the backup configuration task.
+      configs = mkOption {
+        default = {};
+        description = ''
+          This option defines the system jobs started and managed by the
+          Upstart daemon.
+        '';
+        type = types.attrsOf types.optionSet;
+        options = rsnapshotConfigType;
+
+      };
+
+    };
+
+  };
+
+
+in
+
+{
+
+  inherit options;
+
+  config = mkIf cfg.enable {
+    services.cron.systemCronJobs = concatLists( attrValues ( mapAttrs processConfig cfg.configs));
+  };
+  
+}
+
