1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7
8let
9
10 inherit (lib)
11 concatMapStringsSep
12 concatStringsSep
13 isInt
14 isList
15 literalExpression
16 ;
17 inherit (lib)
18 mapAttrs
19 mapAttrsToList
20 mkDefault
21 mkEnableOption
22 mkIf
23 mkOption
24 mkRenamedOptionModule
25 optional
26 types
27 ;
28
29 cfg = config.services.automysqlbackup;
30 pkg = pkgs.automysqlbackup;
31 user = "automysqlbackup";
32 group = "automysqlbackup";
33
34 toStr =
35 val:
36 if isList val then
37 "( ${concatMapStringsSep " " (val: "'${val}'") val} )"
38 else if isInt val then
39 toString val
40 else if true == val then
41 "'yes'"
42 else if false == val then
43 "'no'"
44 else
45 "'${toString val}'";
46
47 configFile = pkgs.writeText "automysqlbackup.conf" ''
48 #version=${pkg.version}
49 # DONT'T REMOVE THE PREVIOUS VERSION LINE!
50 #
51 ${concatStringsSep "\n" (mapAttrsToList (name: value: "CONFIG_${name}=${toStr value}") cfg.config)}
52 '';
53
54in
55{
56 imports = [
57 (mkRenamedOptionModule
58 [ "services" "automysqlbackup" "config" ]
59 [ "services" "automysqlbackup" "settings" ]
60 )
61 ];
62
63 # interface
64 options = {
65 services.automysqlbackup = {
66
67 enable = mkEnableOption "AutoMySQLBackup";
68
69 calendar = mkOption {
70 type = types.str;
71 default = "01:15:00";
72 description = ''
73 Configured when to run the backup service systemd unit (DayOfWeek Year-Month-Day Hour:Minute:Second).
74 '';
75 };
76
77 settings = mkOption {
78 type =
79 with types;
80 attrsOf (oneOf [
81 str
82 int
83 bool
84 (listOf str)
85 ]);
86 default = { };
87 description = ''
88 automysqlbackup configuration. Refer to
89 {file}`''${pkgs.automysqlbackup}/etc/automysqlbackup.conf`
90 for details on supported values.
91 '';
92 example = literalExpression ''
93 {
94 db_names = [ "nextcloud" "matomo" ];
95 table_exclude = [ "nextcloud.oc_users" "nextcloud.oc_whats_new" ];
96 mailcontent = "log";
97 mail_address = "admin@example.org";
98 }
99 '';
100 };
101
102 };
103 };
104
105 # implementation
106 config = mkIf cfg.enable {
107
108 assertions = [
109 {
110 assertion = !config.services.mysqlBackup.enable;
111 message = "Please choose one of services.mysqlBackup or services.automysqlbackup.";
112 }
113 ];
114
115 services.automysqlbackup.config = mapAttrs (name: mkDefault) {
116 mysql_dump_username = user;
117 mysql_dump_host = "localhost";
118 mysql_dump_socket = "/run/mysqld/mysqld.sock";
119 backup_dir = "/var/backup/mysql";
120 db_exclude = [
121 "information_schema"
122 "performance_schema"
123 ];
124 mailcontent = "stdout";
125 mysql_dump_single_transaction = true;
126 };
127
128 systemd.timers.automysqlbackup = {
129 description = "automysqlbackup timer";
130 wantedBy = [ "timers.target" ];
131 timerConfig = {
132 OnCalendar = cfg.calendar;
133 AccuracySec = "5m";
134 };
135 };
136
137 systemd.services.automysqlbackup = {
138 description = "automysqlbackup service";
139 serviceConfig = {
140 User = user;
141 Group = group;
142 ExecStart = "${pkg}/bin/automysqlbackup ${configFile}";
143 };
144 };
145
146 environment.systemPackages = [ pkg ];
147
148 users.users.${user} = {
149 group = group;
150 isSystemUser = true;
151 };
152 users.groups.${group} = { };
153
154 systemd.tmpfiles.rules = [
155 "d '${cfg.config.backup_dir}' 0750 ${user} ${group} - -"
156 ];
157
158 services.mysql.ensureUsers =
159 optional (config.services.mysql.enable && cfg.config.mysql_dump_host == "localhost")
160 {
161 name = user;
162 ensurePermissions = {
163 "*.*" = "SELECT, SHOW VIEW, TRIGGER, LOCK TABLES, EVENT";
164 };
165 };
166
167 };
168}