wmdeit-cf-wmdelib/mysql.cf

201 lines
5.0 KiB
CFEngine3
Raw Normal View History

2023-10-06 21:05:35 +00:00
#
#
#
bundle agent mysql(cfg_param)
2023-10-06 21:05:35 +00:00
{
vars:
"default_cfg" data => '{
"bind_address":"127.0.0.1",
"user":"mysql",
"port":"3306"
}';
"cfg" data => mergedata(@(default_cfg),@(cfg_param));
2023-10-06 21:05:35 +00:00
freebsd::
# "pkg" string => "mariadb106-server";
"pkg" string => "mysql80-server";
"service_name" string => "mysql-server";
"cfg_dir" string => "/usr/local/etc/mysql";
2023-10-06 21:05:35 +00:00
"cfg_file" string => "/usr/local/etc/mysql/my.cnf";
"bin_dir" string => "/usr/local/bin";
2023-10-06 21:05:35 +00:00
debian::
"pkg" slist => {"mariadb-server"};
"client_pkgs" slist => {"mariadb-client"};
2023-10-06 21:05:35 +00:00
"service_name" string => "mysql";
"cfg_dir" string => "/etc/mysql";
2023-10-06 21:05:35 +00:00
"cfg_file" string => "/etc/mysql/my.cnf";
"bin_dir" string => "/usr/bin";
any::
"mysql_cmd" string => "$(bin_dir)/mysql";
"mysqldump_cmd" string => "$(bin_dir)/mysqldump";
2023-10-06 21:05:35 +00:00
files:
"$(cfg_dir)/."
create => "true",
perms => mog("0755","root","root"),
handle => "mysql_cfg_dir_created";
2023-10-11 09:25:38 +00:00
2023-10-06 21:05:35 +00:00
"$(cfg_file)"
create => "true",
perms => m("644"),
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"},
2023-10-06 21:05:35 +00:00
classes => if_repaired("mysql_repaired");
methods:
2023-10-11 09:25:38 +00:00
"any" usebundle => wmde_install_packages(@(mysql.pkg),"mysql"),
depends_on => { "mysql_cfg_created"};
2023-10-06 21:05:35 +00:00
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"};
reports:
2023-10-06 21:05:35 +00:00
}
2023-10-11 09:25:38 +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";
"cmd" string => 'if mysql $(xargs) -e "show tables LIKE \'$(table_name)\'" $(dbdef[db_name]) | grep -q \'$(table_name)\'; then echo "+$(classname)"; else echo "-$(classname)"; fi';
commands:
"$(cmd)"
contain => mysql_cmd,
inform => "false",
module => "true";
reports:
}
bundle agent mysql_backup_all(cfg)
{
vars:
"cmd" string => '$(mysql.mysql_cmd) -N -e \'show databases\' | while read dbname; do $(mysql.mysqldump_cmd) --complete-insert --routines --triggers --single-transaction --max_allowed_packet=512M "$dbname" > $(cfg[backup_dir])/"$dbname".sql; done';
2023-10-11 09:25:38 +00:00
reports:
# "CMD: $(cmd)";
2023-10-11 09:25:38 +00:00
}
bundle agent mysql_backup_db(cfg,file)
{
vars:
"table_exists_cmd" string => '$(mysql.mysql_cmd) -e "show tables LIKE \'$(table_name)\'" $(cdfg[db_name]) | grep -q $(table_name)';
"cmd" string => "$(mysql.mysqldump_cmd) --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-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
#
bundle agent create_mysql_backup_cron_job(cfg,file,table_name,run)
{
classes:
"create_cron" expression => strcmp("$(run)","true");
vars:
2023-10-17 11:29:33 +00:00
"table_exists_cmd" string => '$(mysql.mysql_cmd) -u$(cfg[db_user]) -p$(cfg[db_pass]) $(cfg[db_name]) -e "show tables LIKE \'$(table_name)\'" | grep -q $(table_name)';
"backup_cmd" string => "$(mysql.mysqldump_cmd) --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/$(cfg[db_name])"
delete=>tidy;
create_cron::
"/etc/cron.d/$(cfg[db_name])"
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-10-06 21:05:35 +00:00
bundle agent create_mysql_db(cfg)
{
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])";
"args" string => "-e \"CREATE DATABASE IF NOT EXISTS $(db_name); GRANT ALL PRIVILEGES ON $(db_name).* TO '$(db_user)'@'$(host)' IDENTIFIED BY '$(db_pass)'; \" ";
commands:
debian::
"$(mysql.mysql_cmd)"
args => "$(args)",
inform => "false";
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;
}