Oracle数据库自动备份方案

  • 格式:pdf
  • 大小:786.83 KB
  • 文档页数:7

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:\backup\backuptempdir" mkdir E:\backup\backuptempdir

14

15 set DATADIR=E:\backup\backuptempdir

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 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 getCopyPath(){

67 Map propMap = new HashMap();

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 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

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 dirNum = new ArrayList();

163 for( String dirName : dirNames){

164 if(dirName.matches("[0-9]{8}")){

165 dirNum.add(Integer.parseInt(dirName));

166 }