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. HDU - 6416 :Rikka with Seam(DP & 前缀和 & 数学)

    pro:给定N*M的矩阵,现在让你在每一行删去一个位置,然后形成新N*(M-1)的矩阵,问有多少种不同的新的矩阵.需要满足相邻行删去的位置不大于K. (题目是01矩阵,其实任意矩阵都可以做,本题算法里 ...

  2. jenkins在windows系统下部署安装,使用

    首先需要从官网上下载下来war包,让进入tomcat中 启动tomcat,然后可以看一堆日志 再在网站输入 localhost:8080/jenkins就会进去下面界面: 会出现上面状况: 需要进入: ...

  3. 单片机关键字sfr和sbit的理解

    在单片机C语言编程中,扩充了两个关键字sfr和sbit.sfr(Special Function Register特殊功能寄存器的缩写),sbit(特殊功能寄存器位),与定义一般的int.char型变 ...

  4. git学习---去除版本控制

    本地这样去除文件夹 node_modules 的版本关联:执行:git rm -r --cached "node_modules/"提交: git commit -am 'remo ...

  5. 百度判断手机终端并自动跳转uaredirect.js代码及使用实例

    百度siteapp下的一款跳转的产品,使用起来很方便.你可以用这款JS跳转到手机版,也可以跳转到任何你想跳转的位置. js代码如下: function uaredirect(f) { try { if ...

  6. spring boot——常用注解

    @SpringBootApplication:申明让spring boot自动给程序进行必要的配置,这个配置等同于:@Configuration ,@EnableAutoConfiguration 和 ...

  7. 如何将一个Maven项目转化成一个Eclipse项目

    有时候我们需要将一个Maven项目导入到Eclipse中,直接作为一个普通的eclipse项目来导入是不行的,我们可以通过一个命令来实现:mvn eclipse:eclipse 1. 进入该Maven ...

  8. Win7 IIS 局域网中无法访问网页

    安装好iis后,在局域网中无法浏览网页一,关闭防火墙即可 或者建立入站规则 打开控制面板——window防火墙——高级设置 在入站规则上右键新建入站规则,选择端口然后下一步 选择tcp和特定端口在端口 ...

  9. 5W1H

    WHY   ——为什么做?原因与目标 WHAT ——做什么?目标与内容 WHO   ——谁去做?具体的执行者 WHEN ——什么时间做?执行时间 WHERE——在什么地方做?执行地点 HOW——怎样做 ...

  10. WSGI学习系列多种方式创建WebServer

    def application(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) ...