]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - modules/duply_backup/default.nix
Add alternate cloud storage for daily backups
[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
19 DUPL_PARAMS="$DUPL_PARAMS --full-if-older-than $MAX_FULLBKP_AGE "
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 }
90 ]) v.remotes) config.services.duplyBackup.profiles);
6a8252b1
IB
91
92 services.cron = {
93 enable = true;
94 systemCronJobs = let
95 backups = pkgs.writeScript "backups" ''
96 #!${pkgs.stdenv.shell}
97
5a61f6ad
IB
98 ${builtins.concatStringsSep "\n" (lib.flatten (lib.mapAttrsToList (k: v:
99 map (remote: [
6a8252b1 100 ''
5a61f6ad
IB
101 touch ${varDir}/${varName k remote}.log
102 ${pkgs.duply}/bin/duply ${config.secrets.location}/backup/${varName k remote}/ ${action} --force >> ${varDir}/${varName k remote}.log
103 [[ $? = 0 ]] || echo -e "Error when doing backup for ${varName k remote}, see above\n---------------------------------------" >&2
6a8252b1 104 ''
5a61f6ad
IB
105 ]) v.remotes
106 ) config.services.duplyBackup.profiles))}
6a8252b1
IB
107 '';
108 in
109 [
110 "0 2 * * * root ${backups}"
111 ];
112
113 };
114
56b07e8d
IB
115 security.pki.certificateFiles = [
116 (pkgs.fetchurl {
c29c32be
IB
117 url = "http://downloads.e.eriomem.net/eriomemca.pem";
118 sha256 = "1ixx4c6j3m26j8dp9a3dkvxc80v1nr5aqgmawwgs06bskasqkvvh";
56b07e8d 119 })
6a8252b1
IB
120 ];
121 };
122}