Added bundle to create cronjob for backups
This commit is contained in:
		
							parent
							
								
									4d35d5c79e
								
							
						
					
					
						commit
						cb0dfb9bc4
					
				
							
								
								
									
										82
									
								
								mysql.cf
								
								
								
								
							
							
						
						
									
										82
									
								
								mysql.cf
								
								
								
								
							| 
						 | 
				
			
			@ -19,13 +19,18 @@ vars:
 | 
			
		|||
		"service_name" string => "mysql-server";
 | 
			
		||||
		"cfg_dir" string => "/usr/local/etc/mysql";
 | 
			
		||||
		"cfg_file" string => "/usr/local/etc/mysql/my.cnf";
 | 
			
		||||
		"mysql_cmd" string => "/usr/local/bin/mysql";
 | 
			
		||||
		"bin_dir" string => "/usr/local/bin";
 | 
			
		||||
	debian::
 | 
			
		||||
		"pkg"  slist => {"mariadb-server"};
 | 
			
		||||
		"client_pkgs" slist => {"mariadb-client"};
 | 
			
		||||
		"service_name" string => "mysql";
 | 
			
		||||
		"cfg_dir" string => "/etc/mysql";
 | 
			
		||||
		"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:
 | 
			
		||||
	"$(cfg_dir)/."
 | 
			
		||||
	create => "true",
 | 
			
		||||
| 
						 | 
				
			
			@ -45,7 +50,7 @@ files:
 | 
			
		|||
 | 
			
		||||
methods:
 | 
			
		||||
	"any" usebundle => wmde_install_packages(@(mysql.pkg),"mysql"),
 | 
			
		||||
		depends_on => { "mysql_cfg_created" };
 | 
			
		||||
		depends_on => { "mysql_cfg_created"};
 | 
			
		||||
 | 
			
		||||
services:
 | 
			
		||||
	"$(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
 | 
			
		||||
{
 | 
			
		||||
	useshell=>"useshell";
 | 
			
		||||
| 
						 | 
				
			
			@ -87,15 +100,6 @@ vars:
 | 
			
		|||
	"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 -e \\\"show tables"';
 | 
			
		||||
#	"cmdfile" string => hash("$cmd)","sha256");
 | 
			
		||||
 | 
			
		||||
#files:
 | 
			
		||||
#	"/tmp/$(cmdfile)"
 | 
			
		||||
#		create => "true",
 | 
			
		||||
#		content => "$(cmd)",
 | 
			
		||||
#		handle => "mysql_$(cmdfile)_created";
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
commands:
 | 
			
		||||
	"$(cmd)"
 | 
			
		||||
| 
						 | 
				
			
			@ -104,9 +108,59 @@ commands:
 | 
			
		|||
		module => "true";
 | 
			
		||||
 | 
			
		||||
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)
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			@ -124,7 +178,5 @@ commands:
 | 
			
		|||
		args => "$(args)",
 | 
			
		||||
		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:
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue