diff options
author | Aaron Andersen <aaron@fosslib.net> | 2019-04-12 19:06:11 -0400 |
---|---|---|
committer | Aaron Andersen <aaron@fosslib.net> | 2019-04-15 21:51:55 -0400 |
commit | 5f4df8e50935e219704389d8467f7edd363896d9 (patch) | |
tree | 5b9672047ac2032ee3509d7c7caef4d509a3efee /nixos | |
parent | 1fc591f9a5bd1b016b5d66dfab29560073955a14 (diff) | |
download | nixpkgs-5f4df8e50935e219704389d8467f7edd363896d9.tar nixpkgs-5f4df8e50935e219704389d8467f7edd363896d9.tar.gz nixpkgs-5f4df8e50935e219704389d8467f7edd363896d9.tar.bz2 nixpkgs-5f4df8e50935e219704389d8467f7edd363896d9.tar.lz nixpkgs-5f4df8e50935e219704389d8467f7edd363896d9.tar.xz nixpkgs-5f4df8e50935e219704389d8467f7edd363896d9.tar.zst nixpkgs-5f4df8e50935e219704389d8467f7edd363896d9.zip |
automysqlinit: init at 3.0_rc6
Diffstat (limited to 'nixos')
-rw-r--r-- | nixos/modules/module-list.nix | 1 | ||||
-rw-r--r-- | nixos/modules/services/backup/automysqlbackup.nix | 115 | ||||
-rw-r--r-- | nixos/tests/all-tests.nix | 1 | ||||
-rw-r--r-- | nixos/tests/automysqlbackup.nix | 34 |
4 files changed, 151 insertions, 0 deletions
diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix index 9982cdae9e3..2f09ad9e02e 100644 --- a/nixos/modules/module-list.nix +++ b/nixos/modules/module-list.nix @@ -188,6 +188,7 @@ ./services/audio/snapserver.nix ./services/audio/squeezelite.nix ./services/audio/ympd.nix + ./services/backup/automysqlbackup.nix ./services/backup/bacula.nix ./services/backup/borgbackup.nix ./services/backup/duplicati.nix diff --git a/nixos/modules/services/backup/automysqlbackup.nix b/nixos/modules/services/backup/automysqlbackup.nix new file mode 100644 index 00000000000..b845f370fb7 --- /dev/null +++ b/nixos/modules/services/backup/automysqlbackup.nix @@ -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"; }; + }; + + }; +} diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 8b27ff808e6..950eb01044f 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -23,6 +23,7 @@ in { acme = handleTestOn ["x86_64-linux"] ./acme.nix {}; atd = handleTest ./atd.nix {}; + automysqlbackup = handleTest ./automysqlbackup.nix {}; avahi = handleTest ./avahi.nix {}; bcachefs = handleTestOn ["x86_64-linux"] ./bcachefs.nix {}; # linux-4.18.2018.10.12 is unsupported on aarch64 beanstalkd = handleTest ./beanstalkd.nix {}; diff --git a/nixos/tests/automysqlbackup.nix b/nixos/tests/automysqlbackup.nix new file mode 100644 index 00000000000..ada104a34de --- /dev/null +++ b/nixos/tests/automysqlbackup.nix @@ -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"); + ''; +}) |