From 1b3ac8039ee3bf45c375be89d8ded8902a39f1e2 Mon Sep 17 00:00:00 2001 From: Tube <7u83@mail.ru> Date: Mon, 13 Nov 2023 01:18:10 +0100 Subject: [PATCH] New bundle mysql_resore_conditional --- mysql.cf | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/mysql.cf b/mysql.cf index 720bb29..0ea4672 100644 --- a/mysql.cf +++ b/mysql.cf @@ -9,7 +9,8 @@ vars: "default_cfg" data => '{ "bind_address":"127.0.0.1", "user":"mysql", - "port":"3306" + "port":"3306", + "backup_dir":"/var/backups" }'; "cfg" data => mergedata(@(default_cfg),@(cfg_param)); @@ -71,10 +72,11 @@ files: "$(cfg_file)" - create => "true", + #create => "true", perms => m("644"), - edit_template => "$(sys.workdir)/inputs/$(def.wmde_libdir)/templates/my.cnf.mustache", - template_method => "mustache", + copy_from => seed_cp("$(sys.workdir)/inputs/$(def.wmde_libdir)/templates/my.cnf.mustache"), +# edit_template => "$(sys.workdir)/inputs/$(def.wmde_libdir)/templates/my.cnf.mustache", +# template_method => "mustache", handle => "mysql_cfg_created", depends_on => {"mysql_cfg_dir_created"}, classes => if_repaired("mysql_repaired"); @@ -123,6 +125,7 @@ vars: "j" string => storejson(@(cfg)); methods: + "any" usebundle => mysql(@(cfg)); "any" usebundle => wmde_install_packages(@(mysql.server_pkgs),"mysql_server"); files: @@ -315,25 +318,38 @@ bundle agent create_mysql_db(cfg) { classes: "type_$(mysql.type)" ; +# "do_restore" expression => isvariable("cfg[restore]"); + "do_restore" expression => strcmp("$(cfg[restore])","true"); vars: "db_name" string => "$(cfg[db_name])"; "db_user" string => "$(cfg[db_user])"; "host" string => "$(cfg[db_user_host])"; "db_pass" string => "$(cfg[db_pass])"; + type_mariadb:: "args" string => "-e \"CREATE DATABASE IF NOT EXISTS $(db_name); GRANT ALL PRIVILEGES ON $(db_name).* TO '$(db_user)'@'$(host)' IDENTIFIED BY '$(db_pass)'; \" "; type_mysql8:: "args" string => "-e \"CREATE DATABASE IF NOT EXISTS $(db_name); CREATE USER IF NOT EXISTS '$(db_user)'@'$(host)'; ALTER USER '$(db_user)'@'$(host)' IDENTIFIED BY '$(db_pass)'; GRANT RELOAD ON *.* to '$(db_user)'@'$(host)'; GRANT ALL PRIVILEGES ON $(db_name).* TO '$(db_user)'@'$(host)';\" "; +methods: + do_restore:: + "any" usebundle => restore_mysql_db_conditional(@(cfg),"$(mysql.cfg[backup_dir])/$(cfg[db_name])-dmp.sql"), + depends_on => {"mysql_$(cfg[db_name])_created"}; + commands: "$(mysql.mysql_cmd)" args => "$(args)", + handle => "mysql_$(cfg[db_name])_created", inform => "false"; reports: + do_restore:: + "DO RESTORE"; + !do_restore:: + "DO_NOT_RESTORE"; } @@ -346,3 +362,25 @@ commands: contain => wmde_cmd_useshell; } + +bundle agent restore_mysql_db_conditional(cfg,file) +{ +classes: + "backup_exists" expression => fileexists("$(file)"); +vars: + "table_exists" string => "mysql_$(cfg[db_name])_$(cfg[check_table])_exists"; +methods: + "any" usebundle => mysql_table_exists(@(cfg),"$(cfg[check_table])"); + "!$(table_exists)&backup_exists":: + "any" usebundle => restore_mysql_db(@(cfg),"$(file)"); +reports: + "conditional $(cfg[check_table])_exists"; + "FILE: $(file)"; + + "$(table_exists)":: + "$(table_exists) exists"; + "!$(table_exists)&backup_exists":: + "$(table_exists) does not existsi, can restore"; + +} +