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(功能全面) 这个工具主要就是 ...
随机推荐
- Django---队列
1.队列介绍 任务队列用作跨线程或机器分配工作的机制. 任务队列的输入是称为任务的工作单元. 专用工作进程不断监视任务队列以执行新工作. Celery通过消息进行通信,通常使用经纪人(brokers) ...
- 树莓派安装开源智能家居系统 Domoticz
前言 最近闲来无事开始折腾自己的智能家居系统,对比了几种比较流行的开源智能家居系统,觉得 Domoticz 更适合,Domoticz的官方中文文档,虽然不是很完善但还是可以参考一下.需要注意的是下文用 ...
- android:id 中区别。。
一. android:id="@android:id/tabhost" 是调用系统内部的ID 和代码中 mTabContent = (FrameLayout) findView ...
- POJ 1000 A+B
#include <stdio.h> int main() { int a,b; scanf("%d %d",&a, &b); printf(" ...
- P1147 连续自然数和(思维题)
题目描述 对一个给定的自然数MM,求出所有的连续的自然数段,这些连续的自然数段中的全部数之和为MM. 例子:1998+1999+2000+2001+2002 = 100001998+1999+2000 ...
- nginx配置应用
启动nginxvim /usr/local/lnmp/nginx/conf/nginx.conf mkdir /wwwcd /wwwvim index.html www.westos.orgmkdir ...
- TCP通讯模型简单示例
1. TCP通讯模型 2. 服务器端 ① 创建socket,用函数socket() ② 绑定IP地址.端口号等信息到socket上,用函数bind() ③ 设置允许的最大连接数,用函数listen() ...
- java泛型&bean copy list
参考:https://www.oracle.com/technetwork/cn/articles/java/juneau-generics-2255374-zhs.html E:元素K:键N:数字T ...
- C++ vector类型要点总结(以及各种algorithm算法函数)
概述 C++内置的数组支持容器的机制,但是它不支持容器抽象的语义.要解决此问题我们自己实现这样的类.在标准C++中,用容器向量(vector)实现. 容器向量也是一个类模板.vector是C++标准模 ...
- Abbott's Revenge UVA - 816 (输出bfs路径)
题目链接:https://vjudge.net/problem/UVA-816 题目大意: 有一个最多包含9*9 个交叉点的迷宫.输入起点,离开起点时的朝向和终点,求一条最短路(多解时任意输出 一个即 ...