1 { pkgs, config, lib, ... }:
3 cfg = config.myServices.databasesReplication.mariadb;
6 options.myServices.databasesReplication.mariadb = {
7 enable = lib.mkEnableOption "Enable mariadb replication";
11 Base path to put the replications
14 hosts = lib.mkOption {
19 type = lib.types.attrsOf (lib.types.submodule {
21 package = lib.mkOption {
22 type = lib.types.package;
23 default = pkgs.mariadb;
25 Mariadb package for this host
28 serverId = lib.mkOption {
31 Server id to use for replication cluster (must be unique among the cluster!)
52 password = lib.mkOption {
58 dumpUser = lib.mkOption {
61 User who can do a dump
64 dumpPassword = lib.mkOption {
67 Password for the dump user
75 config = lib.mkIf cfg.enable {
77 description = "MySQL server user";
79 uid = config.ids.uids.mysql;
80 extraGroups = [ "keys" ];
82 users.groups.mysql.gid = config.ids.gids.mysql;
84 secrets.keys = lib.listToAttrs (lib.flatten (lib.mapAttrsToList (name: hcfg: [
85 (lib.nameValuePair "mysql_replication/${name}/slave_init_commands" {
90 CHANGE MASTER TO master_host="${hcfg.host}", master_port=${hcfg.port}, master_user="${hcfg.user}", master_password="${hcfg.password}", master_ssl=1, master_use_gtid=slave_pos;
94 (lib.nameValuePair "mysql_replication/${name}/mysqldump_remote" {
101 password = ${hcfg.password}
104 (lib.nameValuePair "mysql_replication/${name}/mysqldump" {
105 permissions = "0400";
110 user = ${hcfg.dumpUser}
111 password = ${hcfg.dumpPassword}
114 (lib.nameValuePair "mysql_replication/${name}/client" {
115 permissions = "0400";
120 user = ${hcfg.dumpUser}
121 password = ${hcfg.dumpPassword}
128 systemCronJobs = lib.flatten (lib.mapAttrsToList (name: hcfg:
130 dataDir = "${cfg.base}/${name}/mysql";
131 backupDir = "${cfg.base}/${name}/mysql_backup";
132 backup_script = pkgs.writeScript "backup_mysql_${name}" ''
133 #!${pkgs.stdenv.shell}
137 filename=${backupDir}/$(${pkgs.coreutils}/bin/date -Iminutes).sql
138 ${hcfg.package}/bin/mysqldump \
139 --defaults-file=${config.secrets.fullPaths."mysql_replication/${name}/mysqldump"} \
140 -S /run/mysqld_${name}/mysqld.sock \
144 --ignore-database=netdata \
145 --all-databases > $filename
146 ${pkgs.gzip}/bin/gzip $filename
148 u = pkgs.callPackage ./utils.nix {};
149 cleanup_script = pkgs.writeScript "cleanup_mysql_${name}" (u.exponentialDumps "sql.gz" backupDir);
151 "0 22,4,10,16 * * * root ${backup_script}"
152 "0 3 * * * root ${cleanup_script}"
156 system.activationScripts = lib.attrsets.mapAttrs' (name: hcfg:
157 lib.attrsets.nameValuePair "mysql_replication_${name}" {
158 deps = [ "users" "groups" ];
160 install -m 0700 -o mysql -g mysql -d ${cfg.base}/${name}/mysql
161 install -m 0700 -o mysql -g mysql -d ${cfg.base}/${name}/mysql_backup
165 environment.etc = lib.attrsets.mapAttrs' (name: hcfg:
166 lib.attrsets.nameValuePair "mysql/${name}_my.cnf" {
170 socket = /run/mysqld_${name}/mysqld.sock
171 datadir = ${cfg.base}/${name}/mysql/
172 log-bin = mariadb-bin
173 server-id = ${builtins.toString hcfg.serverId}
178 systemd.services = lib.attrsets.mapAttrs' (name: hcfg:
180 dataDir = "${cfg.base}/${name}/mysql";
182 lib.attrsets.nameValuePair "mysql_backup_${name}" {
183 description = "Mysql replication for ${name}";
184 wantedBy = [ "multi-user.target" ];
185 after = [ "network.target" ];
186 restartTriggers = [ config.environment.etc."mysql/${name}_my.cnf".source ];
187 unitConfig.RequiresMountsFor = dataDir;
190 if ! test -e ${dataDir}/mysql; then
191 if ! test -e ${dataDir}/initial.sql; then
192 ${hcfg.package}/bin/mysqldump \
193 --defaults-file=${config.secrets.fullPaths."mysql_replication/${name}/mysqldump_remote"} \
200 --all-databases > ${dataDir}/initial.sql
203 ${hcfg.package}/bin/mysql_install_db \
204 --defaults-file=/etc/mysql/${name}_my.cnf \
206 --datadir=${dataDir} \
207 --basedir=${hcfg.package}
214 RuntimeDirectory = "mysqld_${name}";
215 RuntimeDirectoryMode = "0755";
216 SupplementaryGroups = "keys";
217 PermissionsStartOnly = true;
220 ExecStart = "${hcfg.package}/bin/mysqld --defaults-file=/etc/mysql/${name}_my.cnf --user=mysql --datadir=${dataDir} --basedir=${hcfg.package}";
223 sql_before = pkgs.writeText "mysql-initial-before" ''
225 INSTALL SONAME 'auth_pam';
227 setupScript = pkgs.writeScript "mysql-setup" ''
228 #!${pkgs.runtimeShell} -e
230 if test -e ${dataDir}/initial.sql; then
233 ${dataDir}/initial.sql \
234 ${config.secrets.fullPaths."mysql_replication/${name}/slave_init_commands"} \
235 | ${hcfg.package}/bin/mysql \
236 --defaults-file=/etc/mysql/${name}_my.cnf \
237 -S /run/mysqld_${name}/mysqld.sock \
239 rm -f ${dataDir}/initial.sql
244 # initial dump can take a long time
245 TimeoutStartSec="infinity";
246 TimeoutStopSec = 120;