zoukankan      html  css  js  c++  java
  • Oracle 数据库自动备份方案

    1、新建 backup.bat脚本

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

    创建 windows任务计划:

    3、编写拷贝程序

      1 import java.io.*;
      2 import java.util.*;
      3 import java.net.URL;
      4 import java.text.SimpleDateFormat;
      5 
      6 public class BackupFile{
      7 
      8     //1、获取当前路径
      9     //2、读取当前路径下的属性文件,获取源文件夹和目标文件夹所在目录
     10     //3、拷贝源文件夹下的所有内容至目标文件夹
     11     //4、清空源文件夹
     12     //5、保留30天以内的数据库备份
     13     public static void main(String args[]){
     14 
     15         //获取当前路径
     16         String path = getCurrentPath();
     17         File file = new File(path + "/dirIndex.properties" );
     18 
     19         if(!file.exists()){
     20             System.out.println("配置文件不存在!");
     21             System.exit(1);
     22         }
     23         
     24         //读取当前路径下的属性文件,获取源文件夹和目标文件夹所在目录
     25         Map<String,String> dirConfig = getCopyPath();
     26         
     27         String source = dirConfig.get("sourceDir");
     28         String dest = dirConfig.get("destinationDir");
     29 
     30         File sourceDir = new File(source);//源文件夹
     31         File destinationDir = new File(dest);//目标文件夹
     32         
     33         if(!sourceDir.exists()){
     34             System.out.println("源文件夹不存在!");
     35             System.exit(1);
     36         }
     37 
     38         if(!destinationDir.exists()){
     39             System.out.println("目标文件夹不存在!");
     40             //清空源文件夹
     41             deleteSourceFileChildren(source);
     42             System.exit(1);
     43         }
     44 
     45         //拷贝源文件夹下的所有文件至目标文件夹
     46         copyFiles(source,dest);
     47 
     48         //清空源文件夹
     49         deleteSourceFileChildren(source);
     50 
     51         //保留30天以内的数据库备份
     52         retainData(dest);
     53     }
     54     
     55     /**
     56     * 获取当前路径
     57     */
     58     public static String getCurrentPath(){
     59         String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
     60         return path;
     61     }
     62 
     63     /**
     64     * 读取当前路径下的属性文件,获取源文件夹和目标文件夹所在目录
     65     */
     66     public static Map<String,String> getCopyPath(){
     67         Map<String, String> propMap = new HashMap<String, String>();
     68 
     69         ClassLoader loader = Thread.currentThread().getContextClassLoader();
     70         URL url = loader.getResource("dirIndex.properties");
     71         InputStream in = null;
     72         try {
     73             in = url.openStream();
     74             Properties props = new Properties();
     75             props.load(in);
     76             // 加载属性列表
     77             Iterator<String> it = props.stringPropertyNames().iterator();
     78             while (it.hasNext()) {
     79                 String key = it.next();
     80                 String value = props.getProperty(key);
     81                 propMap.put(key, value);
     82             }
     83         } catch (IOException ioe) {
     84             ioe.printStackTrace();
     85         }finally{
     86             try {
     87                 in.close();
     88             } catch (IOException e) {
     89                 e.printStackTrace();
     90             }
     91         }
     92         return propMap;
     93     }
     94     
     95     /**
     96     * 拷贝源文件夹下的所有内容至目标文件夹
     97     */
     98     public static void copyFiles(String sourceFile,String destinationFile){
     99         Date date = new Date();
    100         SimpleDateFormat sbf = new SimpleDateFormat("yyyyMMdd");
    101         String fileName = sbf.format(date);
    102         System.out.println(fileName);
    103         String destFileStr = destinationFile + "/" + fileName;
    104         File destFile = new File(destFileStr);
    105         if(destFile.exists()){
    106             deleteDir(destFile);//如果存在,删除该目录
    107         }
    108         
    109         try{
    110             destFile.mkdirs();
    111             
    112             File inputFile = new File(sourceFile);
    113 
    114             File[] files = inputFile.listFiles();
    115             FileInputStream input = null;
    116             FileOutputStream output = null;
    117 
    118             long start = System.currentTimeMillis();
    119 
    120             copyFile(sourceFile,destFileStr);//拷贝所有内容至目标文件夹
    121 
    122             long end = System.currentTimeMillis();
    123             System.out.println("共耗时:" + (end - start) + "ms." );
    124             
    125         }catch(Exception e){
    126             e.printStackTrace();
    127         }
    128         
    129     }
    130 
    131     //清空源文件夹
    132     public static void deleteSourceFileChildren(String sourceFilePath){
    133         File file = new File(sourceFilePath);
    134         if(file.exists()){
    135             deleteDir(file);
    136             file.mkdirs();
    137         }
    138     }
    139 
    140     //保留30天以内的数据库备份
    141     public static void retainData(String dataPath){
    142         File file = new File(dataPath);
    143         File[] children = file.listFiles();
    144         
    145         try{
    146             Date date = new Date();
    147             SimpleDateFormat sbf = new SimpleDateFormat("yyyyMMdd");
    148             String dateStr = sbf.format(date);
    149             String dirNames[] = new String[children.length];
    150             for(int i=0; i<dirNames.length; i++){
    151                 File child = children[i];
    152                 if(child.isDirectory()){
    153                     dirNames[i] = child.getName();
    154                 }
    155             }
    156             System.out.println("文件夹长度:" + dirNames.length);
    157             //如果备份数量小于30,则不删除
    158             if(dirNames.length <= 30){
    159                 System.out.println("备份文件小于等于30份,不做删除。");
    160             }else{
    161             //如果备份数量大于30,则删除剩余的几个
    162                 List<Integer> dirNum = new ArrayList<Integer>();
    163                 for( String dirName : dirNames){
    164                     if(dirName.matches("[0-9]{8}")){
    165                         dirNum.add(Integer.parseInt(dirName));
    166                     }
    167                 }
    168                 Integer[] dirArr = new Integer[1];
    169                 dirArr = dirNum.toArray(dirArr);
    170                 Arrays.sort(dirArr);
    171                 dirArr = Arrays.copyOfRange(dirArr,0,dirArr.length - 30);
    172                 for(int i=0; i<dirArr.length; i++){
    173                     deleteDir(new File(dataPath + "/" + dirArr[i] + "" ) );
    174                 }
    175             }
    176         }catch(Exception e){
    177             e.printStackTrace();
    178         }
    179         
    180     }
    181 
    182     /**
    183     *    拷贝文件夹下所有内容(文件夹和文件)到另一个文件夹
    184     */
    185     public static boolean copyFile(String sourceStr,String destStr){
    186         File[] children = new File(sourceStr).listFiles();
    187         FileInputStream input = null;
    188         FileOutputStream output = null;
    189         try{
    190             for(int i=0; i<children.length; i++){
    191                 if(children[i].isDirectory()){
    192                     String newFilePath = destStr + "/" + children[i].getName();
    193                     File newFile = new File(newFilePath);
    194                     newFile.mkdir();
    195                     copyFile( (sourceStr + "/" + children[i].getName()),newFilePath);
    196                 }else{
    197                     input = new FileInputStream(sourceStr + "/" + children[i].getName() );
    198                     output = new FileOutputStream(destStr + "/" + children[i].getName() );
    199                     byte[] data = new byte[1024 * 512];
    200                     int len;
    201                     while( (len = input.read(data)) != -1 ){
    202                         output.write(data,0,len);
    203                     }
    204                     input.close();
    205                     output.close();
    206                 }
    207             }
    208         }catch(Exception e){
    209             e.printStackTrace();
    210             return false;
    211         }
    212         return true;
    213     }
    214 
    215     /**
    216     * 删除目录及目录下所有内容
    217     */
    218     public static boolean deleteDir(File file){
    219         if(file.isDirectory()){
    220             String[] children = file.list();
    221             for(int i=0; i<children.length; i++){
    222                 boolean isSuccess = deleteDir(new File(file,children[i]));
    223                 if(!isSuccess){
    224                     return isSuccess;
    225                 }
    226             }
    227         }
    228         return file.delete();
    229     }
    230 
    231 }
    View Code

    属性配置文件 dirIndex.properties:

    1 #源目录
    2 sourceDir=E:/backup/backuptempdir
    3 #目标目录
    4 destinationDir=H:/
    View Code

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

    4、编写拷贝脚本 copy.bat

    1 cd C:DBBACKUPBACKUPPRO
    2 java BackupFile
    View Code

    5、设定windows任务计划

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

  • 相关阅读:
    C# 监测每个方法的执行次数和占用时间(测试1)
    C# 依赖注入那些事儿
    SQL Server GROUP BY 后 拼接 字符串
    C# 根据Excel生成树
    C# 反射赋值
    C# 集合转换为DataTable
    Oracle 未能加载文件或程序集Oracle.DataAccess
    MySQL 各种主流 SQLServer 迁移到 MySQL 工具对比
    平衡树
    数据结构优化dp
  • 原文地址:https://www.cnblogs.com/ShawnYang/p/6672354.html
Copyright © 2011-2022 走看看