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(功能全面) 这个工具主要就是 ...
随机推荐
- [HNOI2004]宠物收养场 BZOJ1208 splay tree
题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...
- x0vncserver
x0vncserver -display :0 -passwordfile ~/.vnc/passwd
- 【转】Automated Testing Detail Test Plan
Automated Testing Detail Test PlanAutomated Testing DTP Overview This Automated Testing Detail Test ...
- Laravel5.1 目录结构解析
学习一门框架,首先要了解的就是目录结构.对目录结构清晰就可以着手学习了~这里不作新特性的介绍,权当目录结构手册看吧.若发现有何不恰当的地方请联系我哦~注:写本文时参照的是5.1.4版本 目录或文件 说 ...
- yum安装zabbix故障报错
装zabbix时报错 [root@zabbix ~]# rpm -ivh zabbix-server-mysql-2.2.3-1.el6.x86_64.rpm zabbix-web-mysql-2.2 ...
- Mock Server利器 - Moco
Moco介绍Moco独立运行所需环境如何运行Moco启动http服务启动https服务Moco HTTPs API配置如何在配置文件添加注释约定请求Body约定接口的uri约定请求参数约定请求方法约定 ...
- 一个基于QT简单登录对话框(带验证码功能)
1. 对话框样式 2. 源代码 ①. main.cpp #include <QtGui/QApplication> #include "QLoginDialog.h" ...
- NPOI2.0导出excel之添加样式、边框和表头
//优化后导出excel public System.IO.Stream ExcelStream(string search) // { var orderBusiniss = Containers. ...
- 解决ifconfig没有网卡问题
ifconfig -a root@kali:~# ifup eth0 ifup: unknown interface eth0 vim /etc/network/interfaces #自行添加网卡 ...
- java——保存书店每日交易记录程序设计
Books.java: 这个文件定义了一个Books类. 规定Books类拥有的属性:int id, String name, String publish, double price, int nu ...