Oracle 数据库自动备份方案
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 数据库自动备份方案的更多相关文章
- Windows下Oracle数据库自动备份批处理脚本
expdb命令版本 @echo off REM ########################################################### REM # Windows Se ...
- Linux oracle数据库自动备份自动压缩脚本代码
Linux oracle数据库备份完成后可以自动压缩脚本代码. 复制代码代码如下: #!/bin/bash #backup.sh #edit: www.jbxue.com ##系统名称 sysname ...
- ORACLE数据库 自动备份 定时计划任务 windows
疑问为什么没有输入oracle 的数据库安装目录就能直接备份呢,可能是因为oracle默认安装c盘,在docs命令直接能操作吧,不信可以使用sqlplus试试. 一共分三步: 一.建立一个.bat 批 ...
- Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等
功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...
- oracle数据库自动备份脚本
::通过exp命令导出远程机器(192.168.2.1)上指定服务(orcl)指定用户(pmis)及密码(pmis)的数据 ::运行该脚本的机器必须安装oracle @echo off @echo [ ...
- ORACLE数据库自动备份压缩的批处理脚本 rar 7z
使用7z的版本: @echo offset filename="d:\backup\dbname_%date:~0,10%"set zipfile="d:\backup\ ...
- Oracle数据库的备份方法
1.引言 Oracle数据库的备份方法很多,无论使用那种备份方法,备份的目的都是为了在出现故障后能够以尽可能小的时间和代价恢复系统.比如使用export实用程序导出数据库对象.使用Oracle备份数据 ...
- Oracle数据库——用户、方案的创建与管理
一.涉及内容 1.掌握用户.方案与权限的基本概念. 2.熟练掌握用户操作的相关命令. 二.具体操作 (一)选择题: 1.关于方案的描述下列哪一项不正确?(C) A.表或索引等对象一定属于某一个方案 B ...
- 好用的SQLSERVER数据库自动备份工具SQLBackupAndFTP(功能全面)
转载:http://www.cnblogs.com/lyhabc/p/3322437.html 挺好用的SQLSERVER数据库自动备份工具SQLBackupAndFTP(功能全面) 这个工具主要就是 ...
随机推荐
- 11.Find All Numbers Disappeared in an Array(找出数组中缺失的数)
Level: Easy 题目描述: Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements ...
- 关于dubbo服务的xml配置文件报错的问题——The matching wildcard is strict, but no declaration can be found for element 'dubbo:application'
在配置dubbo服务的过程中,经常会遇到虽然程序能够跑起来,但是配置文件一堆红叉,虽然不影响功能,但是确实很让人恶心. 报错信息如下: 解决方案: 下载一个dubbo.xsd文件(就在dubbo.ja ...
- Codeforces Round #335 (Div. 2) B
B. Testing Robots time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- jupyter notebook初步使用
Jupyter Notebook(此前被称为 IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言.在本文中,我们将介绍 Jupyter notebook 的主要特性,以 ...
- 阿里Java开发规约(2)
本文是对阿里插件中规约的详细解释二,关于插件使用,请参考这里 及时清理不再使用的代码段或配置信息. 说明:对于垃圾代码或过时配置,坚决清理干净,避免程序过度臃肿,代码冗余 Positive examp ...
- k2安装LEDE
固件下载时请用Breed Web 恢复控制台恢复固件,步骤如下:1.到LEDE官方网站下载最新开发版固件2.Web Breed台刷写固件3.将一台能上网的路由器LAN口接至K2 WAN口,等待K2连上 ...
- vue.js依赖安装和引入
在项目开发过程中我们要安需安装依赖,安装方法 1.可以在项目的package.json文件中的dependencies写入依赖名称和依赖版本,然后打开命令行工具进入项目运行vue install安装 ...
- my.常用的话
1. 60普通副本+++ 60普通副本+++ 60普通副本+++ 50封妖+++50封妖+++50封妖+++ 60一本十妖+++ 60一本十妖+++ 60一本十妖+++ 60封妖+++60封妖+++6 ...
- linux查看当前用户登陆信息
@(Linux基础)[linux查看当前用户登陆信息] linux查看当前用户登陆信息 作为系统管理员,你可能经常会(在某个时候)需要查看系统中有哪些用户正在活动.有些时候,你甚至需要知道他(她)们正 ...
- 20篇关于商品管理系统和uml技术的相关文献
1.基于UML技术的商品管理系统设计与实现 2.UML技术在行业资源平台系统建模中的应用 3.基于JSP的商品信息管理系统设计与开发 4.基于UML技术的客户关系管理系统实现 5.商品管理系统 6.基 ...