java Spring調整Quartz定時備份資料庫

來源:互聯網
上載者:User

首先實現單獨能夠備份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 --> 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.