Quartz实现Oracle定时备份

spring集成quartz


<!-- quartzConfig.xml,在spring配置文件中通过import引入该文件即可-->

<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
    <!-- 执行的类 -->
    <bean id="dataBackup" class="com.itdage.util.DataBackup">
    <!-- 定义bean和bean中的方法 -->
    <bean id="mijdfb" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 要执行的类名称 -->
        <property name="targetObject" ref="dataBackup">
        <!-- 要执行的方法名称 -->
        <property name="targetMethod" value="backup">
    </bean>
    <!-- 调度触发器 -->
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="mijdfb">
                <!-- 执行的时间配置-->
        <property name="cronExpression" value="0 15 21 * * ?">
    </bean>
    <!-- 调度工厂 -->
    <bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronTrigger" />
            </list>
        </property>
    </bean>
</beans>

Java实现Oracle数据库备份


import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Component;

@Component
public class DataBackup {

    /**
     * 自动备份
     */
    public void backup() {
        File file = new File("F:/备份");
        if (!file.exists()) {
            // 创建文件夹
            file.mkdirs();
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String info = null;// 备份打印的信息
        String fileName = format.format(new Date());
        try {
            System.out.println("开始备份数据....");
            String cmd = "exp system/123456@xe file=F:/backup/" + fileName + ".dmp full=y";
            Process process = Runtime.getRuntime().exec(cmd);
            InputStream inputStream = process.getInputStream();
                    //开启线程,清除缓存区,线程类在下面
            new Thread(new CleanStreamThread("info", inputStream, process)).start();
            InputStream errorStream = process.getErrorStream();
            BufferedReader buf = new BufferedReader(new InputStreamReader(errorStream));
            while ((info = buf.readLine()) != null) {
                System.out.println("error" + "->" + info);
            }
            if (buf.toString().contains("ORA-") && buf.toString().contains("EXP-")) {
                System.err.println("备份失败!");
                process.destroy();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }
}

//线程类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


public class CleanStreamThread implements Runnable{

    private String type;
    private InputStream inputStream;
    private Process process;
    public CleanStreamThread(String type, InputStream inputStream, Process process){
        this.type = type;
        this.inputStream = inputStream;
        this.process = process;
    }
    @Override
    public void run() {
        BufferedReader buf = new BufferedReader(new InputStreamReader(inputStream));
        String info = null;
        int i = 0;
        try {
            while((info = buf.readLine())!=null){
                System.out.println(type + "->" + info);
            }
            if (buf.toString().contains("ORA-") && buf.toString().contains("EXP-")) {
                System.err.println("备份失败!");
                process.destroy();
            } else {
                i = process.waitFor();
                System.out.println("备份完成,状态码: " + i);
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }finally{
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

注意事项

  1. spring集成quartz版本的限制。这里用的是quartz1.8.6版本。
  2. 及时清除process的缓存区,否则备份时会卡住。
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值