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.flatten (lib.mapAttrsToList (name: hcfg: [
86 dest = "mysql_replication/${name}/slave_init_commands";
91 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;
96 dest = "mysql_replication/${name}/mysqldump_remote";
103 password = ${hcfg.password}
107 dest = "mysql_replication/${name}/mysqldump";
108 permissions = "0400";
113 user = ${hcfg.dumpUser}
114 password = ${hcfg.dumpPassword}
118 dest = "mysql_replication/${name}/client";
119 permissions = "0400";
124 user = ${hcfg.dumpUser}
125 password = ${hcfg.dumpPassword}
132 systemCronJobs = lib.flatten (lib.mapAttrsToList (name: hcfg:
134 dataDir = "${cfg.base}/${name}/mysql";
135 backupDir = "${cfg.base}/${name}/mysql_backup";
136 backup_script = pkgs.writeScript "backup_mysql_${name}" ''
137 #!${pkgs.stdenv.shell}
141 filename=${backupDir}/$(${pkgs.coreutils}/bin/date -Iminutes).sql
142 ${hcfg.package}/bin/mysqldump \
143 --defaults-file=${config.secrets.location}/mysql_replication/${name}/mysqldump \
144 -S /run/mysqld_${name}/mysqld.sock \
148 --all-databases > $filename
149 ${pkgs.gzip}/bin/gzip $filename
151 u = pkgs.callPackage ./utils.nix {};
152 cleanup_script = pkgs.writeScript "cleanup_mysql_${name}" (u.exponentialDumps "sql.gz" backupDir);
154 "0 22,4,10,16 * * * root ${backup_script}"
155 "0 3 * * * root ${cleanup_script}"
159 system.activationScripts = lib.attrsets.mapAttrs' (name: hcfg:
160 lib.attrsets.nameValuePair "mysql_replication_${name}" {
161 deps = [ "users" "groups" ];
163 install -m 0700 -o mysql -g mysql -d ${cfg.base}/${name}/mysql
164 install -m 0700 -o mysql -g mysql -d ${cfg.base}/${name}/mysql_backup
168 environment.etc = lib.attrsets.mapAttrs' (name: hcfg:
169 lib.attrsets.nameValuePair "mysql/${name}_my.cnf" {
173 socket = /run/mysqld_${name}/mysqld.sock
174 datadir = ${cfg.base}/${name}/mysql/
175 log-bin = mariadb-bin
176 server-id = ${builtins.toString hcfg.serverId}
181 systemd.services = lib.attrsets.mapAttrs' (name: hcfg:
183 dataDir = "${cfg.base}/${name}/mysql";
185 lib.attrsets.nameValuePair "mysql_backup_${name}" {
186 description = "Mysql replication for ${name}";
187 wantedBy = [ "multi-user.target" ];
188 after = [ "network.target" ];
189 restartTriggers = [ config.environment.etc."mysql/${name}_my.cnf".source ];
190 unitConfig.RequiresMountsFor = dataDir;
193 if ! test -e ${dataDir}/mysql; then
194 ${hcfg.package}/bin/mysqldump \
195 --defaults-file=${config.secrets.location}/mysql_replication/${name}/mysqldump_remote \
202 --all-databases > ${dataDir}/initial.sql
204 ${hcfg.package}/bin/mysql_install_db \
205 --defaults-file=/etc/mysql/${name}_my.cnf \
207 --datadir=${dataDir} \
208 --basedir=${hcfg.package}
215 RuntimeDirectory = "mysqld_${name}";
216 RuntimeDirectoryMode = "0755";
217 SupplementaryGroups = "keys";
218 PermissionsStartOnly = true;
221 ExecStart = "${hcfg.package}/bin/mysqld --defaults-file=/etc/mysql/${name}_my.cnf --user=mysql --datadir=${dataDir} --basedir=${hcfg.package}";
224 sql_before = pkgs.writeText "mysql-initial-before" ''
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.location}/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;