1 { config, lib, pkgs, ... }:
5 inherit (lib) concatMapStringsSep concatStringsSep isInt isList literalExpression;
6 inherit (lib) mapAttrs mapAttrsToList mkDefault mkEnableOption mkIf mkOption mkRenamedOptionModule 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)}
30 (mkRenamedOptionModule [ "services" "automysqlbackup" "config" ] [ "services" "automysqlbackup" "settings" ])
35 services.automysqlbackup = {
37 enable = mkEnableOption "AutoMySQLBackup";
43 Configured when to run the backup service systemd unit (DayOfWeek Year-Month-Day Hour:Minute:Second).
48 type = with types; attrsOf (oneOf [ str int bool (listOf str) ]);
51 automysqlbackup configuration. Refer to
52 {file}`''${pkgs.automysqlbackup}/etc/automysqlbackup.conf`
53 for details on supported values.
55 example = literalExpression ''
57 db_names = [ "nextcloud" "matomo" ];
58 table_exclude = [ "nextcloud.oc_users" "nextcloud.oc_whats_new" ];
60 mail_address = "admin@example.org";
69 config = mkIf cfg.enable {
72 { assertion = !config.services.mysqlBackup.enable;
73 message = "Please choose one of services.mysqlBackup or services.automysqlbackup.";
77 services.automysqlbackup.config = mapAttrs (name: mkDefault) {
78 mysql_dump_username = user;
79 mysql_dump_host = "localhost";
80 mysql_dump_socket = "/run/mysqld/mysqld.sock";
81 backup_dir = "/var/backup/mysql";
82 db_exclude = [ "information_schema" "performance_schema" ];
83 mailcontent = "stdout";
84 mysql_dump_single_transaction = true;
87 systemd.timers.automysqlbackup = {
88 description = "automysqlbackup timer";
89 wantedBy = [ "timers.target" ];
91 OnCalendar = cfg.calendar;
96 systemd.services.automysqlbackup = {
97 description = "automysqlbackup service";
101 ExecStart = "${pkg}/bin/automysqlbackup ${configFile}";
105 environment.systemPackages = [ pkg ];
107 users.users.${user} = {
111 users.groups.${group} = { };
113 systemd.tmpfiles.rules = [
114 "d '${cfg.config.backup_dir}' 0750 ${user} ${group} - -"
117 services.mysql.ensureUsers = optional (config.services.mysql.enable && cfg.config.mysql_dump_host == "localhost") {
119 ensurePermissions = {
120 "*.*" = "SELECT, SHOW VIEW, TRIGGER, LOCK TABLES, EVENT";