2024-02-02 13:45:05 +01:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
inputs,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
let
|
|
|
|
cfg = config.services.az-restic-client-server-postgres;
|
|
|
|
in
|
|
|
|
{
|
2023-06-06 22:23:20 +02:00
|
|
|
options = {
|
|
|
|
services.az-restic-client-server-postgres = {
|
|
|
|
enable = lib.mkEnableOption "Enable restic backups for PostgreSQL.";
|
|
|
|
path = lib.mkOption {
|
|
|
|
type = lib.types.path;
|
|
|
|
description = "The directory to backup.";
|
|
|
|
};
|
|
|
|
tag = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "The tag to attach to the backups.";
|
|
|
|
default = "home-dir";
|
|
|
|
};
|
|
|
|
time = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "The time at which the backup runs.";
|
|
|
|
};
|
2022-11-12 20:47:40 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-06-06 22:23:20 +02:00
|
|
|
config = lib.mkIf cfg.enable {
|
|
|
|
services.az-telegram-notifications.enable = true;
|
|
|
|
|
|
|
|
age.secrets.resticKey.file = "${inputs.self}/scrts/restic.key.age";
|
|
|
|
|
|
|
|
systemd.timers."restic-backups" = {
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
partOf = [ "restic-backups.service" ];
|
2024-02-02 13:45:05 +01:00
|
|
|
timerConfig = {
|
|
|
|
OnCalendar = cfg.time;
|
|
|
|
};
|
2022-11-12 20:47:40 +01:00
|
|
|
};
|
|
|
|
|
2023-06-06 22:23:20 +02:00
|
|
|
systemd.services."restic-backups" = {
|
|
|
|
serviceConfig = {
|
|
|
|
User = "root";
|
|
|
|
Type = "oneshot";
|
|
|
|
};
|
|
|
|
environment = {
|
|
|
|
RESTIC_PASSWORD_FILE = config.age.secrets.resticKey.path;
|
|
|
|
RESTIC_REPOSITORY = "rest:http://10.7.89.30:8000";
|
|
|
|
};
|
|
|
|
onFailure = [ "unit-status-telegram@%n.service" ];
|
|
|
|
script = ''
|
|
|
|
${pkgs.restic}/bin/restic backup \
|
|
|
|
--exclude-file=${inputs.self}/modules/misc/restic-client/excludes.txt \
|
|
|
|
--tag ${cfg.tag} ${cfg.path}
|
|
|
|
|
2024-04-16 19:22:53 +02:00
|
|
|
${pkgs.sudo}/bin/sudo -u postgres ${pkgs.postgresql_15}/bin/pg_dumpall | \
|
2023-06-06 22:23:20 +02:00
|
|
|
${pkgs.restic}/bin/restic backup \
|
|
|
|
--tag postgres \
|
|
|
|
--stdin \
|
|
|
|
--stdin-filename all_databases.sql
|
2022-11-12 20:47:40 +01:00
|
|
|
|
2023-06-06 22:23:20 +02:00
|
|
|
${pkgs.restic}/bin/restic forget \
|
2023-11-26 16:20:06 +01:00
|
|
|
--tag ${cfg.tag} \
|
2023-06-06 22:23:20 +02:00
|
|
|
--host ${config.networking.hostName} \
|
|
|
|
--keep-daily 7 \
|
|
|
|
--keep-weekly 5 \
|
|
|
|
--keep-monthly 12 \
|
2023-11-20 16:05:21 +01:00
|
|
|
--keep-yearly 2
|
2022-11-12 20:47:40 +01:00
|
|
|
|
2023-06-06 22:23:20 +02:00
|
|
|
${pkgs.restic}/bin/restic forget \
|
|
|
|
--tag postgres \
|
|
|
|
--host ${config.networking.hostName} \
|
|
|
|
--keep-daily 7 \
|
|
|
|
--keep-weekly 5 \
|
|
|
|
--keep-monthly 12 \
|
2023-11-20 16:05:21 +01:00
|
|
|
--keep-yearly 2
|
2023-06-06 22:23:20 +02:00
|
|
|
'';
|
|
|
|
};
|
2022-11-12 20:47:40 +01:00
|
|
|
};
|
|
|
|
}
|