This repository has no description
2.1 kB
86 lines
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 cfg = config.lass.mysqlBackup;
8
9 out = {
10 options.lass.mysqlBackup = api;
11 config = mkIf cfg.enable imp;
12 };
13
14 api = {
15 enable = mkEnableOption "mysqlBackup";
16 config = mkOption {
17 type = with types; attrsOf (submodule ({ config, ... }: {
18 options = {
19 name = mkOption {
20 type = types.str;
21 default = config._module.args.name;
22 };
23 startAt = mkOption {
24 type = with types; nullOr str; # TODO systemd.time(7)'s calendar event
25 default = "*-*-* 01:15:00";
26 };
27 user = mkOption {
28 type = str;
29 default = "root";
30 };
31 password = mkOption {
32 type = nullOr str;
33 default = null;
34 description = ''
35 path to a file containing the mysqlPassword for the specified user.
36 '';
37 };
38 databases = mkOption {
39 type = listOf str;
40 default = [];
41 };
42 location = mkOption {
43 type = str;
44 default = "/backups/sql_dumps";
45 };
46 };
47 }));
48 description = "configuration for mysqlBackup";
49 };
50 };
51
52 imp = {
53
54 services.mysql.ensureUsers = [
55 { ensurePermissions = { "*.*" = "ALL"; }; name = "root"; }
56 ];
57
58 systemd.services =
59 mapAttrs' (_: plan: nameValuePair "mysqlBackup-${plan.name}" {
60 path = with pkgs; [
61 mysql
62 gzip
63 ];
64 serviceConfig = rec {
65 ExecStart = start plan;
66 SyslogIdentifier = ExecStart.name;
67 Type = "oneshot";
68 User = plan.user;
69 };
70 startAt = plan.startAt;
71 }) cfg.config;
72 };
73
74
75 start = plan: let
76 backupScript = plan: db: ''
77 mkdir -p ${plan.location}
78 mysqldump -u ${plan.user} ${optionalString (plan.password != null) "-p$(cat ${plan.password})"} ${db} | gzip -c > ${plan.location}/${db}.gz
79 '';
80
81 in pkgs.pkgs.writeDash "mysqlBackup.${plan.name}" ''
82 ${concatMapStringsSep "\n" (backupScript plan) plan.databases}
83 '';
84
85
86in out