日期:2014-05-16 浏览次数:20590 次
user=root password=111 databasePath=D:/upload/backDatabase/ database=erp
/** * 读取properties文件的工具类 * @since 2011-10-5 * @author Jenhui * */ public class PropertiesReader { private String fileName; public PropertiesReader(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); } }
package com.lrh.utils; import java.io.*; import java.text.DateFormat; import java.util.Calendar; import java.util.Date; public class BackMySql { public static void exportDataBase(){ Date now = new Date(); DateFormat df= DateFormat.getDateTimeInstance(); String dbName = df.format(now)+".sql"; dbName=dbName.replaceAll(":", "_"); PropertiesReader pr=new PropertiesReader("config.properties"); String user = pr.readProperty("user"); String password = pr.readProperty("password"); String database = pr.readProperty("database"); String filepath = pr.readProperty("databasePath")+dbName; //System.out.println(filepath); String stmt1 = "mysqldump -u "+user+" -p"+password+" --set-charset=utf8 "+database; try{ Process process = Runtime.getRuntime().exec(stmt1); InputStream in = process.getInputStream(); InputStreamReader xx = new InputStreamReader(in, "utf8"); String inStr; StringBuffer sb = new StringBuffer(""); String outStr; BufferedReader br = new BufferedReader(xx); while ((inStr = br.readLine()) != null) { sb.append(inStr + "\r\n"); } outStr = sb.toString(); FileOutputStream fout = new FileOutputStream(filepath); OutputStreamWriter writer = new OutputStreamWriter(fout, "utf8"); writer.write(outStr); writer.flush(); in.close(); xx.close(); br.close(); writer.close(); fout.close(); }catch(IOException e){ e.printStackTrace(); } } }
public class TriggerImpl { public void BackMySQL(){ BackMySql.exportDataBase(); } }
<bean id="trigger" class="com.lrh.trigger.TriggerImpl"> </bean> <!-- 定义调用对象和调用对象的方法 --> <bean id="projobtask2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="trigger"/> </property> <property name="targetMethod"> <value>BackMySQL</value> </property> </bean> <!--定义触发时间 --> <bean id="timecard2" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="projobtask2"/> </property> <!-- cron表达式 --> <property name="cronExpression"> <!-- 每隔30分钟备份一次--> <value>0 0/30 * * * ?</value> </property> </bean> <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 --> <bean id="startQuertz2" lazy-init="false" autowire="no" class="org.spri