stockholm/lass/3modules/mysql-backup.nix

87 lines
2.1 KiB
Nix
Raw Normal View History

2016-04-20 11:59:22 +00:00
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.lass.mysqlBackup;
out = {
options.lass.mysqlBackup = api;
config = mkIf cfg.enable imp;
};
api = {
enable = mkEnableOption "mysqlBackup";
config = mkOption {
type = with types; attrsOf (submodule ({ config, ... }: {
options = {
name = mkOption {
type = types.str;
default = config._module.args.name;
};
startAt = mkOption {
type = with types; nullOr str; # TODO systemd.time(7)'s calendar event
default = "*-*-* 01:15:00";
};
user = mkOption {
type = str;
default = "root";
};
password = mkOption {
type = nullOr str;
default = null;
description = ''
path to a file containing the mysqlPassword for the specified user.
'';
};
databases = mkOption {
type = listOf str;
default = [];
};
location = mkOption {
type = str;
2018-12-25 13:29:41 +00:00
default = "/backups/sql_dumps";
2016-04-20 11:59:22 +00:00
};
};
}));
description = "configuration for mysqlBackup";
};
};
imp = {
2018-12-25 13:29:41 +00:00
services.mysql.ensureUsers = [
{ ensurePermissions = { "*.*" = "ALL"; }; name = "root"; }
];
2016-04-20 11:59:22 +00:00
systemd.services =
mapAttrs' (_: plan: nameValuePair "mysqlBackup-${plan.name}" {
path = with pkgs; [
mysql
gzip
];
serviceConfig = rec {
ExecStart = start plan;
SyslogIdentifier = ExecStart.name;
Type = "oneshot";
User = plan.user;
};
startAt = plan.startAt;
}) cfg.config;
};
start = plan: let
2018-12-25 13:29:41 +00:00
backupScript = plan: db: ''
mkdir -p ${plan.location}
mysqldump -u ${plan.user} ${optionalString (plan.password != null) "-p$(cat ${plan.password})"} ${db} | gzip -c > ${plan.location}/${db}.gz
'';
2016-04-20 11:59:22 +00:00
in pkgs.pkgs.writeDash "mysqlBackup.${plan.name}" ''
${concatMapStringsSep "\n" (backupScript plan) plan.databases}
'';
in out