about summary refs log tree commit diff
path: root/pkgs/profpatsch/backup/default.nix
blob: f817354e1c25a5d98cc2c8e558a3068bce11fa66 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
{ pkgs, getBins, writeExecline, backtick }:

let

  bins = getBins pkgs.duplicity [ "duplicity" ]
      // getBins pkgs.pass [ "pass" ]
      // getBins pkgs.coreutils [ "printf" "echo" ];

  gpgKeyId = "4ACFD7592710266E18CEBB28C5CFD08B22247CDF";

  fetchSecretIntoEnv = writeExecline "fetch-secret-into-env" { readNArgs = 2; } [
    "backtick" "-in" "$1" [
      bins.pass "show" "$2"
    ]
    "$@"
  ];

  debugExec = msg: writeExecline "debug-exec" {} [
    "if" [
      "fdmove" "-c" "1" "2"
      "if" [ bins.printf "%s: " msg ]
      "if" [ bins.echo "$@" ]
    ]
    "$@"
  ];

  # TODO: create ncdu script by removing trailing slashes
  # from excludes

  exclude-home-dirs = [
    # archived not backupped
    "Downloads/"
    "Music/"
    "Documents/"
    "Dropbox/"
    "Pictures/"
    "videos/"
    "games/"
    # local tmp dir
    "tmp/"
    # big uninteresting stuff
    ".cache/"
    ".local/share/Steam/"
    ".local/share/Trash/"
    ".config/chromium/Default/Service?Worker/"
    ".config/chromium/Default/IndexedDB/"
    ".config/chromium/Default/Local?Storage/"
    ".config/chromium/Default/Application?Cache/"
    ".stack/"
    ".cargo/"
    ".mozilla/firefox/*.default/storage/"
    ".cabal/"
    ".go/"
    ".vagrant.d/"
    ".minecraft/"
    ".npm/"
    ".gem/"
    # consistently updating caches
    ".Mail/.notmuch/xapian/"
    # tmp stuff
    "Mail-bak/"
  ];

  exclude-code-build-dirs = [
    ".stack-work/"
    "target/"
    "node_modules/"
    "dist/"
  ];

  commonOptions = root:
    pkgs.lib.concatMap (e: [ "--exclude" "${root}/${e}" ]) exclude-home-dirs ++
    pkgs.lib.concatMap (e: [ "--exclude" "${root}/kot/**/${e}" ]) exclude-code-build-dirs ++ [
    # "--dry-run"
    "--progress"
    "--verbosity" "debug"
    "--asynchronous-upload"
    "--full-if-older-than" "60D"
    "--num-retries" "3"
    "--use-agent"
    # TODO "--log-fd"
  ];

  callDuplicity = name: argv: writeExecline name {} ([
    # used by duplicity for all kinds of backends
    fetchSecretIntoEnv "FTP_PASSWORD" "backups/backblaze.com/application-keys/profpatsch-restore/applicationKey"
    (debugExec "duplicity call")
    bins.duplicity
  ] ++ argv);

  duplicity-verify = { name, local, write, read }: callDuplicity "duplicity-verify-${name}"
    ([ "verify" ]
    ++ (commonOptions local) ++ [
      "--name" name
      read
      local
    ]);

  duplicity-restore = { name, local, write, read }: callDuplicity "duplicity-restore-${name}"
    ([ "restore" ]
    ++ (commonOptions local) ++ [
      "--name" name
      # extra flags
      "$@"
      read
      local
    ]);

  duplicity-list = { name, local, write, read }: callDuplicity "duplicity-list-${name}"
    ([ "list-current-files" ]
    ++ (commonOptions local) ++ [
      "--name" name
      read
    ]);

  duplicity-incremental = { name, local, write, read }: callDuplicity "duplicity-incremental-${name}"
    ([ "incremental" ]
    ++ (commonOptions local) ++ [
      "--encrypt-sign-key" gpgKeyId
      "--name" name
      local
      write
    ]);

  home = {
    name = "home";
    local = "/home/philip";
    write = "b2://000efe88f7148a00000000001@profpatsch-main-backup/home";
    read = "b2://000efe88f7148a00000000004@profpatsch-main-backup/home";
  };

  legosi = {
    name = "legosi-root";
    local = "/home/philip/tmp/legosi-root";
    write = "b2://000efe88f7148a00000000003@profpatsch-legosi/";
    read = "b2://000efe88f7148a00000000004@profpatsch-legosi/";
  };

  incremental = duplicity-incremental home;
  verify = duplicity-verify home;
  verify-legosi = duplicity-verify legosi;
  restore-legosi = duplicity-restore legosi;
  list-legosi = duplicity-list legosi;

in {
  inherit
    incremental
    verify
    verify-legosi
    restore-legosi
    list-legosi
    ;
}