parent
1fc591f9a5
commit
5f4df8e509
@ -0,0 +1,115 @@ |
||||
{ config, lib, pkgs, ... }: |
||||
|
||||
let |
||||
|
||||
inherit (lib) concatMapStringsSep concatStringsSep isInt isList literalExample; |
||||
inherit (lib) mapAttrs mapAttrsToList mkDefault mkEnableOption mkIf mkOption 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 |
||||
{ |
||||
# interface |
||||
options = { |
||||
services.automysqlbackup = { |
||||
|
||||
enable = mkEnableOption "AutoMySQLBackup"; |
||||
|
||||
calendar = mkOption { |
||||
type = types.str; |
||||
default = "01:15:00"; |
||||
description = '' |
||||
Configured when to run the backup service systemd unit (DayOfWeek Year-Month-Day Hour:Minute:Second). |
||||
''; |
||||
}; |
||||
|
||||
config = mkOption { |
||||
type = with types; attrsOf (either (either str (either int bool)) (listOf str)); |
||||
default = {}; |
||||
description = '' |
||||
automysqlbackup configuration. Refer to |
||||
<filename>''${pkgs.automysqlbackup}/etc/automysqlbackup.conf</filename> |
||||
for details on supported values. |
||||
''; |
||||
example = literalExample '' |
||||
{ |
||||
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"; |
||||
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; |
||||
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"; }; |
||||
}; |
||||
|
||||
}; |
||||
} |
@ -0,0 +1,34 @@ |
||||
import ./make-test.nix ({ pkgs, lib, ... }: |
||||
|
||||
{ |
||||
name = "automysqlbackup"; |
||||
meta.maintainers = [ lib.maintainers.aanderse ]; |
||||
|
||||
machine = |
||||
{ pkgs, ... }: |
||||
{ |
||||
services.mysql.enable = true; |
||||
services.mysql.package = pkgs.mysql; |
||||
services.mysql.initialDatabases = [ { name = "testdb"; schema = ./testdb.sql; } ]; |
||||
|
||||
services.automysqlbackup.enable = true; |
||||
}; |
||||
|
||||
testScript = '' |
||||
startAll; |
||||
|
||||
# Need to have mysql started so that it can be populated with data. |
||||
$machine->waitForUnit("mysql.service"); |
||||
|
||||
# Wait for testdb to be fully populated (5 rows). |
||||
$machine->waitUntilSucceeds("mysql -u root -D testdb -N -B -e 'select count(id) from tests' | grep -q 5"); |
||||
|
||||
# Do a backup and wait for it to start |
||||
$machine->startJob("automysqlbackup.service"); |
||||
$machine->waitForJob("automysqlbackup.service"); |
||||
|
||||
# wait for backup file and check that data appears in backup |
||||
$machine->waitForFile("/var/backup/mysql/daily/testdb"); |
||||
$machine->succeed("${pkgs.gzip}/bin/zcat /var/backup/mysql/daily/testdb/daily_testdb_*.sql.gz | grep hello"); |
||||
''; |
||||
}) |
@ -0,0 +1,32 @@ |
||||
{ stdenv, fetchurl, makeWrapper, mysql, mailutils, pbzip2, pigz, bzip2, gzip }: |
||||
|
||||
stdenv.mkDerivation rec { |
||||
pname = "automysqlbackup"; |
||||
version = "3.0_rc6"; |
||||
|
||||
src = fetchurl { |
||||
url = "mirror://sourceforge/automysqlbackup/AutoMySQLBackup/AutoMySQLBackup%20VER%203.0/automysqlbackup-v${version}.tar.gz"; |
||||
sha256 = "1h1wq86q6my1a682nr8pjagjhai4lxz967m17lhpw1vb116hd7l8"; |
||||
}; |
||||
|
||||
sourceRoot = "."; |
||||
|
||||
nativeBuildInputs = [ makeWrapper ]; |
||||
|
||||
installPhase = '' |
||||
mkdir -p $out/bin $out/etc |
||||
|
||||
cp automysqlbackup $out/bin/ |
||||
cp automysqlbackup.conf $out/etc/ |
||||
|
||||
wrapProgram $out/bin/automysqlbackup --prefix PATH : ${stdenv.lib.makeBinPath [ mysql mailutils pbzip2 pigz bzip2 gzip ]} |
||||
''; |
||||
|
||||
meta = with stdenv.lib; { |
||||
description = "A script to run daily, weekly and monthly backups for your MySQL database"; |
||||
homepage = https://sourceforge.net/projects/automysqlbackup/; |
||||
platforms = platforms.linux; |
||||
maintainers = [ maintainers.aanderse ]; |
||||
license = licenses.gpl2Plus; |
||||
}; |
||||
} |
Loading…
Reference in new issue