2023-10-06 21:05:35 +00:00
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2023-10-13 18:09:33 +00:00
|
|
|
bundle agent mysql(cfg_param)
|
2023-10-06 21:05:35 +00:00
|
|
|
{
|
|
|
|
vars:
|
2023-10-22 23:48:08 +00:00
|
|
|
|
2023-10-13 18:09:33 +00:00
|
|
|
"default_cfg" data => '{
|
|
|
|
"bind_address":"127.0.0.1",
|
|
|
|
"user":"mysql",
|
2023-11-13 00:18:10 +00:00
|
|
|
"port":"3306",
|
2023-11-14 18:47:25 +00:00
|
|
|
"backup_dir":"/var/backups",
|
|
|
|
"mariadb":true
|
2023-10-13 18:09:33 +00:00
|
|
|
}';
|
|
|
|
|
|
|
|
"cfg" data => mergedata(@(default_cfg),@(cfg_param));
|
2023-10-06 21:05:35 +00:00
|
|
|
|
|
|
|
freebsd::
|
2023-10-22 23:48:08 +00:00
|
|
|
"user" string => "mysql";
|
2023-10-22 21:29:39 +00:00
|
|
|
"server_pkgs" slist => {"mysql80-server"};
|
2023-10-22 11:39:35 +00:00
|
|
|
"client_pkgs" slist => {"mysql80-client"};
|
2023-11-16 20:24:38 +00:00
|
|
|
"type" string => "mysql8";
|
2023-11-14 18:47:25 +00:00
|
|
|
|
|
|
|
"server_pkgs" slist => {"mariadb106-server"}, if => strcmp("$(cfg[mariadb])","true");
|
|
|
|
"client_pkgs" slist => {"mariadb106-client"}, if => strcmp("$(cfg[mariadb])","true");
|
2023-11-16 20:24:38 +00:00
|
|
|
"type" string => "mariadb",if => strcmp("$(cfg[mariadb])","true");
|
2023-11-14 18:47:25 +00:00
|
|
|
|
2023-10-06 21:05:35 +00:00
|
|
|
"service_name" string => "mysql-server";
|
2023-10-13 18:09:33 +00:00
|
|
|
"cfg_dir" string => "/usr/local/etc/mysql";
|
2023-10-06 21:05:35 +00:00
|
|
|
"cfg_file" string => "/usr/local/etc/mysql/my.cnf";
|
2023-10-16 07:24:11 +00:00
|
|
|
"bin_dir" string => "/usr/local/bin";
|
2023-10-22 23:48:08 +00:00
|
|
|
"default_datadir" string => "/var/db/mysql";
|
2023-10-23 00:47:29 +00:00
|
|
|
"mysqld_cmd" string => "/usr/local/libexec/mysqld";
|
2023-10-06 21:05:35 +00:00
|
|
|
debian::
|
2023-10-22 23:48:08 +00:00
|
|
|
"user" string => "mysql";
|
2023-10-22 21:29:39 +00:00
|
|
|
"server_pkgs" slist => {"mariadb-server"};
|
2023-10-16 07:24:11 +00:00
|
|
|
"client_pkgs" slist => {"mariadb-client"};
|
2023-10-22 21:29:39 +00:00
|
|
|
"type" string => "mariadb";
|
2023-10-06 21:05:35 +00:00
|
|
|
"service_name" string => "mysql";
|
2023-10-13 18:09:33 +00:00
|
|
|
"cfg_dir" string => "/etc/mysql";
|
2023-10-06 21:05:35 +00:00
|
|
|
"cfg_file" string => "/etc/mysql/my.cnf";
|
2023-10-16 07:24:11 +00:00
|
|
|
"bin_dir" string => "/usr/bin";
|
2023-10-22 23:48:08 +00:00
|
|
|
"default_datadir" string => "/var/lib/mysql";
|
2023-10-23 00:47:29 +00:00
|
|
|
"mysqld_cmd" string => "/usr/sbin/mysqld";
|
2023-10-22 23:48:08 +00:00
|
|
|
|
2023-10-24 23:25:10 +00:00
|
|
|
fedora|centos::
|
2023-10-22 23:48:08 +00:00
|
|
|
"user" string => "mysql";
|
2023-10-22 21:29:39 +00:00
|
|
|
"server_pkgs" slist => {"mariadb-server"};
|
|
|
|
"client_pkgs" slist => {"mariadb-client"};
|
2024-02-16 11:08:59 +00:00
|
|
|
"server_pkgs" slist => {"MariaDB-server"};
|
|
|
|
"client_pkgs" slist => {"MariaDB-client"};
|
2023-10-22 21:29:39 +00:00
|
|
|
"type" string => "mariadb";
|
|
|
|
"service_name" string => "mariadb";
|
|
|
|
"cfg_dir" string => "/etc/mysql";
|
|
|
|
"cfg_file" string => "/etc/my.cnf";
|
2023-10-22 23:48:08 +00:00
|
|
|
"bin_dir" string => "/usr/bin";
|
|
|
|
"default_datadir" string => "/var/lib/mysql";
|
2023-10-23 00:47:29 +00:00
|
|
|
"mysqld_cmd" string => "/usr/sbin/mysqld";
|
2023-10-22 23:48:08 +00:00
|
|
|
|
2023-10-16 07:24:11 +00:00
|
|
|
any::
|
|
|
|
"mysql_cmd" string => "$(bin_dir)/mysql";
|
|
|
|
"mysqldump_cmd" string => "$(bin_dir)/mysqldump";
|
2023-10-23 00:47:29 +00:00
|
|
|
|
2023-10-16 07:24:11 +00:00
|
|
|
|
2023-10-06 21:05:35 +00:00
|
|
|
files:
|
2023-10-22 11:39:35 +00:00
|
|
|
freebsd::
|
|
|
|
"/etc/rc.conf.d/mysql"
|
|
|
|
create => "true",
|
|
|
|
content => "mysql_enable=YES";
|
|
|
|
|
|
|
|
|
|
|
|
any::
|
2023-10-13 18:09:33 +00:00
|
|
|
"$(cfg_dir)/."
|
|
|
|
create => "true",
|
2023-10-22 11:39:35 +00:00
|
|
|
perms => m("0755"),
|
2023-10-13 18:09:33 +00:00
|
|
|
handle => "mysql_cfg_dir_created";
|
|
|
|
|
2023-10-11 09:25:38 +00:00
|
|
|
|
2023-10-06 21:05:35 +00:00
|
|
|
"$(cfg_file)"
|
2023-11-13 00:18:10 +00:00
|
|
|
#create => "true",
|
2023-10-06 21:05:35 +00:00
|
|
|
perms => m("644"),
|
2023-11-13 00:18:10 +00:00
|
|
|
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",
|
2023-10-06 21:05:35 +00:00
|
|
|
handle => "mysql_cfg_created",
|
2023-10-13 18:09:33 +00:00
|
|
|
depends_on => {"mysql_cfg_dir_created"},
|
2023-10-06 21:05:35 +00:00
|
|
|
classes => if_repaired("mysql_repaired");
|
|
|
|
|
|
|
|
|
|
|
|
services:
|
|
|
|
"$(service_name)"
|
|
|
|
depends_on => {"mysql_pkgs_installed"},
|
|
|
|
service_policy => "start",
|
|
|
|
handle => "mysql_running";
|
|
|
|
|
|
|
|
mysql_repaired::
|
|
|
|
"$(service_name)"
|
|
|
|
service_policy => "restart",
|
|
|
|
depends_on => {"mysql_running","mysql_cfg_created"};
|
|
|
|
|
2023-10-13 18:09:33 +00:00
|
|
|
reports:
|
2023-10-06 21:05:35 +00:00
|
|
|
}
|
|
|
|
|
2023-10-22 21:29:39 +00:00
|
|
|
bundle agent install_mysql_server(cfg)
|
|
|
|
{
|
2023-10-23 00:47:29 +00:00
|
|
|
classes:
|
|
|
|
"type_$(mysql.type)" ;
|
|
|
|
|
2023-10-22 21:29:39 +00:00
|
|
|
vars:
|
|
|
|
|
|
|
|
"mysqld_idx" slist => getindices(@(cfg[settings][mysqld]));
|
|
|
|
"settings[mysqld][$(mysqld_idx)]" string => "$(cfg[settings][mysqld][$(mysqld_idx)])";
|
|
|
|
|
|
|
|
|
2023-10-23 00:47:29 +00:00
|
|
|
"datadir" string => ifelse(isvariable("cfg[settings][mysqld][datadir]"),
|
|
|
|
"$(cfg[settings][mysqld][datadir])",
|
|
|
|
"$(mysql.default_datadir)");
|
|
|
|
|
|
|
|
type_mariadb::
|
2023-10-28 12:02:30 +00:00
|
|
|
"install_db_exe" string => ifelse(fileexists("$(mysql.bin_dir)/mysql_install_db"),
|
|
|
|
"$(mysql.bin_dir)/mysql_install_db",
|
|
|
|
"$(mysql.bin_dir)/mariadb-install-db");
|
|
|
|
|
|
|
|
"install_db_cmd" string => "if [ ! -d $(datadir)/mysql ]; then $(install_db_exe) --user=$(mysql.user)>/dev/null ; fi";
|
2023-10-23 00:47:29 +00:00
|
|
|
type_mysql8::
|
|
|
|
"install_db_cmd" string => "if [ ! -d $(datadir)/mysql ]; then $(mysql.mysqld_cmd) --initialize-insecure --user=$(mysql.user); fi";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-10-22 21:29:39 +00:00
|
|
|
"j" string => storejson(@(cfg));
|
|
|
|
methods:
|
2023-11-13 00:18:10 +00:00
|
|
|
"any" usebundle => mysql(@(cfg));
|
2023-10-22 21:29:39 +00:00
|
|
|
"any" usebundle => wmde_install_packages(@(mysql.server_pkgs),"mysql_server");
|
|
|
|
|
|
|
|
files:
|
|
|
|
"$(mysql.cfg_file)"
|
|
|
|
copy_from => seed_cp("$(sys.workdir)/inputs/$(def.wmde_libdir)/templates/my.cnf.mustache"),
|
|
|
|
handle => "myslq_server_cfg_created",
|
|
|
|
depends_on => {"mysql_server_pkgs_installed"};
|
|
|
|
|
|
|
|
|
|
|
|
"$(mysql.cfg_file)"
|
|
|
|
create => "true",
|
|
|
|
perms => m("644"),
|
|
|
|
edit_line => set_variable_values_inix("$(this.bundle).settings","mysqld"),
|
|
|
|
depends_on => {"myslq_server_cfg_created"},
|
|
|
|
handle => "myslq_server_cfg_edited",
|
|
|
|
classes => if_repaired("mysql_server_repaired");
|
|
|
|
|
2023-10-23 00:47:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2023-10-22 23:48:08 +00:00
|
|
|
commands:
|
2023-10-23 00:47:29 +00:00
|
|
|
"$(install_db_cmd)"
|
2023-10-22 23:48:08 +00:00
|
|
|
handle=>"mysql_datadir_initialized",
|
|
|
|
contain => wmde_cmd_useshell,
|
|
|
|
inform => "false",
|
|
|
|
depends_on => {"myslq_server_cfg_edited"};
|
|
|
|
|
2023-10-22 21:29:39 +00:00
|
|
|
methods:
|
|
|
|
"any" usebundle => wmde_service("$(mysql.service_name)","mysql_server_kept","mysql_server_repaired"),
|
2023-10-22 23:48:08 +00:00
|
|
|
depends_on => {"mysql_datadir_initialized"};
|
2023-10-11 09:25:38 +00:00
|
|
|
|
2023-10-22 21:29:39 +00:00
|
|
|
reports:
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bundle edit_line set_variable_values_inix(tab, sectionName)
|
|
|
|
{
|
|
|
|
vars:
|
|
|
|
"index" slist => getindices("$(tab)[$(sectionName)]");
|
|
|
|
|
|
|
|
# Be careful if the index string contains funny chars
|
|
|
|
"cindex[$(index)]" string => canonify("$(index)");
|
|
|
|
|
|
|
|
classes:
|
|
|
|
"edit_$(cindex[$(index)])" not => strcmp("$($(tab)[$(sectionName)][$(index)])","dontchange"),
|
|
|
|
comment => "Create conditions to make changes";
|
|
|
|
|
|
|
|
field_edits:
|
|
|
|
|
|
|
|
# If the line is there, but commented out, first uncomment it
|
|
|
|
# "#+\s*$(index)\s*=.*"
|
|
|
|
# select_region => INI_section(escape("$(sectionName)")),
|
|
|
|
# edit_field => col("\s*=\s*","1","$(index)","set"),
|
|
|
|
# if => "edit_$(cindex[$(index)])";
|
|
|
|
|
|
|
|
# match a line starting like the key something
|
|
|
|
"\s*$(index)\s*=.*"
|
|
|
|
edit_field => col("\s*=\s*","2","$($(tab)[$(sectionName)][$(index)])","set"),
|
|
|
|
select_region => INI_section(escape("$(sectionName)")),
|
|
|
|
classes => results("bundle", "set_variable_values_ini_not_$(cindex[$(index)])"),
|
|
|
|
if => "edit_$(cindex[$(index)])";
|
|
|
|
|
|
|
|
delete_lines:
|
|
|
|
"!include.*";
|
|
|
|
|
|
|
|
insert_lines:
|
|
|
|
"[$(sectionName)]"
|
|
|
|
location => start,
|
|
|
|
comment => "Insert lines";
|
|
|
|
|
|
|
|
"$(index)=$($(tab)[$(sectionName)][$(index)])"
|
|
|
|
select_region => INI_section(escape("$(sectionName)")),
|
|
|
|
if => "!(set_variable_values_ini_not_$(cindex[$(index)])_kept|set_variable_values_ini_not_$(cindex[$(index)])_repaired).edit_$(cindex[$(index)])";
|
|
|
|
|
|
|
|
}
|
2023-10-16 07:24:11 +00:00
|
|
|
|
|
|
|
bundle agent install_mysql_client
|
|
|
|
{
|
|
|
|
methods:
|
|
|
|
"any" usebundle => wmde_install_packages(@(mysql.client_pkgs),"mysql_client"),
|
|
|
|
handle => "mysql_client_installed";
|
|
|
|
}
|
|
|
|
|
2023-10-11 09:25:38 +00:00
|
|
|
body contain mysql_cmd
|
|
|
|
{
|
|
|
|
useshell=>"useshell";
|
|
|
|
}
|
|
|
|
|
|
|
|
bundle agent mysql_table_exists(dbdef,table_name)
|
|
|
|
{
|
|
|
|
vars:
|
|
|
|
"pass_arg" string => ifelse ( isvariable("dbdef[db_pass]"),
|
|
|
|
"-p$(dbdef[db_pass])",""
|
|
|
|
);
|
|
|
|
"host_arg" string => ifelse ( isvariable("dbdef[db_host]"),
|
|
|
|
"-h$(dbdef[db_host])",""
|
|
|
|
);
|
|
|
|
"user_arg" string => ifelse ( isvariable("dbdef[db_user]"),
|
|
|
|
"-u$(dbdef[db_user])",""
|
|
|
|
);
|
|
|
|
|
|
|
|
"xargs" string => "$(pass_arg) $(host_arg) $(user_arg)";
|
|
|
|
|
|
|
|
|
|
|
|
"classname" string => "mysql_$(dbdef[db_name])_$(table_name)_exists";
|
|
|
|
|
2024-06-23 11:49:28 +00:00
|
|
|
"cmd" string => 'mysql $(xargs) -e "show tables LIKE \'$(table_name)\'" $(dbdef[db_name]) > /tmp/$(dbdef[db_name]).check && if grep -q \'$(table_name)\' /tmp/$(dbdef[db_name]).check ; then echo "+$(classname)"; else echo "-$(classname)"; fi';
|
2023-10-11 09:25:38 +00:00
|
|
|
|
|
|
|
commands:
|
|
|
|
"$(cmd)"
|
|
|
|
contain => mysql_cmd,
|
|
|
|
inform => "false",
|
|
|
|
module => "true";
|
|
|
|
|
2023-10-16 07:24:11 +00:00
|
|
|
reports:
|
|
|
|
}
|
|
|
|
|
|
|
|
bundle agent mysql_backup_all(cfg)
|
|
|
|
{
|
|
|
|
|
|
|
|
vars:
|
2024-06-19 10:50:11 +00:00
|
|
|
"cmd" string => '$(mysql.mysql_cmd) -N -e \'show databases\' | while read dbname; do $(mysql.mysqldump_cmd) --default-character-set=utf8mb4 --complete-insert --routines --triggers --single-transaction --max_allowed_packet=512M "$dbname" > $(cfg[backup_dir])/"$dbname".sql; done';
|
2023-10-16 07:24:11 +00:00
|
|
|
|
2023-10-11 09:25:38 +00:00
|
|
|
reports:
|
|
|
|
# "CMD: $(cmd)";
|
2023-10-16 07:24:11 +00:00
|
|
|
|
2023-10-11 09:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-16 07:24:11 +00:00
|
|
|
bundle agent mysql_backup_db(cfg,file)
|
|
|
|
{
|
2023-11-04 20:56:03 +00:00
|
|
|
classes:
|
|
|
|
"type_$(mysql.type)" ;
|
|
|
|
|
2023-10-16 07:24:11 +00:00
|
|
|
vars:
|
|
|
|
"table_exists_cmd" string => '$(mysql.mysql_cmd) -e "show tables LIKE \'$(table_name)\'" $(cdfg[db_name]) | grep -q $(table_name)';
|
2024-06-19 10:50:11 +00:00
|
|
|
"cmd" string => "$(mysql.mysqldump_cmd) --default-character-set=utf8mb4 --no-tablespaces --complete-insert --routines --triggers --single-transaction --max_allowed_packet=512M -h$(cfg[db_host]) -u$(cfg[db_user]) -p$(cfg[db_pass]) $(cfg[db_name]) >$(file)";
|
2023-10-16 07:24:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-17 07:54:06 +00:00
|
|
|
#
|
|
|
|
# Create cronjob
|
|
|
|
# @param cfg definition for database, db_user, db_host, db_pass, db_name
|
|
|
|
# @param file file to dump database into
|
|
|
|
# @table_name a table in the database which has to exist, if not no dump will be done
|
|
|
|
# @run "true" or "false", means create cron job or not
|
|
|
|
#
|
2023-10-16 07:24:11 +00:00
|
|
|
bundle agent create_mysql_backup_cron_job(cfg,file,table_name,run)
|
|
|
|
{
|
|
|
|
classes:
|
2023-11-04 20:56:03 +00:00
|
|
|
"type_$(mysql.type)" ;
|
2023-10-16 07:24:11 +00:00
|
|
|
"create_cron" expression => strcmp("$(run)","true");
|
|
|
|
vars:
|
2023-11-04 20:56:03 +00:00
|
|
|
|
2023-10-27 22:37:52 +00:00
|
|
|
"table_exists_cmd" string => '$(mysql.mysql_cmd) -h$(cfg[db_host]) -u$(cfg[db_user]) -p$(cfg[db_pass]) $(cfg[db_name]) -e "show tables LIKE \'$(table_name)\'" | grep -q $(table_name)';
|
2023-11-04 20:56:03 +00:00
|
|
|
|
|
|
|
type_mariadb::
|
2024-06-19 10:50:11 +00:00
|
|
|
"backup_cmd" string => "$(mysql.mysqldump_cmd) --default-character-set=utf8mb4 --complete-insert --routines --triggers --single-transaction --max_allowed_packet=512M -h$(cfg[db_host]) -u$(cfg[db_user]) -p$(cfg[db_pass]) $(cfg[db_name]) >$(file)";
|
2023-10-16 07:24:11 +00:00
|
|
|
|
2023-11-04 20:56:03 +00:00
|
|
|
type_mysql8::
|
|
|
|
"backup_cmd" string => "$(mysql.mysqldump_cmd) --set-gtid-purged=OFF --no-tablespaces --complete-insert --routines --triggers --single-transaction --max_allowed_packet=512M -h$(cfg[db_host]) -u$(cfg[db_user]) -p$(cfg[db_pass]) $(cfg[db_name]) >$(file)";
|
|
|
|
|
2023-10-16 07:24:11 +00:00
|
|
|
|
|
|
|
files:
|
|
|
|
!create_cron::
|
2023-10-20 18:01:42 +00:00
|
|
|
"/etc/cron.d/dump-mysql-db-$(cfg[db_name])"
|
2023-10-16 07:24:11 +00:00
|
|
|
delete=>tidy;
|
|
|
|
|
|
|
|
create_cron::
|
2023-10-20 18:01:42 +00:00
|
|
|
"/etc/cron.d/dump-mysql-db-$(cfg[db_name])"
|
2023-10-16 07:24:11 +00:00
|
|
|
perms => m("644"),
|
|
|
|
create => "true",
|
|
|
|
content => "
|
|
|
|
#
|
|
|
|
# /etc/cron.d/roundcube
|
|
|
|
#
|
|
|
|
SHELL=/bin/sh
|
|
|
|
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
|
|
|
|
|
|
|
|
0 * * * * root $(table_exists_cmd) && $(backup_cmd)
|
|
|
|
";
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-11-13 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Create cronjob
|
|
|
|
# @param cfg definition for database, db_user, db_host, db_pass, db_name
|
|
|
|
# @param file file to dump database into
|
|
|
|
# @run "true" or "false", means create cron job or not
|
|
|
|
#
|
|
|
|
bundle agent create_mysqldump_cron(cfg,file,run)
|
|
|
|
{
|
|
|
|
classes:
|
|
|
|
"type_$(mysql.type)" ;
|
|
|
|
"create_cron" expression => strcmp("$(run)","true");
|
|
|
|
vars:
|
|
|
|
"cron" string => ifelse(isvariable("cfg[db_cron]"),
|
|
|
|
"$(cfg[db_cron])","0 1 * * * ");
|
|
|
|
|
|
|
|
"table_exists_cmd" string => '$(mysql.mysql_cmd) -h$(cfg[db_host]) -u$(cfg[db_user]) -p$(cfg[db_pass]) $(cfg[db_name]) -e "show tables LIKE \'$(cfg[db_check_table])\'" | grep -q $(cfg[db_check_table])';
|
|
|
|
|
|
|
|
type_mariadb::
|
2024-06-19 10:50:11 +00:00
|
|
|
"backup_cmd" string => "$(mysql.mysqldump_cmd) --default-character-set=utf8mb4 --complete-insert --routines --triggers --single-transaction --max_allowed_packet=512M -h$(cfg[db_host]) -u$(cfg[db_user]) -p$(cfg[db_pass]) $(cfg[db_name]) >$(file)";
|
2023-11-13 10:40:40 +00:00
|
|
|
|
|
|
|
type_mysql8::
|
|
|
|
"backup_cmd" string => "$(mysql.mysqldump_cmd) --set-gtid-purged=OFF --no-tablespaces --complete-insert --routines --triggers --single-transaction --max_allowed_packet=512M -h$(cfg[db_host]) -u$(cfg[db_user]) -p$(cfg[db_pass]) $(cfg[db_name]) >$(file)";
|
|
|
|
|
|
|
|
|
|
|
|
files:
|
|
|
|
!create_cron::
|
|
|
|
"/etc/cron.d/mysqldump-$(cfg[db_name])"
|
|
|
|
delete=>tidy;
|
|
|
|
|
|
|
|
create_cron::
|
|
|
|
"/etc/cron.d/mysqldump-$(cfg[db_name])"
|
|
|
|
perms => m("644"),
|
|
|
|
create => "true",
|
|
|
|
content => "
|
|
|
|
#
|
|
|
|
# /etc/cron.d/mysqldump-$(cfg[db_name])
|
|
|
|
#
|
|
|
|
SHELL=/bin/sh
|
|
|
|
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
|
|
|
|
|
|
|
|
$(cron) root $(table_exists_cmd) && $(backup_cmd)
|
|
|
|
";
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-10-06 21:05:35 +00:00
|
|
|
bundle agent create_mysql_db(cfg)
|
|
|
|
{
|
2023-10-22 21:29:39 +00:00
|
|
|
classes:
|
2023-11-14 18:47:25 +00:00
|
|
|
|
2023-10-22 21:29:39 +00:00
|
|
|
"type_$(mysql.type)" ;
|
2023-11-13 00:18:10 +00:00
|
|
|
# "do_restore" expression => isvariable("cfg[restore]");
|
2023-11-13 10:40:40 +00:00
|
|
|
"do_restore" expression => strcmp("$(cfg[db_restore])","true");
|
|
|
|
"do_backup" expression => strcmp("$(cfg[db_backup])","true");
|
2023-10-06 21:05:35 +00:00
|
|
|
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])";
|
|
|
|
|
2023-11-13 00:18:10 +00:00
|
|
|
|
2023-11-14 18:47:25 +00:00
|
|
|
"args" string => "pnunf";
|
|
|
|
|
2023-10-22 21:29:39 +00:00
|
|
|
type_mariadb::
|
2023-10-06 21:05:35 +00:00
|
|
|
"args" string => "-e \"CREATE DATABASE IF NOT EXISTS $(db_name); GRANT ALL PRIVILEGES ON $(db_name).* TO '$(db_user)'@'$(host)' IDENTIFIED BY '$(db_pass)'; \" ";
|
|
|
|
|
2023-10-22 21:29:39 +00:00
|
|
|
type_mysql8::
|
2023-11-04 20:56:03 +00:00
|
|
|
"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)';\" ";
|
2023-10-22 21:29:39 +00:00
|
|
|
|
2023-11-13 00:18:10 +00:00
|
|
|
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"};
|
2023-11-13 10:40:40 +00:00
|
|
|
do_backup::
|
|
|
|
"any" usebundle => create_mysqldump_cron(@(cfg),"$(mysql.cfg[backup_dir])/$(cfg[db_name])-dmp.sql","true");
|
|
|
|
!do_backup::
|
|
|
|
"any" usebundle => create_mysqldump_cron(@(cfg),"$(mysql.cfg[backup_dir])/$(cfg[db_name])-dmp.sql","false");
|
|
|
|
|
2023-11-13 00:18:10 +00:00
|
|
|
|
2023-10-06 21:05:35 +00:00
|
|
|
commands:
|
2023-10-22 21:29:39 +00:00
|
|
|
|
|
|
|
"$(mysql.mysql_cmd)"
|
|
|
|
args => "$(args)",
|
2023-11-13 00:18:10 +00:00
|
|
|
handle => "mysql_$(cfg[db_name])_created",
|
2023-10-22 21:29:39 +00:00
|
|
|
inform => "false";
|
2023-10-06 21:05:35 +00:00
|
|
|
|
|
|
|
reports:
|
|
|
|
}
|
2023-10-17 11:29:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bundle agent restore_mysql_db(cfg,file)
|
|
|
|
{
|
|
|
|
commands:
|
|
|
|
"$(mysql.mysql_cmd)"
|
|
|
|
args => "-h$(cfg[db_host]) -u$(cfg[db_user]) -p$(cfg[db_pass]) $(cfg[db_name]) < $(file)",
|
|
|
|
contain => wmde_cmd_useshell;
|
|
|
|
}
|
|
|
|
|
2023-11-13 00:18:10 +00:00
|
|
|
|
|
|
|
bundle agent restore_mysql_db_conditional(cfg,file)
|
|
|
|
{
|
|
|
|
classes:
|
|
|
|
"backup_exists" expression => fileexists("$(file)");
|
|
|
|
vars:
|
2023-11-13 10:40:40 +00:00
|
|
|
"table_exists" string => "mysql_$(cfg[db_name])_$(cfg[db_check_table])_exists";
|
2023-11-13 00:18:10 +00:00
|
|
|
methods:
|
2023-11-16 20:24:38 +00:00
|
|
|
"any" usebundle => mysql_table_exists(@(cfg),"$(cfg[db_check_table])"),
|
|
|
|
handle=>"$(table_exists)_checked";
|
|
|
|
|
2023-11-13 00:18:10 +00:00
|
|
|
"!$(table_exists)&backup_exists"::
|
2023-11-16 20:24:38 +00:00
|
|
|
"any" usebundle => restore_mysql_db(@(cfg),"$(file)"),
|
|
|
|
depends_on=>{"$(table_exists)_checked"};
|
2023-11-13 00:18:10 +00:00
|
|
|
reports:
|
2024-06-23 11:49:28 +00:00
|
|
|
#"!$(table_exists)&backup_exists"::
|
|
|
|
# "DB DOES NOT EXISTS";
|
|
|
|
#"$(table_exists)&backup_exists"::
|
|
|
|
# "DB DOES EXISTS";
|
2023-11-13 00:18:10 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|