2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
|
2021-10-06 13:57:05 +00:00
|
|
|
inherit (lib) concatMapStringsSep concatStringsSep isInt isList literalExpression;
|
2023-05-24 13:37:59 +00:00
|
|
|
inherit (lib) mapAttrs mapAttrsToList mkDefault mkEnableOption mkIf mkOption mkRenamedOptionModule optional types;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
cfg = config.services.automysqlbackup;
|
|
|
|
pkg = pkgs.automysqlbackup;
|
|
|
|
user = "automysqlbackup";
|
|
|
|
group = "automysqlbackup";
|
|
|
|
|
|
|
|
toStr = val:
|
|
|
|
if isList val then "( ${concatMapStringsSep " " (val: "'${val}'") val} )"
|
|
|
|
else if isInt val then toString val
|
|
|
|
else if true == val then "'yes'"
|
|
|
|
else if false == val then "'no'"
|
|
|
|
else "'${toString val}'";
|
|
|
|
|
|
|
|
configFile = pkgs.writeText "automysqlbackup.conf" ''
|
|
|
|
#version=${pkg.version}
|
|
|
|
# DONT'T REMOVE THE PREVIOUS VERSION LINE!
|
|
|
|
#
|
|
|
|
${concatStringsSep "\n" (mapAttrsToList (name: value: "CONFIG_${name}=${toStr value}") cfg.config)}
|
|
|
|
'';
|
|
|
|
|
|
|
|
in
|
|
|
|
{
|
2023-05-24 13:37:59 +00:00
|
|
|
imports = [
|
|
|
|
(mkRenamedOptionModule [ "services" "automysqlbackup" "config" ] [ "services" "automysqlbackup" "settings" ])
|
|
|
|
];
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
# interface
|
|
|
|
options = {
|
|
|
|
services.automysqlbackup = {
|
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "AutoMySQLBackup");
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
calendar = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "01:15:00";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Configured when to run the backup service systemd unit (DayOfWeek Year-Month-Day Hour:Minute:Second).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-05-24 13:37:59 +00:00
|
|
|
settings = mkOption {
|
2020-04-24 23:36:52 +00:00
|
|
|
type = with types; attrsOf (oneOf [ str int bool (listOf str) ]);
|
|
|
|
default = {};
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
automysqlbackup configuration. Refer to
|
2022-08-12 12:06:08 +00:00
|
|
|
{file}`''${pkgs.automysqlbackup}/etc/automysqlbackup.conf`
|
2020-04-24 23:36:52 +00:00
|
|
|
for details on supported values.
|
|
|
|
'';
|
2021-10-06 13:57:05 +00:00
|
|
|
example = literalExpression ''
|
2020-04-24 23:36:52 +00:00
|
|
|
{
|
|
|
|
db_names = [ "nextcloud" "matomo" ];
|
|
|
|
table_exclude = [ "nextcloud.oc_users" "nextcloud.oc_whats_new" ];
|
|
|
|
mailcontent = "log";
|
|
|
|
mail_address = "admin@example.org";
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
# implementation
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
|
|
|
assertions = [
|
|
|
|
{ assertion = !config.services.mysqlBackup.enable;
|
|
|
|
message = "Please choose one of services.mysqlBackup or services.automysqlbackup.";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
services.automysqlbackup.config = mapAttrs (name: mkDefault) {
|
|
|
|
mysql_dump_username = user;
|
|
|
|
mysql_dump_host = "localhost";
|
2021-09-18 10:52:07 +00:00
|
|
|
mysql_dump_socket = "/run/mysqld/mysqld.sock";
|
2020-04-24 23:36:52 +00:00
|
|
|
backup_dir = "/var/backup/mysql";
|
|
|
|
db_exclude = [ "information_schema" "performance_schema" ];
|
|
|
|
mailcontent = "stdout";
|
|
|
|
mysql_dump_single_transaction = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.timers.automysqlbackup = {
|
|
|
|
description = "automysqlbackup timer";
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
timerConfig = {
|
|
|
|
OnCalendar = cfg.calendar;
|
|
|
|
AccuracySec = "5m";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.automysqlbackup = {
|
|
|
|
description = "automysqlbackup service";
|
|
|
|
serviceConfig = {
|
|
|
|
User = user;
|
|
|
|
Group = group;
|
|
|
|
ExecStart = "${pkg}/bin/automysqlbackup ${configFile}";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
environment.systemPackages = [ pkg ];
|
|
|
|
|
|
|
|
users.users.${user} = {
|
|
|
|
group = group;
|
|
|
|
isSystemUser = true;
|
|
|
|
};
|
|
|
|
users.groups.${group} = { };
|
|
|
|
|
|
|
|
systemd.tmpfiles.rules = [
|
|
|
|
"d '${cfg.config.backup_dir}' 0750 ${user} ${group} - -"
|
|
|
|
];
|
|
|
|
|
|
|
|
services.mysql.ensureUsers = optional (config.services.mysql.enable && cfg.config.mysql_dump_host == "localhost") {
|
|
|
|
name = user;
|
2023-05-24 13:37:59 +00:00
|
|
|
ensurePermissions = {
|
|
|
|
"*.*" = "SELECT, SHOW VIEW, TRIGGER, LOCK TABLES, EVENT";
|
|
|
|
|
|
|
|
# https://forums.mysql.com/read.php?10,668311,668315#msg-668315
|
|
|
|
"function sys.extract_table_from_file_name" = "execute";
|
|
|
|
"function sys.format_path" = "execute";
|
|
|
|
"function sys.format_statement" = "execute";
|
|
|
|
"function sys.extract_schema_from_file_name" = "execute";
|
|
|
|
"function sys.ps_thread_account" = "execute";
|
|
|
|
"function sys.format_time" = "execute";
|
|
|
|
"function sys.format_bytes" = "execute";
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|