1 { lib, pkgs, config, ... }:
3 partModule = lib.types.submodule {
5 remote_folder = lib.mkOption {
11 exclude_from = lib.mkOption {
12 type = lib.types.listOf lib.types.path;
15 Paths to exclude from the backup
18 files_from = lib.mkOption {
19 type = lib.types.listOf lib.types.path;
22 Paths to take for the backup
23 (if empty: whole folder minus exclude_from)
27 type = lib.types.nullOr lib.types.str;
30 additional arguments for rsync
35 profileModule = lib.types.submodule {
41 Number of backups to keep
44 login = lib.mkOption {
63 host_key = lib.mkOption {
66 Host key to use as known host
69 host_key_type = lib.mkOption {
75 parts = lib.mkOption {
76 type = lib.types.attrsOf partModule;
78 folders to backup in the host
83 cfg = config.services.rsyncBackup;
85 ssh_key = config.secrets.fullPaths."rsync_backup/identity";
88 #!${pkgs.stdenv.shell}
94 if [ -s "$TMP_STDERR" ]; then
97 rm -f $TMP_STDERR $EXCL_FROM $FILES_FROM
102 exec 2> "$TMP_STDERR"
108 backup_profile = name: profile: builtins.concatStringsSep "\n" (
109 [(backup_profile_head name profile)]
110 ++ lib.mapAttrsToList (backup_part name) profile.parts
111 ++ [(backup_profile_tail name profile)]);
113 backup_profile_head = name: profile: ''
115 PORT="${profile.port}"
116 DEST="${profile.login}@${profile.host}"
117 BASE="${cfg.mountpoint}/${name}"
118 OLD_BAK_BASE=$BASE/older/j
119 BAK_BASE=''${OLD_BAK_BASE}0
120 RSYNC_OUTPUT=$BASE/rsync_output
121 NBR=${builtins.toString profile.keep}
124 -o PreferredAuthentications=publickey \
125 -o StrictHostKeyChecking=yes \
126 -o ClearAllForwardings=yes \
127 -o UserKnownHostsFile=/dev/null \
132 echo "Fichier de verrouillage backup sur $DEST ou impossible de se connecter" >&2
136 rm -rf ''${OLD_BAK_BASE}''${NBR}
137 for j in `seq -w $(($NBR-1)) -1 0`; do
138 [ ! -d ''${OLD_BAK_BASE}$j ] && continue
139 mv ''${OLD_BAK_BASE}$j ''${OLD_BAK_BASE}$(($j+1))
142 mv $RSYNC_OUTPUT $BAK_BASE
145 if [ "$skip" != "$DEST" ]; then
148 backup_profile_tail = name: profile: ''
149 ssh -o UserKnownHostsFile=/dev/null -o CheckHostIP=no -i ${ssh_key} -p $PORT $DEST sh -c "date > .cache/last_backup"
150 fi # [ "$skip" != "$DEST" ]
151 ##### End ${name} #####
154 backup_part = profile_name: part_name: part: ''
155 ### ${profile_name} ${part_name} ###
157 REMOTE="${part.remote_folder}"
159 if [ ! -d "$BASE/$LOCAL" ]; then
163 cat > $EXCL_FROM <<EOF
164 ${builtins.concatStringsSep "\n" part.exclude_from}
166 cat > $FILES_FROM <<EOF
167 ${builtins.concatStringsSep "\n" part.files_from}
170 OUT=$RSYNC_OUTPUT/$LOCAL
171 ${pkgs.rsync}/bin/rsync --new-compress -XAavbr --fake-super -e "ssh -o UserKnownHostsFile=/dev/null -o CheckHostIP=no -i ${ssh_key} -p $PORT" --numeric-ids --delete \
172 --backup-dir=$BAK_BASE/$LOCAL \${
173 lib.optionalString (part.args != null) "\n ${part.args} \\"}${
174 lib.optionalString (builtins.length part.exclude_from > 0) "\n --exclude-from=$EXCL_FROM \\"}${
175 lib.optionalString (builtins.length part.files_from > 0) "\n --files-from=$FILES_FROM \\"}
176 $DEST:$REMOTE . > $OUT || true
177 ### End ${profile_name} ${part_name} ###
181 options.services.rsyncBackup = {
182 mountpoint = lib.mkOption {
183 type = lib.types.path;
184 description = "Path to the base folder for backups";
186 profiles = lib.mkOption {
187 type = lib.types.attrsOf profileModule;
193 ssh_key_public = lib.mkOption {
194 type = lib.types.str;
195 description = "Public key for the backup";
197 ssh_key_private = lib.mkOption {
198 type = lib.types.str;
199 description = "Private key for the backup";
203 config = lib.mkIf (builtins.length (builtins.attrNames cfg.profiles) > 0) {
204 # FIXME: monitoring to check that backup is less than 14h old
205 users.users.backup = {
207 uid = config.ids.uids.backup;
209 extraGroups = [ "keys" ];
212 users.groups.backup = {
213 gid = config.ids.gids.backup;
216 services.cron.systemCronJobs = let
217 backup = pkgs.writeScript "backup.sh" (builtins.concatStringsSep "\n" ([
219 ] ++ lib.mapAttrsToList backup_profile cfg.profiles));
222 25 3,15 * * * backup ${backup}
226 programs.ssh.knownHosts = lib.attrsets.mapAttrs' (name: profile: lib.attrsets.nameValuePair name {
227 hostNames = [ profile.host ];
228 publicKey = "${profile.host_key_type} ${profile.host_key}";
231 system.activationScripts.rsyncBackup = {
233 text = builtins.concatStringsSep "\n" (map (v: ''
234 install -m 0700 -o backup -g backup -d ${cfg.mountpoint}/${v} ${cfg.mountpoint}/${v}/older ${cfg.mountpoint}/${v}/rsync_output
235 '') (builtins.attrNames cfg.profiles)
241 dest = "rsync_backup/identity";
244 permissions = "0400";
245 text = cfg.ssh_key_private;
248 dest = "rsync_backup/identity.pub";
251 permissions = "0444";
252 text = cfg.ssh_key_public;