Added bundle to create cronjob for backups

This commit is contained in:
Tobias Herre 2023-10-16 09:24:11 +02:00
parent 4d35d5c79e
commit cb0dfb9bc4
1 changed files with 67 additions and 15 deletions

View File

@ -19,13 +19,18 @@ vars:
"service_name" string => "mysql-server"; "service_name" string => "mysql-server";
"cfg_dir" string => "/usr/local/etc/mysql"; "cfg_dir" string => "/usr/local/etc/mysql";
"cfg_file" string => "/usr/local/etc/mysql/my.cnf"; "cfg_file" string => "/usr/local/etc/mysql/my.cnf";
"mysql_cmd" string => "/usr/local/bin/mysql"; "bin_dir" string => "/usr/local/bin";
debian:: debian::
"pkg" slist => {"mariadb-server"}; "pkg" slist => {"mariadb-server"};
"client_pkgs" slist => {"mariadb-client"};
"service_name" string => "mysql"; "service_name" string => "mysql";
"cfg_dir" string => "/etc/mysql"; "cfg_dir" string => "/etc/mysql";
"cfg_file" string => "/etc/mysql/my.cnf"; "cfg_file" string => "/etc/mysql/my.cnf";
"mysql_cmd" string => "/usr/bin/mysql"; "bin_dir" string => "/usr/bin";
any::
"mysql_cmd" string => "$(bin_dir)/mysql";
"mysqldump_cmd" string => "$(bin_dir)/mysqldump";
files: files:
"$(cfg_dir)/." "$(cfg_dir)/."
create => "true", create => "true",
@ -45,7 +50,7 @@ files:
methods: methods:
"any" usebundle => wmde_install_packages(@(mysql.pkg),"mysql"), "any" usebundle => wmde_install_packages(@(mysql.pkg),"mysql"),
depends_on => { "mysql_cfg_created" }; depends_on => { "mysql_cfg_created"};
services: services:
"$(service_name)" "$(service_name)"
@ -63,6 +68,14 @@ reports:
} }
bundle agent install_mysql_client
{
methods:
"any" usebundle => wmde_install_packages(@(mysql.client_pkgs),"mysql_client"),
handle => "mysql_client_installed";
}
body contain mysql_cmd body contain mysql_cmd
{ {
useshell=>"useshell"; useshell=>"useshell";
@ -87,15 +100,6 @@ vars:
"classname" string => "mysql_$(dbdef[db_name])_$(table_name)_exists"; "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'; "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';
#"cmd" string => '"if mysql -e \\\"show tables"';
# "cmdfile" string => hash("$cmd)","sha256");
#files:
# "/tmp/$(cmdfile)"
# create => "true",
# content => "$(cmd)",
# handle => "mysql_$(cmdfile)_created";
commands: commands:
"$(cmd)" "$(cmd)"
@ -104,9 +108,59 @@ commands:
module => "true"; module => "true";
reports: reports:
# "CMD: $(cmd)";
} }
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';
reports:
# "CMD: $(cmd)";
}
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)";
}
bundle agent create_mysql_backup_cron_job(cfg,file,table_name,run)
{
classes:
"create_cron" expression => strcmp("$(run)","true");
vars:
"table_exists_cmd" string => '$(mysql.mysql_cmd) -u$(cfg[db_user]) -p$(cfg[db_pass]) $(cfg[db_name]) -e "show tables LIKE \'$(table_name)\'" $(cfg[db_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)
";
}
bundle agent create_mysql_db(cfg) bundle agent create_mysql_db(cfg)
{ {
@ -124,7 +178,5 @@ commands:
args => "$(args)", args => "$(args)",
inform => "false"; inform => "false";
#mysql -u <username> -p -e "USE <database_name>;" && mysql -u <username> -p -e "SHOW TABLES LIKE '<table_name>';" | grep -q '<table_name>' && exit 0 || exit 1
reports: reports:
} }