1. 程式人生 > >使用java程式定時備份資料庫檔案和恢復資料庫檔案

使用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>