Add some automatic backups of postgresql databases
when using zfs volume
This commit is contained in:
parent
457eb3f6b0
commit
1ca3a407f2
5 changed files with 263 additions and 197 deletions
88
nix/modules/nixos/fs/zfs/services/postgresql/default.nix
Normal file
88
nix/modules/nixos/fs/zfs/services/postgresql/default.nix
Normal file
|
@ -0,0 +1,88 @@
|
|||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
let
|
||||
inherit (import ../../options.nix { inherit config lib; }) datasetModule;
|
||||
zfsCfg = config.khscodes.fs.zfs;
|
||||
cfg = zfsCfg.services.postgresql;
|
||||
pgCfg = config.services.postgresql;
|
||||
in
|
||||
{
|
||||
options.khscodes.fs.zfs.services.postgresql = {
|
||||
enable = lib.mkOption {
|
||||
description = "Enables storing postgresql data on a zfs zpool";
|
||||
type = lib.types.bool;
|
||||
default = zfsCfg.enable && pgCfg.enable;
|
||||
};
|
||||
pool = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = zfsCfg.mainPoolName;
|
||||
};
|
||||
datasetName = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "database/postgresql";
|
||||
};
|
||||
backupDatasetName = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "backup/database/postgresql";
|
||||
};
|
||||
datasetConfig = lib.mkOption {
|
||||
type = datasetModule;
|
||||
default = {
|
||||
mountpoint = "/var/lib/postgresql";
|
||||
};
|
||||
};
|
||||
backupDatasetConfig = lib.mkOption {
|
||||
type = datasetModule;
|
||||
default = {
|
||||
mountpoint = "/var/backup/postgresql";
|
||||
};
|
||||
};
|
||||
backupDatabases = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = pgCfg.ensureDatabases;
|
||||
};
|
||||
};
|
||||
config = lib.mkMerge [
|
||||
(lib.mkIf (zfsCfg.enable && cfg.enable) {
|
||||
khscodes.fs.zfs.zpools."${cfg.pool}".datasets."${cfg.datasetName}" = cfg.datasetConfig;
|
||||
systemd.services.postgresql = {
|
||||
after = [ "khscodes-zpool-setup.service" ];
|
||||
unitConfig = {
|
||||
RequiresMountsFor = [ cfg.datasetConfig.mountpoint ];
|
||||
};
|
||||
};
|
||||
services.postgresql.dataDir = "${cfg.datasetConfig.mountpoint}/${pgCfg.package.psqlSchema}";
|
||||
})
|
||||
(lib.mkIf (zfsCfg.enable && cfg.enable) {
|
||||
khscodes.fs.zfs.zpools."${cfg.pool}".datasets."${cfg.backupDatasetName}" = cfg.backupDatasetConfig;
|
||||
services.postgresqlBackup = {
|
||||
enable = true;
|
||||
databases = cfg.backupDatabases;
|
||||
};
|
||||
systemd.services =
|
||||
(lib.listToAttrs (
|
||||
lib.lists.map (db: {
|
||||
name = "postgresqlBackup-${db}";
|
||||
value = {
|
||||
after = [ "khscodes-zpool-setup.service" ];
|
||||
unitConfig = {
|
||||
RequiresMountsFor = [ cfg.backupDatasetConfig.mountpoint ];
|
||||
};
|
||||
};
|
||||
}) cfg.backupDatabases
|
||||
))
|
||||
// {
|
||||
khscodes-zpool-setup.serviceConfig = {
|
||||
ExecStartPost = [
|
||||
"${lib.getExe' pkgs.uutils-coreutils-noprefix "chown"} ${config.systemd.services.postgresql.serviceConfig.User}:${config.systemd.services.postgresql.serviceConfig.Group} ${lib.escapeShellArg cfg.backupDatasetConfig.mountpoint}"
|
||||
"${lib.getExe' pkgs.uutils-coreutils-noprefix "chmod"} 0700 ${lib.escapeShellArg cfg.backupDatasetConfig.mountpoint}"
|
||||
];
|
||||
};
|
||||
};
|
||||
})
|
||||
];
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue