1 { config, lib, pkgs, ... }:
5 inherit (lib) concatMapStringsSep concatStringsSep isInt isList literalExpression;
6 inherit (lib) mapAttrs mapAttrsToList mkDefault mkEnableOption mkIf mkOption optional types;
8 cfg = config.services.automysqlbackup;
9 pkg = pkgs.automysqlbackup;
10 user = "automysqlbackup";
11 group = "automysqlbackup";
14 if isList val then "( ${concatMapStringsSep " " (val: "'${val}'") val} )"
15 else if isInt val then toString val
16 else if true == val then "'yes'"
17 else if false == val then "'no'"
18 else "'${toString val}'";
20 configFile = pkgs.writeText "automysqlbackup.conf" ''
21 #version=${pkg.version}
22 # DONT'T REMOVE THE PREVIOUS VERSION LINE!
24 ${concatStringsSep "\n" (mapAttrsToList (name: value: "CONFIG_${name}=${toStr value}") cfg.config)}
31 services.automysqlbackup = {
33 enable = mkEnableOption (lib.mdDoc "AutoMySQLBackup");
38 description = lib.mdDoc ''
39 Configured when to run the backup service systemd unit (DayOfWeek Year-Month-Day Hour:Minute:Second).
44 type = with types; attrsOf (oneOf [ str int bool (listOf str) ]);
46 description = lib.mdDoc ''
47 automysqlbackup configuration. Refer to
48 {file}`''${pkgs.automysqlbackup}/etc/automysqlbackup.conf`
49 for details on supported values.
51 example = literalExpression ''
53 db_names = [ "nextcloud" "matomo" ];
54 table_exclude = [ "nextcloud.oc_users" "nextcloud.oc_whats_new" ];
56 mail_address = "admin@example.org";
65 config = mkIf cfg.enable {
68 { assertion = !config.services.mysqlBackup.enable;
69 message = "Please choose one of services.mysqlBackup or services.automysqlbackup.";
73 services.automysqlbackup.config = mapAttrs (name: mkDefault) {
74 mysql_dump_username = user;
75 mysql_dump_host = "localhost";
76 mysql_dump_socket = "/run/mysqld/mysqld.sock";
77 backup_dir = "/var/backup/mysql";
78 db_exclude = [ "information_schema" "performance_schema" ];
79 mailcontent = "stdout";
80 mysql_dump_single_transaction = true;
83 systemd.timers.automysqlbackup = {
84 description = "automysqlbackup timer";
85 wantedBy = [ "timers.target" ];
87 OnCalendar = cfg.calendar;
92 systemd.services.automysqlbackup = {
93 description = "automysqlbackup service";
97 ExecStart = "${pkg}/bin/automysqlbackup ${configFile}";
101 environment.systemPackages = [ pkg ];
103 users.users.${user} = {
107 users.groups.${group} = { };
109 systemd.tmpfiles.rules = [
110 "d '${cfg.config.backup_dir}' 0750 ${user} ${group} - -"
113 services.mysql.ensureUsers = optional (config.services.mysql.enable && cfg.config.mysql_dump_host == "localhost") {
115 ensurePermissions = { "*.*" = "SELECT, SHOW VIEW, TRIGGER, LOCK TABLES, EVENT"; };