actually use healthchecks
This commit is contained in:
parent
cb25aef9d7
commit
896e2a2f6d
1 changed files with 137 additions and 78 deletions
115
flake.nix
115
flake.nix
|
@ -230,31 +230,27 @@
|
||||||
};
|
};
|
||||||
default = { };
|
default = { };
|
||||||
};
|
};
|
||||||
|
healthcheck = lib.options.mkOption {
|
||||||
|
type = lib.types.submodule {
|
||||||
|
options = {
|
||||||
|
enable = lib.options.mkEnableOption "use healthchecks";
|
||||||
|
fullBackupPingURL = lib.options.mkOption {
|
||||||
|
type = lib.types.str;
|
||||||
|
};
|
||||||
|
differentialBackupPingURL = lib.options.mkOption {
|
||||||
|
type = lib.types.str;
|
||||||
|
};
|
||||||
|
incrementalBackupPingURL = lib.options.mkOption {
|
||||||
|
type = lib.types.str;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
default = { };
|
default = { };
|
||||||
};
|
};
|
||||||
|
|
||||||
healthcheck = lib.options.mkOption {
|
|
||||||
type = lib.types.submodule {
|
|
||||||
options = {
|
|
||||||
enable = lib.options.mkEnableOption "use healthcheck";
|
|
||||||
api_url = lib.options.mkOption {
|
|
||||||
type = lib.types.str;
|
|
||||||
};
|
|
||||||
api_key = lib.options.mkOption {
|
|
||||||
type = lib.types.str;
|
|
||||||
};
|
|
||||||
timeout = lib.options.mkOption {
|
|
||||||
type = lib.types.int;
|
|
||||||
default = 86400;
|
|
||||||
};
|
|
||||||
grace = lib.options.mkOption {
|
|
||||||
type = lib.types.int;
|
|
||||||
default = 14400;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
default = { };
|
default = { };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -627,6 +623,7 @@
|
||||||
]
|
]
|
||||||
else
|
else
|
||||||
"";
|
"";
|
||||||
|
curl = "${pkgs.curl}/bin/curl --silent --show-error --max-time 10 --retry 5";
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
description = "PostgreSQL Server";
|
description = "PostgreSQL Server";
|
||||||
|
@ -643,7 +640,15 @@
|
||||||
);
|
);
|
||||||
path = [
|
path = [
|
||||||
postgresql
|
postgresql
|
||||||
];
|
] ++ (
|
||||||
|
if cfg.backup.enable && (!cfg.replication.enable || cfg.replication.role == "primary")
|
||||||
|
then
|
||||||
|
[
|
||||||
|
pgbackrest
|
||||||
|
]
|
||||||
|
else
|
||||||
|
[ ]
|
||||||
|
);
|
||||||
|
|
||||||
preStart =
|
preStart =
|
||||||
if (!cfg.replication.enable || cfg.replication.role == "primary")
|
if (!cfg.replication.enable || cfg.replication.role == "primary")
|
||||||
|
@ -815,16 +820,33 @@
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
systemd.services.postgresql-backup-full = lib.mkIf (cfg.backup.enable && (!cfg.replication.enable || cfg.replication.role == "primary")) {
|
systemd.services.postgresql-backup-full = lib.mkIf (cfg.backup.enable && (!cfg.replication.enable || cfg.replication.role == "primary")) (
|
||||||
|
let
|
||||||
|
hcStart =
|
||||||
|
if cfg.backup.healthchecks.enable then
|
||||||
|
"${curl} ${cfg.backup.healthchecks.fullBackupPingURL}/start"
|
||||||
|
else
|
||||||
|
"";
|
||||||
|
hcStop =
|
||||||
|
if cfg.backup.healthchecks.enable then
|
||||||
|
"${curl} ${cfg.backup.healthchecks.fullBackupPingURL}"
|
||||||
|
else
|
||||||
|
"";
|
||||||
|
in
|
||||||
|
{
|
||||||
description = "PostgreSQL Full Backup";
|
description = "PostgreSQL Full Backup";
|
||||||
requires = [ "postgresql.service" ];
|
requires = [ "postgresql.service" ];
|
||||||
script = ''
|
script = ''
|
||||||
|
${hcStart}
|
||||||
|
|
||||||
while ! ${postgresql}/bin/psql -d postgres -c "" 2> /dev/null
|
while ! ${postgresql}/bin/psql -d postgres -c "" 2> /dev/null
|
||||||
do
|
do
|
||||||
sleep 0.1
|
sleep 0.1
|
||||||
done
|
done
|
||||||
|
|
||||||
${pgbackrest}/bin/pgbackrest --type=full --start-fast --stop-auto --delta backup
|
${pgbackrest}/bin/pgbackrest --type=full --start-fast --stop-auto --delta backup
|
||||||
|
|
||||||
|
${hcStop}
|
||||||
'';
|
'';
|
||||||
environment = pgbackrestEnvironment;
|
environment = pgbackrestEnvironment;
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
|
@ -833,7 +855,8 @@
|
||||||
Group = "postgres";
|
Group = "postgres";
|
||||||
TimeoutSec = 3600;
|
TimeoutSec = 3600;
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
);
|
||||||
|
|
||||||
systemd.timers.postgresql-backup-full = lib.mkIf (cfg.backup.enable && (!cfg.replication.enable || cfg.replication.role == "primary")) {
|
systemd.timers.postgresql-backup-full = lib.mkIf (cfg.backup.enable && (!cfg.replication.enable || cfg.replication.role == "primary")) {
|
||||||
description = "PostgreSQL Full Backup";
|
description = "PostgreSQL Full Backup";
|
||||||
|
@ -844,16 +867,33 @@
|
||||||
wantedBy = [ "multi-user.target" ];
|
wantedBy = [ "multi-user.target" ];
|
||||||
};
|
};
|
||||||
|
|
||||||
systemd.services.postgresql-backup-diff = lib.mkIf (cfg.backup.enable && (!cfg.replication.enable || cfg.replication.role == "primary")) {
|
systemd.services.postgresql-backup-diff = lib.mkIf (cfg.backup.enable && (!cfg.replication.enable || cfg.replication.role == "primary")) (
|
||||||
|
let
|
||||||
|
hcStart =
|
||||||
|
if cfg.backup.healthchecks.enable then
|
||||||
|
"${curl} ${cfg.backup.healthchecks.differentialBackupPingURL}/start"
|
||||||
|
else
|
||||||
|
"";
|
||||||
|
hcStop =
|
||||||
|
if cfg.backup.healthchecks.enable then
|
||||||
|
"${curl} ${cfg.backup.healthchecks.differentialBackupPingURL}"
|
||||||
|
else
|
||||||
|
"";
|
||||||
|
in
|
||||||
|
{
|
||||||
description = "PostgreSQL Differential Backup";
|
description = "PostgreSQL Differential Backup";
|
||||||
requires = [ "postgresql.service" ];
|
requires = [ "postgresql.service" ];
|
||||||
script = ''
|
script = ''
|
||||||
|
${hcStart}
|
||||||
|
|
||||||
while ! ${postgresql}/bin/psql -d postgres -c "" 2> /dev/null
|
while ! ${postgresql}/bin/psql -d postgres -c "" 2> /dev/null
|
||||||
do
|
do
|
||||||
sleep 0.1
|
sleep 0.1
|
||||||
done
|
done
|
||||||
|
|
||||||
${pgbackrest}/bin/pgbackrest --type=diff --start-fast --stop-auto --delta backup
|
${pgbackrest}/bin/pgbackrest --type=diff --start-fast --stop-auto --delta backup
|
||||||
|
|
||||||
|
${hcStop}
|
||||||
'';
|
'';
|
||||||
environment = pgbackrestEnvironment;
|
environment = pgbackrestEnvironment;
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
|
@ -862,7 +902,8 @@
|
||||||
Group = "postgres";
|
Group = "postgres";
|
||||||
TimeoutSec = 3600;
|
TimeoutSec = 3600;
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
);
|
||||||
|
|
||||||
systemd.timers.postgresql-backup-diff = lib.mkIf (cfg.backup.enable && (!cfg.replication.enable || cfg.replication.role == "primary")) {
|
systemd.timers.postgresql-backup-diff = lib.mkIf (cfg.backup.enable && (!cfg.replication.enable || cfg.replication.role == "primary")) {
|
||||||
description = "PostgreSQL Differential Backup";
|
description = "PostgreSQL Differential Backup";
|
||||||
|
@ -873,16 +914,33 @@
|
||||||
wantedBy = [ "multi-user.target" ];
|
wantedBy = [ "multi-user.target" ];
|
||||||
};
|
};
|
||||||
|
|
||||||
systemd.services.postgresql-backup-incr = lib.mkIf (cfg.backup.enable && (!cfg.replication.enable || cfg.replication.role == "primary")) {
|
systemd.services.postgresql-backup-incr = lib.mkIf (cfg.backup.enable && (!cfg.replication.enable || cfg.replication.role == "primary")) (
|
||||||
|
let
|
||||||
|
hcStart =
|
||||||
|
if cfg.backup.healthchecks.enable then
|
||||||
|
"${curl} ${cfg.backup.healthchecks.incrementalBackupPingURL}/start"
|
||||||
|
else
|
||||||
|
"";
|
||||||
|
hcStop =
|
||||||
|
if cfg.backup.healthchecks.enable then
|
||||||
|
"${curl} ${cfg.backup.healthchecks.incrementalBackupPingURL}"
|
||||||
|
else
|
||||||
|
"";
|
||||||
|
in
|
||||||
|
{
|
||||||
description = "PostgreSQL Incremental Backup";
|
description = "PostgreSQL Incremental Backup";
|
||||||
requires = [ "postgresql.service" ];
|
requires = [ "postgresql.service" ];
|
||||||
script = ''
|
script = ''
|
||||||
|
${hcStart}
|
||||||
|
|
||||||
while ! ${postgresql}/bin/psql -d postgres -c "" 2> /dev/null
|
while ! ${postgresql}/bin/psql -d postgres -c "" 2> /dev/null
|
||||||
do
|
do
|
||||||
sleep 0.1
|
sleep 0.1
|
||||||
done
|
done
|
||||||
|
|
||||||
${pgbackrest}/bin/pgbackrest --type=incr --start-fast --stop-auto --delta backup
|
${pgbackrest}/bin/pgbackrest --type=incr --start-fast --stop-auto --delta backup
|
||||||
|
|
||||||
|
${hsStop}
|
||||||
'';
|
'';
|
||||||
environment = pgbackrestEnvironment;
|
environment = pgbackrestEnvironment;
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
|
@ -891,12 +949,13 @@
|
||||||
Group = "postgres";
|
Group = "postgres";
|
||||||
TimeoutSec = 3600;
|
TimeoutSec = 3600;
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
);
|
||||||
|
|
||||||
systemd.timers.postgresql-backup-incr = lib.mkIf (cfg.backup.enable && (!cfg.replication.enable || cfg.replication.role == "primary")) {
|
systemd.timers.postgresql-backup-incr = lib.mkIf (cfg.backup.enable && (!cfg.replication.enable || cfg.replication.role == "primary")) {
|
||||||
description = "PostgreSQL Incremental Backup";
|
description = "PostgreSQL Incremental Backup";
|
||||||
timerConfig = {
|
timerConfig = {
|
||||||
OnCalendar = "*-*-* 06,10,14,18,22:00:00";
|
OnCalendar = "*-*-* 00,06,10,14,18,22:00:00";
|
||||||
RandomizedDelaySec = "5m";
|
RandomizedDelaySec = "5m";
|
||||||
};
|
};
|
||||||
wantedBy = [ "multi-user.target" ];
|
wantedBy = [ "multi-user.target" ];
|
||||||
|
|
Loading…
Reference in a new issue