1、新建 backup.bat脚本

 @echo off
echo ================================================
echo Windows环境下Oracle数据库的自动备份脚本
echo 1. 使用当前日期命名备份文件。
echo ================================================
::以“YYYYMMDD”格式取出当前时间。
set BACKUPDATE=%date:~0,4%%date:~5,2%%date:~8,2%
::设置用户名、密码和要备份的数据库。
set USER=用户名
set PASSWORD=密码
set DATABASE=数据库实例名
::创建备份目录。
if not exist "E:\backup\backuptempdir" mkdir E:\backup\backuptempdir set DATADIR=E:\backup\backuptempdir
expdp '%USER%/%PASSWORD%@%DATABASE% as sysdba' directory=dump_dir dumpfile=data_%BACKUPDATE%.dmp full=y;
exit

创建 windows任务计划:

3、编写拷贝程序

 import java.io.*;
import java.util.*;
import java.net.URL;
import java.text.SimpleDateFormat; public class BackupFile{ //1、获取当前路径
//2、读取当前路径下的属性文件,获取源文件夹和目标文件夹所在目录
//3、拷贝源文件夹下的所有内容至目标文件夹
//4、清空源文件夹
//5、保留30天以内的数据库备份
public static void main(String args[]){ //获取当前路径
String path = getCurrentPath();
File file = new File(path + "/dirIndex.properties" ); if(!file.exists()){
System.out.println("配置文件不存在!");
System.exit(1);
} //读取当前路径下的属性文件,获取源文件夹和目标文件夹所在目录
Map<String,String> dirConfig = getCopyPath(); String source = dirConfig.get("sourceDir");
String dest = dirConfig.get("destinationDir"); File sourceDir = new File(source);//源文件夹
File destinationDir = new File(dest);//目标文件夹 if(!sourceDir.exists()){
System.out.println("源文件夹不存在!");
System.exit(1);
} if(!destinationDir.exists()){
System.out.println("目标文件夹不存在!");
//清空源文件夹
deleteSourceFileChildren(source);
System.exit(1);
} //拷贝源文件夹下的所有文件至目标文件夹
copyFiles(source,dest); //清空源文件夹
deleteSourceFileChildren(source); //保留30天以内的数据库备份
retainData(dest);
} /**
* 获取当前路径
*/
public static String getCurrentPath(){
String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
return path;
} /**
* 读取当前路径下的属性文件,获取源文件夹和目标文件夹所在目录
*/
public static Map<String,String> getCopyPath(){
Map<String, String> propMap = new HashMap<String, String>(); ClassLoader loader = Thread.currentThread().getContextClassLoader();
URL url = loader.getResource("dirIndex.properties");
InputStream in = null;
try {
in = url.openStream();
Properties props = new Properties();
props.load(in);
// 加载属性列表
Iterator<String> it = props.stringPropertyNames().iterator();
while (it.hasNext()) {
String key = it.next();
String value = props.getProperty(key);
propMap.put(key, value);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return propMap;
} /**
* 拷贝源文件夹下的所有内容至目标文件夹
*/
public static void copyFiles(String sourceFile,String destinationFile){
Date date = new Date();
SimpleDateFormat sbf = new SimpleDateFormat("yyyyMMdd");
String fileName = sbf.format(date);
System.out.println(fileName);
String destFileStr = destinationFile + "/" + fileName;
File destFile = new File(destFileStr);
if(destFile.exists()){
deleteDir(destFile);//如果存在,删除该目录
} try{
destFile.mkdirs(); File inputFile = new File(sourceFile); File[] files = inputFile.listFiles();
FileInputStream input = null;
FileOutputStream output = null; long start = System.currentTimeMillis(); copyFile(sourceFile,destFileStr);//拷贝所有内容至目标文件夹 long end = System.currentTimeMillis();
System.out.println("共耗时:" + (end - start) + "ms." ); }catch(Exception e){
e.printStackTrace();
} } //清空源文件夹
public static void deleteSourceFileChildren(String sourceFilePath){
File file = new File(sourceFilePath);
if(file.exists()){
deleteDir(file);
file.mkdirs();
}
} //保留30天以内的数据库备份
public static void retainData(String dataPath){
File file = new File(dataPath);
File[] children = file.listFiles(); try{
Date date = new Date();
SimpleDateFormat sbf = new SimpleDateFormat("yyyyMMdd");
String dateStr = sbf.format(date);
String dirNames[] = new String[children.length];
for(int i=0; i<dirNames.length; i++){
File child = children[i];
if(child.isDirectory()){
dirNames[i] = child.getName();
}
}
System.out.println("文件夹长度:" + dirNames.length);
//如果备份数量小于30,则不删除
if(dirNames.length <= 30){
System.out.println("备份文件小于等于30份,不做删除。");
}else{
//如果备份数量大于30,则删除剩余的几个
List<Integer> dirNum = new ArrayList<Integer>();
for( String dirName : dirNames){
if(dirName.matches("[0-9]{8}")){
dirNum.add(Integer.parseInt(dirName));
}
}
Integer[] dirArr = new Integer[1];
dirArr = dirNum.toArray(dirArr);
Arrays.sort(dirArr);
dirArr = Arrays.copyOfRange(dirArr,0,dirArr.length - 30);
for(int i=0; i<dirArr.length; i++){
deleteDir(new File(dataPath + "/" + dirArr[i] + "" ) );
}
}
}catch(Exception e){
e.printStackTrace();
} } /**
* 拷贝文件夹下所有内容(文件夹和文件)到另一个文件夹
*/
public static boolean copyFile(String sourceStr,String destStr){
File[] children = new File(sourceStr).listFiles();
FileInputStream input = null;
FileOutputStream output = null;
try{
for(int i=0; i<children.length; i++){
if(children[i].isDirectory()){
String newFilePath = destStr + "/" + children[i].getName();
File newFile = new File(newFilePath);
newFile.mkdir();
copyFile( (sourceStr + "/" + children[i].getName()),newFilePath);
}else{
input = new FileInputStream(sourceStr + "/" + children[i].getName() );
output = new FileOutputStream(destStr + "/" + children[i].getName() );
byte[] data = new byte[1024 * 512];
int len;
while( (len = input.read(data)) != -1 ){
output.write(data,0,len);
}
input.close();
output.close();
}
}
}catch(Exception e){
e.printStackTrace();
return false;
}
return true;
} /**
* 删除目录及目录下所有内容
*/
public static boolean deleteDir(File file){
if(file.isDirectory()){
String[] children = file.list();
for(int i=0; i<children.length; i++){
boolean isSuccess = deleteDir(new File(file,children[i]));
if(!isSuccess){
return isSuccess;
}
}
}
return file.delete();
} }

属性配置文件 dirIndex.properties:

 #源目录
sourceDir=E:/backup/backuptempdir
#目标目录
destinationDir=H:/

编译 BackupFile.java 后形成 BackupFile.class 文件

4、编写拷贝脚本 copy.bat

 cd C:\DBBACKUP\BACKUPPRO
java BackupFile

5、设定windows任务计划

注意:很多目录和磁盘事先要有,如果没有,新建或者更改就行了。

Oracle 数据库自动备份方案的更多相关文章

  1. Windows下Oracle数据库自动备份批处理脚本

    expdb命令版本 @echo off REM ########################################################### REM # Windows Se ...

  2. Linux oracle数据库自动备份自动压缩脚本代码

    Linux oracle数据库备份完成后可以自动压缩脚本代码. 复制代码代码如下: #!/bin/bash #backup.sh #edit: www.jbxue.com ##系统名称 sysname ...

  3. ORACLE数据库 自动备份 定时计划任务 windows

    疑问为什么没有输入oracle 的数据库安装目录就能直接备份呢,可能是因为oracle默认安装c盘,在docs命令直接能操作吧,不信可以使用sqlplus试试. 一共分三步: 一.建立一个.bat 批 ...

  4. Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等

    功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...

  5. oracle数据库自动备份脚本

    ::通过exp命令导出远程机器(192.168.2.1)上指定服务(orcl)指定用户(pmis)及密码(pmis)的数据 ::运行该脚本的机器必须安装oracle @echo off @echo [ ...

  6. ORACLE数据库自动备份压缩的批处理脚本 rar 7z

    使用7z的版本: @echo offset filename="d:\backup\dbname_%date:~0,10%"set zipfile="d:\backup\ ...

  7. Oracle数据库的备份方法

    1.引言 Oracle数据库的备份方法很多,无论使用那种备份方法,备份的目的都是为了在出现故障后能够以尽可能小的时间和代价恢复系统.比如使用export实用程序导出数据库对象.使用Oracle备份数据 ...

  8. Oracle数据库——用户、方案的创建与管理

    一.涉及内容 1.掌握用户.方案与权限的基本概念. 2.熟练掌握用户操作的相关命令. 二.具体操作 (一)选择题: 1.关于方案的描述下列哪一项不正确?(C) A.表或索引等对象一定属于某一个方案 B ...

  9. 好用的SQLSERVER数据库自动备份工具SQLBackupAndFTP(功能全面)

    转载:http://www.cnblogs.com/lyhabc/p/3322437.html 挺好用的SQLSERVER数据库自动备份工具SQLBackupAndFTP(功能全面) 这个工具主要就是 ...

随机推荐

  1. 序列终结者 BZOJ 1251 Splay

    题目背景 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术 ...

  2. vue.js组件之j间的通讯一 子组件接受父祖件数据

    Vue2.0的三种常用传值方式.父传子.子传父.非父子组件传值 在Vue的框架开发的项目过程中,经常会用到组件来管理不同的功能,有一些公共的组件会被提取出来.这时必然会产生一些疑问和需求?比如一个组件 ...

  3. Codeforces Round #532 (Div. 2)- C(公式计算)

    NN is an experienced internet user and that means he spends a lot of time on the social media. Once ...

  4. P2906 [USACO08OPEN]牛的街区Cow Neighborhoods

    传送门 曼哈顿距离好像不好直接算,我们可以把牛的坐标转化一下以方便计算距离 (x,y) --> (x+y,x-y) 那么距离就可以表示成 $max(\left |x_1-x_2  \right ...

  5. nginx与 Keepalived高可用

    1.1 keepalived软件能干什么? Keepalived软件起初是专为LVS负载均衡软件设计的, 用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能 K ...

  6. spring boot CrossOrigin不生效?

    直接postman, curl, 浏览器访问后端接口, response header是不会自动加上Access-Control-Allow-Origin的. 需要在ajax中调用,客户端reques ...

  7. Message Unable to connect to SQL Server '(local)'

    最近在sql server 加了一些job,但是run job的时候发生了一下错误: ssage Unable to connect to SQL Server '(local)' 问题根源:调用 T ...

  8. gitlab 的安装、汉化、卸载

    新机 dell服务器 2核4G 官网: https://about.gitlab.com/install/ 1.本次安装选择版本v10.8.4 wget https://mirrors.tuna.ts ...

  9. robotframework自动化测试之测试数据

    相信很多人在做自动化测试的时候都会遇到一个问题,就是用例不能重复执行,比如名称不能重复,手机号码不能重复等等问题,或者在测试用例执行完后通过操作数据库把相关的数据删除: 那么怎么样让我们的测试用例能重 ...

  10. sql查询约束

    写作业的时候发现书上竟然找不到查询约束的语句,百度搜了好久的资料,终于查询成功,在这里记录下来 主键约束 SELECT   tab.name AS [表名],   idx.name AS [主键名称] ...