Adjust quartz regular backup database in Java spring

Source: Internet
Author: User

First, implement a Java program that can independently back up the database. The idea is to dynamically write the batch processing command to be executed into one bat file, and then call the Java program to execute this batch.

Execute the statement: mysqldump-u root-p123456 sshweb> E: \ mysql_backup \ 2013-1-3_15_29_00. SQL

1. Configuration File

JDBC. driverclassname = com. MySQL. JDBC. driverjdbc. url = JDBC: mysql: // localhost: 3306/sshweb? Chain Unicode = true & characterencoding = utf-8jdbc.username = rootjdbc. password = 123456jdbc. database = sshwebjdbc. backuppath = e \: \ elog4j_log \ mysql_backup \ // MySQL directory. lumu = E: // MySQL bin directory MySQL. binpath = e \ :\\ music_flash \ npmserv \ mysql5.1 \ bin

2. Tool class for reading configuration files

Package COM. util; import Java. util. properties; import Org. springframework. core. io. classpathresource; import Org. springframework. core. io. resource;/*** tool class for reading the properties file **/public class propertiesutil {private string filename; Public propertiesutil (string filename) {This. filename = filename;} Public String readproperty (string name) {resource res = new classpathresource (filename); properties P = new properties (); try {P. load (res. getinputstream (); // system. out. println (P. getproperty (name);} catch (exception e) {e. printstacktrace ();} return p. getproperty (name );}}

3. Back up the Java class of MySQL

Package COM. util; import Java. io. *; import Java. text. dateformat; import Java. util. date; import Org. apache. log4j. logger; import COM. config. config;/*** database tool class ** @ author administrator **/public class mysqlutil {static logger = logger. getlogger (mysqlutil. class);/*** backup database */public static void exportdatabase () {date now = new date (); dateformat df = dateformat. getdatetimeinstance (); string dbname = DF. format (now) + ". SQL "; dbname = dbname. replaceall (":", "_"); dbname = dbname. replaceall ("", "_"); propertiesutil Pr = new propertiesutil ("JDBC. properties "); string user = Pr. readproperty ("JDBC. username "); string Password = Pr. readproperty ("JDBC. password "); string database = Pr. readproperty ("JDBC. database "); string filepath = Pr. readproperty ("JDBC. backuppath ") + dbname; // create and execute the batch processing fileoutputstream fout = NULL; outputstreamwriter writer = NULL; try {string batfile = config. project_path + "// backup_databae.bat"; fout = new fileoutputstream (batfile); writer = new outputstreamwriter (fout, "utf8"); stringbuffer sb = new stringbuffer (""); sb. append (PR. readproperty ("MySQL. lumu ") +" \ r \ n "); sb. append ("cd" + PR. readproperty ("MySQL. binpath ") +" \ r \ n "); sb. append ("mysqldump-U" + User + "-P" + password + "" + database + ">" + fil Epath + "\ r \ n"); sb. append ("exit"); string outstr = cmdc ("CMD/C start" + batfile); logger.info ("database backed up successful! ");} Catch (ioexception e) {e. printstacktrace (); logger. Error (" failed to back up the database! ", E) ;}finally {writer = NULL; fout = NULL ;}}}

Quartz Integration

<! -- Database Scheduled Backup Service start --> <! -- Define the call object and call object method --> <bean id = "backupdatabasejobdetail" class = "org. springframework. scheduling. quartz. methodinvokingjobdetailfactorybean "> <property name =" targetobject "> <ref bean =" mysqlservice "/> </property> <property name =" targetmethod "> <value> backmysql </value> </property> </bean> <! -- Define the trigger time --> <bean id = "backupdatabasetrigger" class = "org. springframework. scheduling. quartz. crontriggerbean "> <property name =" jobdetail "> <ref bean =" backupdatabasejobdetail "/> </property> <! -- Cron expression --> <property name = "cronexpression"> <! -- Backup every 90 minutes --> <value> 0 0/90 ***? </Value> </property> </bean> <! -- If the general management class uses lazy-init = 'false', the container will execute the scheduler when it is started --> <bean id = "backupdatabasescheduler" lazy-init = "false" autowire = "No "Class =" org. springframework. scheduling. quartz. schedulerfactorybean "> <property name =" triggers "> <list> <ref bean =" backupdatabasetrigger "/> </List> </property> </bean> <! -- Database scheduled backup service end -->

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.