2024-05-31 14:36:37 +00:00
|
|
|
{
|
|
|
|
pkgs,
|
|
|
|
lib,
|
|
|
|
config,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
let
|
|
|
|
createDatatbaseState =
|
|
|
|
db:
|
|
|
|
let
|
2024-06-05 16:37:31 +00:00
|
|
|
folder = "/var/backup/postgres/${db.name}";
|
2024-06-06 13:17:47 +00:00
|
|
|
current = "${folder}/pg-dump";
|
|
|
|
compression = lib.optionalString (lib.versionAtLeast config.services.postgresql.package.version "16") "--compress=zstd";
|
2024-05-31 14:36:37 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
folders = [ folder ];
|
|
|
|
preBackupCommand = ''
|
2024-06-05 16:37:31 +00:00
|
|
|
export PATH=${
|
|
|
|
lib.makeBinPath [
|
|
|
|
config.services.postgresql.package
|
|
|
|
config.systemd.package
|
|
|
|
pkgs.coreutils
|
|
|
|
pkgs.util-linux
|
|
|
|
pkgs.zstd
|
|
|
|
]
|
|
|
|
}
|
|
|
|
while [[ "$(systemctl is-active postgresql)" == activating ]]; do
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
|
|
|
|
mkdir -p "${folder}"
|
2024-06-06 13:17:47 +00:00
|
|
|
runuser -u postgres -- pg_dump ${compression} --dbname=${db.name} -Fc -c > "${current}.tmp"
|
2024-06-05 16:37:31 +00:00
|
|
|
mv "${current}.tmp" ${current}
|
2024-05-31 14:36:37 +00:00
|
|
|
'';
|
2024-06-11 14:00:39 +00:00
|
|
|
postRestoreCommand = "postgres-db-restore-command-${db.name}";
|
2024-05-31 14:36:37 +00:00
|
|
|
};
|
2024-06-04 10:56:39 +00:00
|
|
|
|
|
|
|
createDatabase = db: ''
|
|
|
|
CREATE DATABASE "${db.name}" ${
|
|
|
|
lib.concatStringsSep " " (
|
2024-06-05 16:37:31 +00:00
|
|
|
lib.mapAttrsToList (name: value: "${name} = '${value}'") db.create.options
|
2024-06-04 10:56:39 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
cfg = config.clan.postgresql;
|
|
|
|
|
|
|
|
userClauses = lib.mapAttrsToList (
|
2024-06-05 16:37:31 +00:00
|
|
|
_: user:
|
2024-06-04 10:56:39 +00:00
|
|
|
''$PSQL -tAc "SELECT 1 FROM pg_roles WHERE rolname='${user.name}'" | grep -q 1 || $PSQL -tAc 'CREATE USER "${user.name}"' ''
|
|
|
|
) cfg.users;
|
|
|
|
databaseClauses = lib.mapAttrsToList (
|
|
|
|
name: db:
|
2024-06-05 16:37:31 +00:00
|
|
|
lib.optionalString db.create.enable ''$PSQL -d postgres -c "SELECT 1 FROM pg_database WHERE datname = '${name}'" | grep -q 1 || $PSQL -d postgres -c ${lib.escapeShellArg (createDatabase db)} ''
|
2024-06-04 10:56:39 +00:00
|
|
|
) cfg.databases;
|
2024-05-31 14:36:37 +00:00
|
|
|
in
|
|
|
|
{
|
2024-06-05 16:37:31 +00:00
|
|
|
options.clan.postgresql = {
|
|
|
|
# we are reimplemeting ensureDatabase and ensureUser options here to allow to create databases with options
|
2024-05-31 14:36:37 +00:00
|
|
|
databases = lib.mkOption {
|
2024-06-04 10:56:39 +00:00
|
|
|
default = { };
|
|
|
|
type = lib.types.attrsOf (
|
|
|
|
lib.types.submodule (
|
|
|
|
{ name, ... }:
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
name = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
default = name;
|
|
|
|
};
|
|
|
|
# set to false, in case the upstream module uses ensureDatabase option
|
2024-06-05 16:37:31 +00:00
|
|
|
create.enable = lib.mkOption {
|
2024-06-04 10:56:39 +00:00
|
|
|
type = lib.types.bool;
|
|
|
|
default = true;
|
2024-06-05 16:37:31 +00:00
|
|
|
description = "Create the database if it does not exist.";
|
2024-06-04 10:56:39 +00:00
|
|
|
};
|
2024-06-05 16:37:31 +00:00
|
|
|
create.options = lib.mkOption {
|
2024-06-04 10:56:39 +00:00
|
|
|
type = lib.types.lazyAttrsOf lib.types.str;
|
|
|
|
default = { };
|
|
|
|
example = {
|
|
|
|
TEMPLATE = "template0";
|
|
|
|
LC_COLLATE = "C";
|
|
|
|
LC_CTYPE = "C";
|
|
|
|
ENCODING = "UTF8";
|
|
|
|
OWNER = "foo";
|
|
|
|
};
|
|
|
|
};
|
2024-06-06 13:17:47 +00:00
|
|
|
restore.stopOnRestore = lib.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.str;
|
|
|
|
default = [ ];
|
|
|
|
description = "List of services to stop before restoring the database.";
|
2024-06-05 16:37:31 +00:00
|
|
|
};
|
2024-06-04 10:56:39 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
users = lib.mkOption {
|
|
|
|
default = { };
|
|
|
|
type = lib.types.attrsOf (
|
|
|
|
lib.types.submodule (
|
|
|
|
{ name, ... }:
|
|
|
|
{
|
|
|
|
options.name = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
default = name;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
2024-05-31 14:36:37 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
config = {
|
2024-06-05 16:37:31 +00:00
|
|
|
services.postgresql.settings = {
|
|
|
|
wal_level = "replica";
|
|
|
|
max_wal_senders = 3;
|
|
|
|
};
|
|
|
|
|
2024-05-31 14:36:37 +00:00
|
|
|
services.postgresql.enable = true;
|
2024-06-04 10:56:39 +00:00
|
|
|
# We are duplicating a bit the upstream module but allow to create databases with options
|
|
|
|
systemd.services.postgresql.postStart = ''
|
|
|
|
PSQL="psql --port=${builtins.toString config.services.postgresql.settings.port}"
|
|
|
|
|
|
|
|
while ! $PSQL -d postgres -c "" 2> /dev/null; do
|
2024-06-05 16:37:31 +00:00
|
|
|
if ! kill -0 "$MAINPID"; then exit 1; fi
|
|
|
|
sleep 0.1
|
2024-06-04 10:56:39 +00:00
|
|
|
done
|
|
|
|
${lib.concatStringsSep "\n" userClauses}
|
|
|
|
${lib.concatStringsSep "\n" databaseClauses}
|
|
|
|
'';
|
2024-06-05 16:37:31 +00:00
|
|
|
|
2024-06-04 10:56:39 +00:00
|
|
|
clanCore.state = lib.mapAttrs' (
|
|
|
|
_: db: lib.nameValuePair "postgresql-${db.name}" (createDatatbaseState db)
|
|
|
|
) config.clan.postgresql.databases;
|
2024-06-11 14:00:39 +00:00
|
|
|
|
|
|
|
environment.systemPackages = builtins.map (
|
|
|
|
db:
|
|
|
|
let
|
|
|
|
folder = "/var/backup/postgres/${db.name}";
|
|
|
|
current = "${folder}/pg-dump";
|
|
|
|
in
|
|
|
|
pkgs.writeShellScriptBin "postgres-db-restore-command-${db.name}" ''
|
|
|
|
export PATH=${
|
|
|
|
lib.makeBinPath [
|
|
|
|
config.services.postgresql.package
|
|
|
|
config.systemd.package
|
|
|
|
pkgs.coreutils
|
|
|
|
pkgs.util-linux
|
|
|
|
pkgs.zstd
|
|
|
|
]
|
|
|
|
}
|
|
|
|
while [[ "$(systemctl is-active postgresql)" == activating ]]; do
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
echo "Waiting for postgres to be ready..."
|
|
|
|
while ! runuser -u postgres -- psql --port=${builtins.toString config.services.postgresql.settings.port} -d postgres -c "" ; do
|
|
|
|
if ! systemctl is-active postgresql; then exit 1; fi
|
|
|
|
sleep 0.1
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ -e "${current}" ]]; then
|
|
|
|
(
|
2024-06-14 09:14:49 +00:00
|
|
|
${
|
|
|
|
lib.optionalString (db.restore.stopOnRestore != [ ]) ''
|
|
|
|
systemctl stop ${builtins.toString db.restore.stopOnRestore}
|
|
|
|
trap "systemctl start ${builtins.toString db.restore.stopOnRestore}" EXIT
|
|
|
|
''
|
|
|
|
}
|
2024-06-11 14:00:39 +00:00
|
|
|
|
|
|
|
mkdir -p "${folder}"
|
|
|
|
runuser -u postgres -- dropdb "${db.name}"
|
|
|
|
runuser -u postgres -- pg_restore -C -d postgres "${current}"
|
|
|
|
)
|
|
|
|
else
|
|
|
|
echo No database backup found, skipping restore
|
|
|
|
fi
|
|
|
|
''
|
|
|
|
) (builtins.attrValues config.clan.postgresql.databases);
|
2024-05-31 14:36:37 +00:00
|
|
|
};
|
|
|
|
}
|