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
{ depot, pkgs, ... }:

let
  inherit (depot.nix) writeExecline getBins;
  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 "$@" ]
    ]
    "$@"
  ];

  exclude-home-dirs = [
    "Downloads/" "Music/" "Documents/" "Dropbox/" "Pictures/" "videos/"
    "games/" "tmp/" ".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/"
    "Android/" ".config/Code/" ".stack/" ".cargo/"
    ".mozilla/firefox/*.default/storage/"
    ".cabal/" ".go/" ".m2" ".gradle" ".rustup/" ".android/" ".vscode/"
    ".vagrant.d/" ".minecraft/" ".npm/" ".gem/" "VirtualBox VMs/"
    ".Mail/.notmuch/xapian/"
  ];

  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 ++ [
    "--progress" "--verbosity" "info" "--asynchronous-upload"
    "--full-if-older-than" "60D" "--num-retries" "3" "--use-agent"
  ];

  callDuplicity = name: argv: writeExecline name {} ([
    fetchSecretIntoEnv "FTP_PASSWORD" "backups/backblaze.com/application-keys/duplicity-main-backup/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 "$@" 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/";
  };

in
{
  incremental-home = duplicity-incremental home;
  verify-home = duplicity-verify home;
  list-home = duplicity-list home;
  verify-legosi = duplicity-verify legosi;
  restore-legosi = duplicity-restore legosi;
  list-legosi = duplicity-list legosi;
}