J2EE uses quartz Task Scheduling for automatic MySQL Database Backup

Source: Internet
Author: User

Every time we back up a database on our website, we have to back up the database manually. Sometimes we will forget that there is no problem. If there is a problem, it will be troublesome. So simply write a backup program on your own.

The quartz task scheduling function is also integrated into spring by the way, and the source code is used to share with you:

1. databackupimp. Java

Package CN. hunqiu. data; import Java. io. ioexception; import Java. util. date; public class databackupimp {private stringbuffer copystring = new stringbuffer ("mysqldump"); // use MySQL's mysqldump tool, the premise is that you want to add the MySQL bin directory to your system pathprivate string laststr = NULL; private string user = "-u ****"; // *** private string Pwd = "-P ***"; // *** private string DB = "****"; // Private string Path = system. getproperty ("testhunqiu. root "); // The ing address of your website program private string sqlfilename = NULL; @ suppresswarnings (" deprecation ") Public databackupimp () {copystring. append (user ). append (""). append (PWD ). append (""). append (db ). append (">"); sqlfilename = path + "WEB-INF \ datacopy \" + dB + "_" + new date (). tolocalestring (). replaceall (":", "-") + ". SQL "; laststr = copystring + sqlfilename;} @ suppresswarnings (" deprecation ") Public databackupimp (string user, string PWD, string dB) {copystring. append (user ). append (""). append (PWD ). append (""). append (db ). append (">"); sqlfilename = path + "WEB-INF \ datacopy \" + dB + "_" + new date (). tolocalestring (). replaceall (":", "-") + ". SQL "; laststr = copystring + sqlfilename;} @ suppresswarnings (" deprecation ") Public void datacopy () throws ioexception {string temp = (" CMD/C "+ laststr ). replaceall ("//","\"\\\\\""). replaceall ("\", "\" \ "); // replace \ with the address separator/temp = temp for Windows. replacefirst ("\" "," "contains invalid runtime.getruntime(.exe C (temp); system. out. println ("database file backup has been completed ----------"); system. out. println ("file path:" + sqlfilename + "backup time" + new date (). tolocalestring ());}}

2. applicationcontextquartz. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: AOP = "http://www.springframework.org/schema/aop" xmlns: context = "http://www.springframework.org/schema/context" xmlns: MVC = "http://www.springframework.org/schema/context/mvc" xmlns: P = "http://www.springframework.org/schema/p" xmlns: Tx = "http://www.springframework.org/schema/tx" xmlns: util =" Http://www.springframework.org/schema/util "xsi: schemalocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframewor K.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsdhttp://www.springframework.org/schema/context/mvc http://www.springframework.org/schema/context/mvc/spring-mvc-3.0.xsd "> <! -- Quartz task scheduling --> <! -- Database backup --> <bean id = "datacopyquartz" class = "CN. hunqiu. data. databackupimp "/> <bean id =" hqdatacopyquartz "class =" org. springframework. scheduling. quartz. methodinvokingjobdetailfactorybean "> <property name =" targetobject "> <ref bean =" datacopyquartz "/> </property> <property name =" targetmethod "> <value> datacopy </value> </property> </bean> <bean id = "datacopyquartztime" class = "org. springframework. scheduling. quartz. cr Ontriggerbean "> <property name =" jobdetail "> <ref bean =" hqdatacopyquartz "/> </property> <property name =" cronexpression "> <! -- <Value> 0 0 23 **? </Value> --> <! -- Every day --> <value> 0 0 ***? </Value> <! -- Hourly --> </property> </bean> <bean id = "startquartz" lazy-init = "false" autowire = "no" class = "org. springframework. scheduling. quartz. schedulerfactorybean "> <property name =" triggers "> <list> <ref bean =" datacopyquartztime "/> </List> </property> </bean> </beans>

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.