使用java程序定時備份數據庫文件和恢復數據庫文件

注:要將mysql的bin目錄加入到環境變量Path中 

1、將MySql中的數據庫定時導出到文件中 備份 

<span style="font-size:18px;">
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import mail.MailSenderInfo;
import mail.SimpleMailSender;

import org.jeecgframework.web.demo.service.test.TaskDemoServiceI;
import org.springframework.stereotype.Service;

@Service("taskDemoService")
public class TaskDemoServiceImpl implements TaskDemoServiceI {

	public void works() {
		// 數據庫導出
		String user = "root"; // 數據庫帳號
		String password = "root"; // 登陸密碼
		String database = "oapms"; // 需要備份的數據庫名
		Date currentDate = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
		String sdfDate = sdf.format(currentDate);
		String filepath = "e:\\oapms_" + sdfDate + ".sql"; // 備份的路徑地址
		// 注意mysqldump是調用mysql數據庫的一個組件,在未在系統變量中聲明的話,要在這裏寫mysqldump的完整路徑
		String stmt1 = "mysqldump " + database + " -u " + user + " -p"
				+ password + " --result-file=" + filepath;
		try {
			Runtime.getRuntime().exec(stmt1);
			System.out.println("數據已導出到文件" + filepath + "中");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}</span>
2、將數據從磁盤上的文本文件還原到MySql中的數據庫 
<span style="font-size:18px;">package util;

import java.io.IOException;

public class Beifen {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// 還原MySql數據庫
		String filepath = "e:\\oapms_11-35-00.sql"; // 備份的路徑地址
		// 新建數據庫test

		String stmt1 = "mysqladmin -u root -proot create testbeifen";

		String stmt2 = "mysql -u root -proot testbeifen < " + filepath;
		String[] cmd = { "cmd", "/c", stmt2 };

		try {
			Runtime.getRuntime().exec(stmt1);
			Runtime.getRuntime().exec(cmd);
			System.out.println("數據已從 " + filepath + " 導入到數據庫中");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}</span>

附:定時任務的配置文件

<span style="font-size:18px;"><?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"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
	default-autowire="byName" default-lazy-init="false">


	<!-- 定時任務配置 scheduler 方式 註解 暫時不支持動態更新 -->
	<context:component-scan base-package="org.jeecgframework.core.timer" />
	<task:executor id="executor" pool-size="5" />
	<task:scheduler id="scheduler" pool-size="10" />
	<task:annotation-driven executor="executor"
		scheduler="scheduler" />
	<!-- 定時任務配置 quartz 可配置到管理界面 -->
<bean id="taskDemoServiceTaskJob2"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="taskDemoService" />
		<property name="targetMethod" value="works" />
		<property name="concurrent" value="true" />
	</bean>
	<bean id="taskDemoServiceTaskCronTrigger2" class="org.jeecgframework.core.timer.DataBaseCronTriggerBean">
		<property name="jobDetail" ref="taskDemoServiceTaskJob2" />
		<property name="cronExpression" value="0 * 15 * * ?" />
	</bean>
	<!-- 定時任務調度器 -->
	<bean id="schedulerFactory" lazy-init="false" autowire="no"
		class="org.jeecgframework.core.timer.DataBaseSchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="taskDemoServiceTaskCronTrigger2" />
			</list>
		</property>
	</bean>

</beans></span>




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章