Add a service to backup Postgresql

This commit is contained in:
Andreas Zweili 2022-11-12 20:47:40 +01:00
parent 649fa4894c
commit e9fdf1f0ec
2 changed files with 60 additions and 1 deletions

View File

@ -0,0 +1,59 @@
{ custom
, path
, tag ? "home-dir"
, time
}: { config, pkgs, ... }:
{
imports = [
(import "${custom.inputs.self}/modules/telegram-notifications" { inherit custom; })
];
age.secrets.resticKey.file = "${custom.inputs.self}/scrts/restic.key.age";
systemd.timers."restic-backups" = {
wantedBy = [ "timers.target" ];
partOf = [ "restic-backups.service" ];
timerConfig = {
OnCalendar = time;
};
};
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=${custom.inputs.self}/modules/restic/excludes.txt \
--tag ${tag} ${path}
sudo -u postgres ${pkgs.postgresql}/bin/pg_dumpall | \
${pkgs.restic}/bin/restic backup \
--tag postgres \
--stdin \
--stdin-filename all_databases.sql
${pkgs.restic}/bin/restic forget \
--tag home-dir \
--host ${config.networking.hostName} \
--keep-daily 7 \
--keep-weekly 5 \
--keep-monthly 12 \
--keep-yearly 75
${pkgs.restic}/bin/restic forget \
--tag mariadb \
--host ${config.networking.hostName} \
--keep-daily 7 \
--keep-weekly 5 \
--keep-monthly 12 \
--keep-yearly 75
'';
};
}

View File

@ -14,7 +14,7 @@ in
port = "8082";
inherit custom;
})
(import "${custom.inputs.self}/modules/restic-server-client" {
(import "${custom.inputs.self}/modules/restic-server-postgres-client" {
path = "/var/lib/ttrss";
tag = "tt-rss";
time = "23:00";