at 22.05-pre 3.4 kB view raw
1{ config, lib, pkgs, ... }: 2 3let 4 5 inherit (lib) concatMapStringsSep concatStringsSep isInt isList literalExpression; 6 inherit (lib) mapAttrs mapAttrsToList mkDefault mkEnableOption mkIf mkOption optional types; 7 8 cfg = config.services.automysqlbackup; 9 pkg = pkgs.automysqlbackup; 10 user = "automysqlbackup"; 11 group = "automysqlbackup"; 12 13 toStr = val: 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}'"; 19 20 configFile = pkgs.writeText "automysqlbackup.conf" '' 21 #version=${pkg.version} 22 # DONT'T REMOVE THE PREVIOUS VERSION LINE! 23 # 24 ${concatStringsSep "\n" (mapAttrsToList (name: value: "CONFIG_${name}=${toStr value}") cfg.config)} 25 ''; 26 27in 28{ 29 # interface 30 options = { 31 services.automysqlbackup = { 32 33 enable = mkEnableOption "AutoMySQLBackup"; 34 35 calendar = mkOption { 36 type = types.str; 37 default = "01:15:00"; 38 description = '' 39 Configured when to run the backup service systemd unit (DayOfWeek Year-Month-Day Hour:Minute:Second). 40 ''; 41 }; 42 43 config = mkOption { 44 type = with types; attrsOf (oneOf [ str int bool (listOf str) ]); 45 default = {}; 46 description = '' 47 automysqlbackup configuration. Refer to 48 <filename>''${pkgs.automysqlbackup}/etc/automysqlbackup.conf</filename> 49 for details on supported values. 50 ''; 51 example = literalExpression '' 52 { 53 db_names = [ "nextcloud" "matomo" ]; 54 table_exclude = [ "nextcloud.oc_users" "nextcloud.oc_whats_new" ]; 55 mailcontent = "log"; 56 mail_address = "admin@example.org"; 57 } 58 ''; 59 }; 60 61 }; 62 }; 63 64 # implementation 65 config = mkIf cfg.enable { 66 67 assertions = [ 68 { assertion = !config.services.mysqlBackup.enable; 69 message = "Please choose one of services.mysqlBackup or services.automysqlbackup."; 70 } 71 ]; 72 73 services.automysqlbackup.config = mapAttrs (name: mkDefault) { 74 mysql_dump_username = user; 75 mysql_dump_host = "localhost"; 76 mysql_dump_socket = "/run/mysqld/mysqld.sock"; 77 backup_dir = "/var/backup/mysql"; 78 db_exclude = [ "information_schema" "performance_schema" ]; 79 mailcontent = "stdout"; 80 mysql_dump_single_transaction = true; 81 }; 82 83 systemd.timers.automysqlbackup = { 84 description = "automysqlbackup timer"; 85 wantedBy = [ "timers.target" ]; 86 timerConfig = { 87 OnCalendar = cfg.calendar; 88 AccuracySec = "5m"; 89 }; 90 }; 91 92 systemd.services.automysqlbackup = { 93 description = "automysqlbackup service"; 94 serviceConfig = { 95 User = user; 96 Group = group; 97 ExecStart = "${pkg}/bin/automysqlbackup ${configFile}"; 98 }; 99 }; 100 101 environment.systemPackages = [ pkg ]; 102 103 users.users.${user} = { 104 group = group; 105 isSystemUser = true; 106 }; 107 users.groups.${group} = { }; 108 109 systemd.tmpfiles.rules = [ 110 "d '${cfg.config.backup_dir}' 0750 ${user} ${group} - -" 111 ]; 112 113 services.mysql.ensureUsers = optional (config.services.mysql.enable && cfg.config.mysql_dump_host == "localhost") { 114 name = user; 115 ensurePermissions = { "*.*" = "SELECT, SHOW VIEW, TRIGGER, LOCK TABLES"; }; 116 }; 117 118 }; 119}