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