2020-06-11 11:39:17 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
options.services.backups = {
|
2022-03-03 15:53:04 +00:00
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc ''
|
2022-03-03 15:53:04 +00:00
|
|
|
Enable backups of node data.
|
|
|
|
This uses the NixOS duplicity service.
|
|
|
|
To further configure the backup, you can set NixOS options `services.duplicity.*`.
|
|
|
|
The `services.duplicity.cleanup.*` options are particularly useful.
|
|
|
|
'';
|
|
|
|
};
|
2020-06-11 11:39:17 +00:00
|
|
|
with-bulk-data = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc ''
|
2020-06-11 11:39:17 +00:00
|
|
|
Whether to also backup Bitcoin blockchain and other bulk data.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
destination = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "file:///var/lib/localBackups";
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc ''
|
2020-06-11 11:39:17 +00:00
|
|
|
Where to back up to.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
frequency = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc ''
|
2020-06-11 11:39:17 +00:00
|
|
|
Run backup with the given frequency. If null, do not run automatically.
|
|
|
|
'';
|
|
|
|
};
|
2021-09-08 15:01:09 +00:00
|
|
|
postgresqlDatabases = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc "List of database names to backup.";
|
2021-09-08 15:01:09 +00:00
|
|
|
};
|
2020-06-11 11:39:17 +00:00
|
|
|
extraFiles = mkOption {
|
2021-09-24 11:27:04 +00:00
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
example = [ "/var/lib/nginx" ];
|
2022-12-18 12:13:47 +00:00
|
|
|
description = mdDoc "Additional files to be appended to filelist.";
|
2020-06-11 11:39:17 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-09-13 11:40:47 +00:00
|
|
|
cfg = config.services.backups;
|
|
|
|
|
2023-06-13 20:49:46 +00:00
|
|
|
# Potential backup file paths are matched against filelist
|
2021-10-05 19:44:58 +00:00
|
|
|
# entries from top to bottom.
|
|
|
|
# The first match determines inclusion or exclusion.
|
|
|
|
filelist = builtins.toFile "filelist.txt" ''
|
|
|
|
${builtins.concatStringsSep "\n" cfg.extraFiles}
|
|
|
|
|
2021-10-05 19:44:57 +00:00
|
|
|
${optionalString (!cfg.with-bulk-data) ''
|
|
|
|
- ${config.services.bitcoind.dataDir}/blocks
|
|
|
|
- ${config.services.bitcoind.dataDir}/chainstate
|
|
|
|
- ${config.services.bitcoind.dataDir}/indexes
|
|
|
|
''}
|
2021-09-13 11:40:47 +00:00
|
|
|
${config.services.bitcoind.dataDir}
|
|
|
|
${config.services.clightning.dataDir}
|
2022-05-05 19:56:16 +00:00
|
|
|
${config.services.clightning-rest.dataDir}
|
2021-09-13 11:40:47 +00:00
|
|
|
${config.services.lnd.dataDir}
|
2021-10-05 19:44:57 +00:00
|
|
|
${optionalString (!cfg.with-bulk-data) ''
|
|
|
|
- ${config.services.liquidd.dataDir}/*/blocks
|
|
|
|
- ${config.services.liquidd.dataDir}/*/chainstate
|
|
|
|
- ${config.services.liquidd.dataDir}/*/indexes
|
|
|
|
''}
|
2021-09-13 11:40:47 +00:00
|
|
|
${config.services.liquidd.dataDir}
|
|
|
|
${optionalString cfg.with-bulk-data "${config.services.electrs.dataDir}"}
|
|
|
|
${config.services.nbxplorer.dataDir}
|
|
|
|
${config.services.btcpayserver.dataDir}
|
|
|
|
${config.services.joinmarket.dataDir}
|
|
|
|
${optionalString config.nix-bitcoin.generateSecrets "${config.nix-bitcoin.secretsDir}"}
|
|
|
|
/var/lib/tor
|
|
|
|
/var/lib/nixos
|
|
|
|
|
|
|
|
${builtins.concatStringsSep "\n" postgresqlBackupPaths}
|
|
|
|
|
|
|
|
# Exclude all unspecified files and directories
|
|
|
|
- /
|
|
|
|
'';
|
|
|
|
|
|
|
|
postgresqlBackupDir = config.services.postgresqlBackup.location;
|
|
|
|
postgresqlBackupPaths = map (db: "${postgresqlBackupDir}/${db}.sql.gz") cfg.postgresqlDatabases;
|
|
|
|
postgresqlBackupServices = map (db: "postgresqlBackup-${db}.service") cfg.postgresqlDatabases;
|
|
|
|
in {
|
|
|
|
inherit options;
|
|
|
|
|
2021-09-08 15:01:09 +00:00
|
|
|
config = mkIf cfg.enable {
|
2020-06-11 11:39:17 +00:00
|
|
|
environment.systemPackages = [ pkgs.duplicity ];
|
|
|
|
|
|
|
|
services.duplicity = {
|
|
|
|
enable = true;
|
|
|
|
extraFlags = [
|
|
|
|
"--include-filelist" "${filelist}"
|
|
|
|
];
|
2022-03-03 15:53:04 +00:00
|
|
|
fullIfOlderThan = mkDefault "1M";
|
2021-02-01 21:53:12 +00:00
|
|
|
targetUrl = cfg.destination;
|
2020-06-11 11:39:17 +00:00
|
|
|
frequency = cfg.frequency;
|
|
|
|
secretFile = "${config.nix-bitcoin.secretsDir}/backup-encryption-env";
|
|
|
|
};
|
|
|
|
|
2021-09-08 15:01:09 +00:00
|
|
|
systemd.services.duplicity = {
|
|
|
|
wants = postgresqlBackupServices;
|
|
|
|
after = postgresqlBackupServices;
|
2020-09-22 16:40:20 +00:00
|
|
|
};
|
2021-09-08 15:01:09 +00:00
|
|
|
|
|
|
|
services.postgresqlBackup = {
|
|
|
|
enable = mkIf (cfg.postgresqlDatabases != []) true;
|
|
|
|
databases = cfg.postgresqlDatabases;
|
2020-09-22 16:40:20 +00:00
|
|
|
};
|
2021-09-08 15:01:09 +00:00
|
|
|
|
|
|
|
nix-bitcoin.secrets.backup-encryption-env.user = "root";
|
2021-09-08 15:01:18 +00:00
|
|
|
nix-bitcoin.generateSecretsCmds.backups = ''
|
|
|
|
makePasswordSecret backup-encryption-password
|
|
|
|
if [[ backup-encryption-password -nt backup-encryption-env ]]; then
|
|
|
|
echo "PASSPHRASE=$(cat backup-encryption-password)" > backup-encryption-env
|
|
|
|
fi
|
|
|
|
'';
|
2021-09-08 15:01:09 +00:00
|
|
|
|
2022-04-30 13:35:46 +00:00
|
|
|
services.backups.postgresqlDatabases = mkIf config.services.btcpayserver.enable (
|
|
|
|
[ "btcpaydb" ] ++ optional cfg.with-bulk-data "nbxplorer"
|
|
|
|
);
|
2021-09-08 15:01:09 +00:00
|
|
|
};
|
2020-06-11 11:39:17 +00:00
|
|
|
}
|