]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - modules/duply_backup/default.nix
Move secrets to flakes
[perso/Immae/Config/Nix.git] / modules / duply_backup / default.nix
CommitLineData
5a61f6ad 1{ lib, pkgs, config, name, ... }:
6a8252b1
IB
2
3let
ab8f306d 4 cfg = config.myEnv.backup;
6a8252b1 5 varDir = "/var/lib/duply";
5a61f6ad 6 duplyProfile = profile: remote: prefix: ''
6a8252b1 7 GPG_PW="${cfg.password}"
5a61f6ad
IB
8 TARGET="${cfg.remotes.${remote}.remote profile.bucket}${prefix}"
9 export AWS_ACCESS_KEY_ID="${cfg.remotes.${remote}.accessKeyId}"
10 export AWS_SECRET_ACCESS_KEY="${cfg.remotes.${remote}.secretAccessKey}"
6a8252b1
IB
11 SOURCE="${profile.rootDir}"
12 FILENAME=".duplicity-ignore"
13 DUPL_PARAMS="$DUPL_PARAMS --exclude-if-present '$FILENAME'"
14 VERBOSITY=4
15 ARCH_DIR="${varDir}/caches"
16
17 # Do a full backup after 1 month
18 MAX_FULLBKP_AGE=1M
64517bbc 19 DUPL_PARAMS="$DUPL_PARAMS --allow-source-mismatch --exclude-other-filesystems --full-if-older-than $MAX_FULLBKP_AGE "
6a8252b1
IB
20 # Backups older than 2months are deleted
21 MAX_AGE=2M
22 # Keep 2 full backups
23 MAX_FULL_BACKUPS=2
24 MAX_FULLS_WITH_INCRS=2
25 '';
26 action = "bkp_purge_purgeFull_purgeIncr";
5a61f6ad
IB
27 varName = k: remoteName:
28 if remoteName == "eriomem" then k else remoteName + "_" + k;
6a8252b1
IB
29in
30{
31 options = {
d2e703c5 32 services.duplyBackup.enable = lib.mkOption {
6a8252b1
IB
33 type = lib.types.bool;
34 default = false;
35 description = ''
36 Whether to enable remote backups.
37 '';
38 };
d2e703c5 39 services.duplyBackup.profiles = lib.mkOption {
6a8252b1
IB
40 type = lib.types.attrsOf (lib.types.submodule {
41 options = {
42 rootDir = lib.mkOption {
43 type = lib.types.path;
44 description = ''
45 Path to backup
46 '';
47 };
5a61f6ad
IB
48 bucket = lib.mkOption {
49 type = lib.types.str;
50 default = "immae-${name}";
51 description = ''
52 Bucket to use
53 '';
54 };
55 remotes = lib.mkOption {
56 type = lib.types.listOf lib.types.str;
57 default = ["eriomem"];
58 description = ''
59 Remotes to use for backup
60 '';
61 };
6a8252b1
IB
62 excludeFile = lib.mkOption {
63 type = lib.types.lines;
64 default = "";
65 description = ''
66 Content to put in exclude file
67 '';
68 };
69 };
70 });
71 };
72 };
73
d2e703c5 74 config = lib.mkIf config.services.duplyBackup.enable {
6a8252b1
IB
75 system.activationScripts.backup = ''
76 install -m 0700 -o root -g root -d ${varDir} ${varDir}/caches
77 '';
5a61f6ad
IB
78 secrets.keys = lib.flatten (lib.mapAttrsToList (k: v:
79 map (remote: [
80 {
81 permissions = "0400";
82 dest = "backup/${varName k remote}/conf";
83 text = duplyProfile v remote "${k}/";
84 }
85 {
86 permissions = "0400";
87 dest = "backup/${varName k remote}/exclude";
88 text = v.excludeFile;
89 }
da30ae4f
IB
90 {
91 permissions = "0500";
92 dest = "backup/${varName k remote}";
93 isDir = true;
94 }
5a61f6ad 95 ]) v.remotes) config.services.duplyBackup.profiles);
6a8252b1
IB
96
97 services.cron = {
98 enable = true;
99 systemCronJobs = let
100 backups = pkgs.writeScript "backups" ''
101 #!${pkgs.stdenv.shell}
102
5a61f6ad
IB
103 ${builtins.concatStringsSep "\n" (lib.flatten (lib.mapAttrsToList (k: v:
104 map (remote: [
6a8252b1 105 ''
5a61f6ad 106 touch ${varDir}/${varName k remote}.log
da30ae4f 107 ${pkgs.duply}/bin/duply ${config.secrets.fullPaths."backup/${varName k remote}"}/ ${action} --force >> ${varDir}/${varName k remote}.log
5a61f6ad 108 [[ $? = 0 ]] || echo -e "Error when doing backup for ${varName k remote}, see above\n---------------------------------------" >&2
6a8252b1 109 ''
5a61f6ad
IB
110 ]) v.remotes
111 ) config.services.duplyBackup.profiles))}
6a8252b1
IB
112 '';
113 in
114 [
115 "0 2 * * * root ${backups}"
116 ];
117
118 };
119
56b07e8d
IB
120 security.pki.certificateFiles = [
121 (pkgs.fetchurl {
c29c32be
IB
122 url = "http://downloads.e.eriomem.net/eriomemca.pem";
123 sha256 = "1ixx4c6j3m26j8dp9a3dkvxc80v1nr5aqgmawwgs06bskasqkvvh";
56b07e8d 124 })
6a8252b1
IB
125 ];
126 };
127}