@@ -6,10 +6,28 @@ let
66
77 inherit (pkgs) mysql gzip;
88
9− cfg = config.services.mysqlBackup ;
10− location = cfg.location ;
11− mysqlBackupCron = db : ''
12− ${cfg.period} ${cfg.user} ${mysql}/bin/mysqldump ${if cfg.singleTransaction then "--single-transaction" else ""} ${db} | ${gzip}/bin/gzip -c > ${location}/${db}.gz
9+ cfg = config.services.mysqlBackup;
10+ defaultUser = "mysqlbackup";
11+
12+ backupScript = ''
13+ set -o pipefail
14+ failed=""
15+ ${concatMapStringsSep "\n" backupDatabaseScript cfg.databases}
16+ if [ -n "$failed" ]; then
17+ echo "Backup of database(s) failed:$failed"
18+ exit 1
19+ fi
20+ '';
21+ backupDatabaseScript = db: ''
22+ dest="${cfg.location}/${db}.gz"
23+ if ${mysql}/bin/mysqldump ${if cfg.singleTransaction then "--single-transaction" else ""} ${db} | ${gzip}/bin/gzip -c > $dest.tmp; then
24+ mv $dest.tmp $dest
25+ echo "Backed up to $dest"
26+ else
27+ echo "Failed to back up to $dest"
28+ rm -f $dest.tmp
29+ failed="$failed ${db}"
30+ fi
1331 '';
1432
1533in
@@ -26,17 +44,16 @@ in
2644 '';
2745 };
2846
29− period = mkOption {
30− default = "15 01 * * *";
47+ calendar = mkOption {
48+ type = types.str;
49+ default = "01:15:00";
3150 description = ''
32− This option defines (in the format used by cron) when the
33− databases should be dumped.
34− The default is to update at 01:15 (at night) every day.
51+ Configured when to run the backup service systemd unit (DayOfWeek Year-Month-Day Hour:Minute:Second).
3552 '';
3653 };
3754
3855 user = mkOption {
39− default = "mysql";
56+ default = defaultUser;
4057 description = ''
4158 User to be used to perform backup.
4259 '';
@@ -66,16 +83,49 @@ in
6683
6784 };
6885
69− config = mkIf config.services.mysqlBackup.enable {
70−
71− services.cron.systemCronJobs = map mysqlBackupCron config.services.mysqlBackup.databases;
72−
73− system.activationScripts.mysqlBackup = stringAfter [ "stdio" "users" ]
74− ''
75− mkdir -m 0700 -p ${config.services.mysqlBackup.location}
76− chown ${config.services.mysqlBackup.user} ${config.services.mysqlBackup.location}
77− '';
78−
86+ config = mkIf cfg.enable {
87+ users.extraUsers = optionalAttrs (cfg.user == defaultUser) (singleton
88+ { name = defaultUser;
89+ isSystemUser = true;
90+ createHome = false;
91+ home = cfg.location;
92+ group = "nogroup";
93+ });
94+
95+ services.mysql.ensureUsers = [{
96+ name = cfg.user;
97+ ensurePermissions = with lib;
98+ let
99+ privs = "SELECT, SHOW VIEW, TRIGGER, LOCK TABLES";
100+ grant = db: nameValuePair "${db}.*" privs;
101+ in
102+ listToAttrs (map grant cfg.databases);
103+ }];
104+
105+ systemd = {
106+ timers."mysql-backup" = {
107+ description = "Mysql backup timer";
108+ wantedBy = [ "timers.target" ];
109+ timerConfig = {
110+ OnCalendar = cfg.calendar;
111+ AccuracySec = "5m";
112+ Unit = "mysql-backup.service";
113+ };
114+ };
115+ services."mysql-backup" = {
116+ description = "Mysql backup service";
117+ enable = true;
118+ serviceConfig = {
119+ User = cfg.user;
120+ PermissionsStartOnly = true;
121+ };
122+ preStart = ''
123+ mkdir -m 0700 -p ${cfg.location}
124+ chown -R ${cfg.user} ${cfg.location}
125+ '';
126+ script = backupScript;
127+ };
128+ };
79129 };
80130
81131}