java实现数据库定时备份及删除
最近需要对服务器上的数据库进行定时备份和删除,在网上查了相关资料后完成了,现将相关代码记录下来。
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
// TODO Auto-generated method stub
logJobStart("backup day job");
PrintWriter printWriter = null;
BufferedReader bufferedReader = null;
try {
//初始化
Properties webPro = new Properties();
Properties dbPro = new Properties();
// 使用ClassLoader加载properties配置文件生成对应的输入流
InputStream web = BackupDayJob.class.getClassLoader().getResourceAsStream("webConfig.properties");
InputStream db = BackupDayJob.class.getClassLoader().getResourceAsStream("dbconfig.properties");
// 使用properties对象加载输入流
webPro.load(web);
dbPro.load(db);
//获取key对应的value值
String savePath = webPro.getProperty("savePath");
String databaseName = webPro.getProperty("databaseName");
String hostIP = webPro.getProperty("hostIP");
String userName = dbPro.getProperty("jdbc_username");
String password = dbPro.getProperty("jdbc_password");
String fileName = DateHelp.DateToString(new Date(), "yyyyMMdd") + ".sql";
File saveFile = new File(savePath);
if (!saveFile.exists()) {// 如果目录不存在
saveFile.mkdirs();// 创建文件夹
}
//首先删除7天前的数据
Date date = DateHelp.setDay(new Date(), -7);
File[] files = saveFile.listFiles();
for (int i=0;i<files.length;i++){
File file = files[i];
if (new Date(file.lastModified()).before(date)){
file.delete();
}
}
//备份新的数据
if(!savePath.endsWith(File.separator)){
savePath = savePath + File.separator;
}
printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(savePath + fileName), "utf8"));
Process process = Runtime.getRuntime().exec("mysqldump -h" + hostIP + " -u" + userName + " -p" + password + " --set-charset=UTF8 " + databaseName);
InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), "utf8");
bufferedReader = new BufferedReader(inputStreamReader);
String line;
while((line = bufferedReader.readLine())!= null){
printWriter.println(line);
}
printWriter.flush();
}catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
if (printWriter != null) {
printWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
logJobEnd("backup day job");
}
以上是删除和备份的代码,接下来还需要配置quartz设置一个定时任务。
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="定时文件路径" />
<bean id="backupDayJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="doc.web.job.BackupDayJob" />
</bean>
<bean id="backupTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="backupDayJob" />
<property name="cronExpression" value="0 0 3 * * ?" />
</bean>
<bean id="jobFactory" class="doc.web.job.JobFactory"></bean>
<bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="backupTrigger" />
</list>
</property>
<property name="jobFactory" ref="jobFactory"></property>
</bean>
</beans>