about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/backup/automysqlbackup.nix
blob: 27bbff813b105b4a25fa17ab8e4b5502369a049d (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
{ config, lib, pkgs, ... }:

let

  inherit (lib) concatMapStringsSep concatStringsSep isInt isList literalExpression;
  inherit (lib) mapAttrs mapAttrsToList mkDefault mkEnableOption mkIf mkOption mkRenamedOptionModule optional types;

  cfg = config.services.automysqlbackup;
  pkg = pkgs.automysqlbackup;
  user = "automysqlbackup";
  group = "automysqlbackup";

  toStr = val:
    if isList val then "( ${concatMapStringsSep " " (val: "'${val}'") val} )"
    else if isInt val then toString val
    else if true == val then "'yes'"
    else if false == val then "'no'"
    else "'${toString val}'";

  configFile = pkgs.writeText "automysqlbackup.conf" ''
    #version=${pkg.version}
    # DONT'T REMOVE THE PREVIOUS VERSION LINE!
    #
    ${concatStringsSep "\n" (mapAttrsToList (name: value: "CONFIG_${name}=${toStr value}") cfg.config)}
  '';

in
{
  imports = [
    (mkRenamedOptionModule [ "services" "automysqlbackup" "config" ] [ "services" "automysqlbackup" "settings" ])
  ];

  # interface
  options = {
    services.automysqlbackup = {

      enable = mkEnableOption (lib.mdDoc "AutoMySQLBackup");

      calendar = mkOption {
        type = types.str;
        default = "01:15:00";
        description = lib.mdDoc ''
          Configured when to run the backup service systemd unit (DayOfWeek Year-Month-Day Hour:Minute:Second).
        '';
      };

      settings = mkOption {
        type = with types; attrsOf (oneOf [ str int bool (listOf str) ]);
        default = {};
        description = lib.mdDoc ''
          automysqlbackup configuration. Refer to
          {file}`''${pkgs.automysqlbackup}/etc/automysqlbackup.conf`
          for details on supported values.
        '';
        example = literalExpression ''
          {
            db_names = [ "nextcloud" "matomo" ];
            table_exclude = [ "nextcloud.oc_users" "nextcloud.oc_whats_new" ];
            mailcontent = "log";
            mail_address = "admin@example.org";
          }
        '';
      };

    };
  };

  # implementation
  config = mkIf cfg.enable {

    assertions = [
      { assertion = !config.services.mysqlBackup.enable;
        message = "Please choose one of services.mysqlBackup or services.automysqlbackup.";
      }
    ];

    services.automysqlbackup.config = mapAttrs (name: mkDefault) {
      mysql_dump_username = user;
      mysql_dump_host = "localhost";
      mysql_dump_socket = "/run/mysqld/mysqld.sock";
      backup_dir = "/var/backup/mysql";
      db_exclude = [ "information_schema" "performance_schema" ];
      mailcontent = "stdout";
      mysql_dump_single_transaction = true;
    };

    systemd.timers.automysqlbackup = {
      description = "automysqlbackup timer";
      wantedBy = [ "timers.target" ];
      timerConfig = {
        OnCalendar = cfg.calendar;
        AccuracySec = "5m";
      };
    };

    systemd.services.automysqlbackup = {
      description = "automysqlbackup service";
      serviceConfig = {
        User = user;
        Group = group;
        ExecStart = "${pkg}/bin/automysqlbackup ${configFile}";
      };
    };

    environment.systemPackages = [ pkg ];

    users.users.${user} = {
      group = group;
      isSystemUser = true;
    };
    users.groups.${group} = { };

    systemd.tmpfiles.rules = [
      "d '${cfg.config.backup_dir}' 0750 ${user} ${group} - -"
    ];

    services.mysql.ensureUsers = optional (config.services.mysql.enable && cfg.config.mysql_dump_host == "localhost") {
      name = user;
      ensurePermissions = {
        "*.*" = "SELECT, SHOW VIEW, TRIGGER, LOCK TABLES, EVENT";

        # https://forums.mysql.com/read.php?10,668311,668315#msg-668315
        "function sys.extract_table_from_file_name" = "execute";
        "function sys.format_path" = "execute";
        "function sys.format_statement" = "execute";
        "function sys.extract_schema_from_file_name" = "execute";
        "function sys.ps_thread_account" = "execute";
        "function sys.format_time" = "execute";
        "function sys.format_bytes" = "execute";
      };
    };

  };
}