java Spring调整Quartz定时备份数据库

长平狐 发布于 2013/01/06 11:32
阅读 593
收藏 1

首先实现单独能够备份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.Driver
jdbc.url= jdbc:mysql://localhost:3306/sshweb?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
jdbc.database=sshweb
jdbc.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 --> 



原文链接:http://blog.csdn.net/sd0902/article/details/8461330
加载中
返回顶部
顶部