blob: 6616de07f14ec721806c63610d027130fc110b7a (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
{ pkgs, config, lib, ... }:
let
cfg = config.myServices.databasesReplication.mariadb;
in
{
options.myServices.databasesReplication.mariadb = {
enable = lib.mkEnableOption "Enable mariadb replication";
base = lib.mkOption {
type = lib.types.path;
description = ''
Base path to put the replications
'';
};
hosts = lib.mkOption {
default = {};
description = ''
Hosts to backup
'';
type = lib.types.attrsOf (lib.types.submodule {
options = {
package = lib.mkOption {
type = lib.types.package;
default = pkgs.mariadb;
description = ''
Mariadb package for this host
'';
};
serverId = lib.mkOption {
type = lib.types.int;
description = ''
Server id to use for replication cluster (must be unique among the cluster!)
'';
};
host = lib.mkOption {
type = lib.types.str;
description = ''
Host to connect to
'';
};
port = lib.mkOption {
type = lib.types.int;
description = ''
Port to connect to
'';
};
user = lib.mkOption {
type = lib.types.str;
description = ''
User to connect as
'';
};
password = lib.mkOption {
type = lib.types.str;
description = ''
Password to use
'';
};
dumpUser = lib.mkOption {
type = lib.types.str;
description = ''
User who can do a dump
'';
};
dumpPassword = lib.mkOption {
type = lib.types.str;
description = ''
Password for the dump user
'';
};
};
});
};
};
config = lib.mkIf cfg.enable {
myServices.chatonsProperties.hostings.mysql-replication = {
file.datetime = "2022-08-27T15:00:00";
hosting = {
name = "Mysql replication";
description = "Replication of mysql database";
website = "db-1.immae.eu";
status.level = "OK";
status.description = "OK";
registration.load = "OPEN";
install.type = "PACKAGE";
};
software = {
name = "MariaDB";
website = "https://mariadb.org/";
license.url = "https://github.com/MariaDB/server/blob/10.11/COPYING";
license.name = "GNU General Public License v2.0";
version = pkgs.mariadb.version;
source.url = "https://github.com/MariaDB/server";
};
};
users.users.mysql = {
description = "MySQL server user";
group = "mysql";
uid = config.ids.uids.mysql;
extraGroups = [ "keys" ];
};
users.groups.mysql.gid = config.ids.gids.mysql;
secrets.keys = lib.listToAttrs (lib.flatten (lib.mapAttrsToList (name: hcfg: [
(lib.nameValuePair "mysql_replication/${name}/slave_init_commands" {
user = "mysql";
group = "mysql";
permissions = "0400";
text = ''
CHANGE MASTER TO master_host="${hcfg.host}", master_port=${builtins.toString hcfg.port}, master_user="${hcfg.user}", master_password="${hcfg.password}", master_ssl=1, master_use_gtid=slave_pos;
START SLAVE;
'';
})
(lib.nameValuePair "mysql_replication/${name}/mysqldump_remote" {
permissions = "0400";
user = "root";
group = "root";
text = ''
[mysqldump]
user = ${hcfg.user}
password = ${hcfg.password}
'';
})
(lib.nameValuePair "mysql_replication/${name}/mysqldump" {
permissions = "0400";
user = "root";
group = "root";
text = ''
[mysqldump]
user = ${hcfg.dumpUser}
password = ${hcfg.dumpPassword}
'';
})
(lib.nameValuePair "mysql_replication/${name}/client" {
permissions = "0400";
user = "mysql";
group = "mysql";
text = ''
[client]
user = ${hcfg.dumpUser}
password = ${hcfg.dumpPassword}
'';
})
]) cfg.hosts));
services.cron = {
enable = true;
systemCronJobs = lib.flatten (lib.mapAttrsToList (name: hcfg:
let
dataDir = "${cfg.base}/${name}/mysql";
backupDir = "${cfg.base}/${name}/mysql_backup";
backup_script = pkgs.writeScript "backup_mysql_${name}" ''
#!${pkgs.stdenv.shell}
set -euo pipefail
filename=${backupDir}/$(${pkgs.coreutils}/bin/date -Iminutes).sql
${hcfg.package}/bin/mysqldump \
--defaults-file=${config.secrets.fullPaths."mysql_replication/${name}/mysqldump"} \
-S /run/mysqld_${name}/mysqld.sock \
--gtid \
--master-data \
--flush-privileges \
--ignore-database=netdata \
--all-databases > $filename
${pkgs.gzip}/bin/gzip $filename
'';
u = pkgs.callPackage ./utils.nix {};
cleanup_script = pkgs.writeScript "cleanup_mysql_${name}" (u.exponentialDumps "sql.gz" backupDir);
in [
"0 22,4,10,16 * * * root ${backup_script}"
"0 3 * * * root ${cleanup_script}"
]) cfg.hosts);
};
system.activationScripts = lib.attrsets.mapAttrs' (name: hcfg:
lib.attrsets.nameValuePair "mysql_replication_${name}" {
deps = [ "users" "groups" ];
text = ''
install -m 0700 -o mysql -g mysql -d ${cfg.base}/${name}/mysql
install -m 0700 -o mysql -g mysql -d ${cfg.base}/${name}/mysql_backup
'';
}) cfg.hosts;
environment.etc = lib.attrsets.mapAttrs' (name: hcfg:
lib.attrsets.nameValuePair "mysql/${name}_my.cnf" {
text = ''
[mysqld]
skip-networking
socket = /run/mysqld_${name}/mysqld.sock
datadir = ${cfg.base}/${name}/mysql/
log-bin = mariadb-bin
server-id = ${builtins.toString hcfg.serverId}
'';
}
) cfg.hosts;
environment.systemPackages = lib.mapAttrsToList (name: hcfg:
pkgs.writeScriptBin "mysql_backup_${name}" ''
#!${pkgs.stdenv.shell}
exec ${hcfg.package}/bin/mysql -S /run/mysqld_${name}/mysqld.sock "$@"
''
) cfg.hosts;
systemd.services = lib.attrsets.mapAttrs' (name: hcfg:
let
dataDir = "${cfg.base}/${name}/mysql";
in
lib.attrsets.nameValuePair "mysql_backup_${name}" {
description = "Mysql replication for ${name}";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
restartTriggers = [ config.environment.etc."mysql/${name}_my.cnf".source ];
unitConfig.RequiresMountsFor = dataDir;
preStart = ''
if ! test -e ${dataDir}/mysql; then
if ! test -e ${dataDir}/initial.sql; then
${hcfg.package}/bin/mysqldump \
--defaults-file=${config.secrets.fullPaths."mysql_replication/${name}/mysqldump_remote"} \
-h ${hcfg.host} \
-P ${builtins.toString hcfg.port} \
--ssl \
--gtid \
--flush-privileges \
--master-data \
--all-databases > ${dataDir}/initial.sql
fi
${hcfg.package}/bin/mysql_install_db \
--defaults-file=/etc/mysql/${name}_my.cnf \
--user=mysql \
--datadir=${dataDir} \
--basedir=${hcfg.package}
fi
'';
serviceConfig = {
User = "mysql";
Group = "mysql";
RuntimeDirectory = "mysqld_${name}";
RuntimeDirectoryMode = "0755";
SupplementaryGroups = "keys";
PermissionsStartOnly = true;
Type = "notify";
ExecStart = "${hcfg.package}/bin/mysqld --defaults-file=/etc/mysql/${name}_my.cnf --user=mysql --datadir=${dataDir} --basedir=${hcfg.package}";
ExecStartPost =
let
sql_before = pkgs.writeText "mysql-initial-before" ''
DROP DATABASE test;
INSTALL SONAME 'auth_pam';
'';
setupScript = pkgs.writeScript "mysql-setup" ''
#!${pkgs.runtimeShell} -e
if test -e ${dataDir}/initial.sql; then
cat \
${sql_before} \
${dataDir}/initial.sql \
${config.secrets.fullPaths."mysql_replication/${name}/slave_init_commands"} \
| ${hcfg.package}/bin/mysql \
--defaults-file=/etc/mysql/${name}_my.cnf \
-S /run/mysqld_${name}/mysqld.sock \
--user=root
rm -f ${dataDir}/initial.sql
fi
'';
in
"+${setupScript}";
# initial dump can take a long time
TimeoutStartSec="infinity";
TimeoutStopSec = 120;
};
}) cfg.hosts;
};
}
|