creaate_db can create cron jobs for backup

This commit is contained in:
Tube 2023-11-13 11:40:40 +01:00
parent ab21a02be7
commit 20d1366ac1
1 changed files with 65 additions and 14 deletions

View File

@ -314,12 +314,69 @@ PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
}
#
# 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::
"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)";
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)
";
}
bundle agent create_mysql_db(cfg)
{
classes:
"type_$(mysql.type)" ;
# "do_restore" expression => isvariable("cfg[restore]");
"do_restore" expression => strcmp("$(cfg[restore])","true");
"do_restore" expression => strcmp("$(cfg[db_restore])","true");
"do_backup" expression => strcmp("$(cfg[db_backup])","true");
vars:
"db_name" string => "$(cfg[db_name])";
"db_user" string => "$(cfg[db_user])";
@ -337,6 +394,11 @@ 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"};
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");
commands:
@ -346,10 +408,6 @@ commands:
inform => "false";
reports:
do_restore::
"DO RESTORE";
!do_restore::
"DO_NOT_RESTORE";
}
@ -368,19 +426,12 @@ 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";
"table_exists" string => "mysql_$(cfg[db_name])_$(cfg[db_check_table])_exists";
methods:
"any" usebundle => mysql_table_exists(@(cfg),"$(cfg[check_table])");
"any" usebundle => mysql_table_exists(@(cfg),"$(cfg[db_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";
}