首先實現單獨能夠備份db的java程式,思路是執行動態把要執行的批處理命令寫入1個bat檔案中,然後調用java程式執行這個批處理
執行語句:mysqldump -u root -p123456 sshweb >E:\mysql_backup\2013-1-3_15_29_00.sql
1.設定檔
jdbc.driverClassName= com.mysql.jdbc.Driverjdbc.url= jdbc:mysql://localhost:3306/sshweb?useUnicode=true&characterEncoding=utf-8jdbc.username=rootjdbc.password=123456jdbc.database=sshwebjdbc.backupPath=E\:\\Elog4j_log\\mysql_backup\\//mysql的跟目錄mysql.lumu=E://mysql的bin目錄mysql.binPath=E\:\\music_flash\\NPMserv\\MySQL5.1\\bin
2.讀取設定檔的工具類
package com.util;import java.util.Properties;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.Resource;/** * 讀取properties檔案的工具類 * */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.備份的mysql的java類
package com.util;import java.io.*;import java.text.DateFormat;import java.util.Date;import org.apache.log4j.Logger;import com.config.Config;/** * 資料庫工具類 * * @author Administrator * */public class MysqlUtil {static Logger logger = Logger.getLogger(MysqlUtil.class);/** * 備份資料庫 */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;// 建立執行的批處理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+" >"+filepath+"\r\n");sb.append("exit");String outStr = sb.toString();writer.write(outStr);writer.flush();writer.close();fout.close();Runtime.getRuntime().exec(" cmd /c start " + batFile);logger.info("備份資料庫成功!");} catch (IOException e) {e.printStackTrace();logger.error("備份資料庫失敗!", e);}finally{writer=null;fout=null;}}}
整合quartz
<!-- 資料庫定時備份服務 start--> <!-- 定義調用對象和調用對象的方法 --> <bean id="backupDatabaseJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="mysqlService"/> </property> <property name="targetMethod"> <value>BackMySQL</value> </property> </bean> <!--定義觸發時間 --> <bean id="backupDatabaseTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="backupDatabaseJobDetail"/> </property> <!-- cron運算式 --> <property name="cronExpression"> <!-- 每隔90分鐘備份一次--> <value>0 0/90 * * * ?</value> </property> </bean> <!-- 總管理類 如果將lazy-init='false'那麼容器啟動就會執行發送器 --> <bean id="backupDatabaseScheduler" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="backupDatabaseTrigger"/> </list> </property> </bean> <!-- 資料庫定時備份服務 end -->