RMAN备份ORACLE数据库脚本(包括全备与增量)

简介:

    一个客户原来的ORACLE数据库没有上RMAN备份,平时都是使用的EXPDP来导数据做为备份,今天叫我给他们写一个脚本来实现每天的增量,一周做一次全备。

    于是马上把原来把NBU自带的RMAN备份脚本拿出来改改。

    下面是脚本内容,实现每周周末一天全备,其它的时候都是增量备份,备份完的归档自己删除。

    注意其中的参数,因为环境不同需要做相应的修改。

 
  1. [oracle@11rac1 ~]$ cat hot_rman_backup.sh 
  2. #!/bin/sh 
  3. # $Header: hot_database_backup.sh,v 1.3 2010/08/04 17:56:02 $ 
  4. --------------------------------------------------------------------------- 
  5. # this script directory. 
  6. --------------------------------------------------------------------------- 
  7.  
  8. SHELL_HOME=/home/oracle 
  9.  
  10. --------------------------------------------------------------------------- 
  11. # Determine the time executing this script. 
  12. --------------------------------------------------------------------------- 
  13.  
  14. RMAN_DATA=`date '+%Y%m%d%H'
  15.  
  16. --------------------------------------------------------------------------- 
  17. # test log directiry .  If not, run the following lines. 
  18. --------------------------------------------------------------------------- 
  19.  
  20. if [ ! -d "$SHELL_HOME/log" ] 
  21. then 
  22.         mkdir -p $SHELL_HOME/log 
  23. fi 
  24.  
  25. --------------------------------------------------------------------------- 
  26. # Put output in <this file name>.out. Change as desired. 
  27. # Note: output directory requires write permission. 
  28. --------------------------------------------------------------------------- 
  29.  
  30. RMAN_LOG_FILE=${SHELL_HOME}/log/hot_rman_backup_${RMAN_DATA}.out 
  31.  
  32. --------------------------------------------------------------------------- 
  33. # You may want to delete the output file so that backup information does 
  34. not accumulate.  If notdelete the following lines. 
  35. --------------------------------------------------------------------------- 
  36.  
  37. if [ -f "$RMAN_LOG_FILE" ] 
  38. then 
  39.         rm -f "$RMAN_LOG_FILE" 
  40. fi 
  41.  
  42. --------------------------------------------------------------------------- 
  43. # rman image output directory. 
  44. --------------------------------------------------------------------------- 
  45.  
  46. RMAN_IMAGE_DIR=/home/oracle/rman 
  47.  
  48. echo "rman image output directory:${RMAN_IMAGE_DIR}">>$RMAN_LOG_FILE 
  49.   
  50. --------------------------------------------------------------------------- 
  51. # Log the start of this script. 
  52. --------------------------------------------------------------------------- 
  53.   
  54. echo Script $0 >> $RMAN_LOG_FILE 
  55. echo ==== started on `date` ==== >> $RMAN_LOG_FILE 
  56. echo >> $RMAN_LOG_FILE 
  57.   
  58. --------------------------------------------------------------------------- 
  59. Replace /db/oracle/product/ora102, below, with the Oracle home path. 
  60. --------------------------------------------------------------------------- 
  61.  
  62. ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1 
  63. export ORACLE_HOME 
  64.  
  65. --------------------------------------------------------------------------- 
  66. Replace ora102, below, with the Oracle SID of the target database
  67. --------------------------------------------------------------------------- 
  68.  
  69. ORACLE_SID=power1 
  70. export ORACLE_SID 
  71.  
  72.  
  73. --------------------------------------------------------------------------- 
  74. Set the target connect string. 
  75. Replace "sys/manager", below, with the target connect string. 
  76. --------------------------------------------------------------------------- 
  77.  
  78. TARGET_CONNECT_STR=sys/manager 
  79.   
  80. --------------------------------------------------------------------------- 
  81. Set the Oracle Recovery Manager name
  82. --------------------------------------------------------------------------- 
  83.  
  84. RMAN=$ORACLE_HOME/bin/rman 
  85.  
  86. --------------------------------------------------------------------------- 
  87. # Print out the value of the variables set by this script. 
  88. --------------------------------------------------------------------------- 
  89.  
  90. echo >> $RMAN_LOG_FILE 
  91. echo   "RMAN: $RMAN" >> $RMAN_LOG_FILE 
  92. echo   "ORACLE_SID: $ORACLE_SID" >> $RMAN_LOG_FILE 
  93. echo   "ORACLE_USER: $ORACLE_USER" >> $RMAN_LOG_FILE 
  94. echo   "ORACLE_HOME: $ORACLE_HOME" >> $RMAN_LOG_FILE 
  95.  
  96. --------------------------------------------------------------------------- 
  97. # Print out the value of the variables set by bphdb. 
  98. --------------------------------------------------------------------------- 
  99.  
  100. echo  >> $RMAN_LOG_FILE 
  101.  
  102. --------------------------------------------------------------------------- 
  103. # NOTE: This script assumes that the database is properly opened. If desired, 
  104. # this would be the place to verify that. 
  105. --------------------------------------------------------------------------- 
  106.  
  107. echo >> $RMAN_LOG_FILE 
  108. --------------------------------------------------------------------------- 
  109. # If this script is executed from a NetBackup schedule, NetBackup 
  110. # sets an NB_ORA environment variable based on the schedule type. 
  111. # The NB_ORA variable is then used to dynamically set BACKUP_TYPE 
  112. For example, when
  113. #     schedule type is                BACKUP_TYPE is 
  114. #     ----------------                -------------- 
  115. # Automatic Full                     INCREMENTAL LEVEL=0 
  116. # Automatic Differential Incremental INCREMENTAL LEVEL=1 
  117. # Automatic Cumulative Incremental   INCREMENTAL LEVEL=1 CUMULATIVE 
  118. #  
  119. For user initiated backups, BACKUP_TYPE defaults to incremental 
  120. level 0 (full).  To change the default for a user initiated 
  121. # backup to incremental or incremental cumulative, uncomment 
  122. # one of the following two lines. 
  123. # BACKUP_TYPE="INCREMENTAL LEVEL=1" 
  124. # BACKUP_TYPE="INCREMENTAL LEVEL=1 CUMULATIVE" 
  125. #  
  126. # Note that we use incremental level 0 to specify full backups. 
  127. # That is because, although they are identical in content, only 
  128. # the incremental level 0 backup can have incremental backups of 
  129. level > 0 applied to it. 
  130. --------------------------------------------------------------------------- 
  131. INCR_DATA=`date '+%A'
  132.  
  133. if [ "$INCR_DATA" = "Sunday" ] 
  134. then 
  135.         echo "Full backup requested" >> $RMAN_LOG_FILE 
  136.         BACKUP_TYPE="INCREMENTAL LEVEL=0" 
  137.   
  138. else  
  139.         echo "Differential incremental backup requested" >> $RMAN_LOG_FILE 
  140.         BACKUP_TYPE="INCREMENTAL LEVEL=1" 
  141.   
  142. #elif [ "$NB_ORA_CINC" = "1" ] 
  143. #then 
  144. #        echo "Cumulative incremental backup requested" >> $RMAN_LOG_FILE 
  145. #        BACKUP_TYPE="INCREMENTAL LEVEL=1 CUMULATIVE" 
  146. #  
  147. #elif [ "$BACKUP_TYPE" = "" ] 
  148. #then 
  149. #        echo "Default - Full backup requested" >> $RMAN_LOG_FILE 
  150. #        BACKUP_TYPE="INCREMENTAL LEVEL=0" 
  151. fi 
  152.  
  153. echo "BACKUP_TYPE">> $RMAN_LOG_FILE 
  154. echo ${BACKUP_TYPE} >> $RMAN_LOG_FILE 
  155. --------------------------------------------------------------------------- 
  156. # Call Recovery Manager to initiate the backup. This example does not use a 
  157. # Recovery Catalog. If you choose to use one, replace the option 'nocatalog' 
  158. from the rman command line below with the  
  159. 'catalog <userid>/<passwd>@<net service name>' statement. 
  160. # Note: Any environment variables needed at run time by RMAN  
  161. #       must be set and exported within the switch user (su) command. 
  162. --------------------------------------------------------------------------- 
  163. #  Backs up the whole database.  This backup is part of the incremental 
  164. #  strategy (this means it can have incremental backups of levels > 0 
  165. #  applied to it). 
  166. #  We do not need to explicitly request the control file to be included 
  167. #  in this backup, as it is automatically included each time file 1 of 
  168. #  the system tablespace is backed up (the inference: as it is a whole 
  169. #  database backup, file 1 of the system tablespace will be backed up, 
  170. #  hence the controlfile will also be included automatically). 
  171. #  Typically, a level 0 backup would be done at least once a week. 
  172. #  The scenario assumes: 
  173. #     o you are backing your database up to two tape drives 
  174. #     o you want each backup set to include a maximum of 5 files 
  175. #     o you wish to include offline datafiles, and read-only tablespaces, 
  176. #       in the backup 
  177. #     o you want the backup to continue if any files are inaccessible. 
  178. #     o you are not using a Recovery Catalog 
  179. #     o you are explicitly backing up the control file.  Since you are 
  180. #       specifying nocatalog, the controlfile backup that occurs 
  181. #       automatically as the result of backing up the system file is 
  182. #       not sufficient; it will not contain records for the backup that 
  183. #       is currently in progress. 
  184. #     o you want to archive the current log, back up all the 
  185. #       archive logs using two channels, putting a maximum of 20 logs 
  186. #       in a backup setand deleting them once the backup is complete. 
  187. #  Note that the format string is constructed to guarantee uniqueness and 
  188. #  to enhance NetBackup for Oracle backup and restore performance. 
  189. #  NOTE WHEN USING NET SERVICE NAMEWhen connecting to a database 
  190. #  using a net service name, you must use a send command or a parms operand to  
  191. #  specify environment variables.  In other words, when accessing a database 
  192. #  through a listener, the environment variables set at the system level are not  
  193. #  visible when RMAN is running.  For more information on the environment 
  194. #  variables, please refer to the NetBackup for Oracle Admin. Guide. 
  195. --------------------------------------------------------------------------- 
  196.  
  197. ORACLE_HOME=$ORACLE_HOME 
  198. export ORACLE_HOME 
  199. ORACLE_SID=$ORACLE_SID 
  200. export ORACLE_SID 
  201. $RMAN target $TARGET_CONNECT_STR nocatalog msglog $RMAN_LOG_FILE append << EOF 
  202. RUN { 
  203. ALLOCATE CHANNEL ch00 TYPE DISK; 
  204. ALLOCATE CHANNEL ch01 TYPE DISK; 
  205. # crosscheck archivelog  
  206. CROSSCHECK ARCHIVELOG ALL
  207. # crosscheck backup image 
  208. CROSSCHECK BACKUP; 
  209. #DELETE OBSOLETE BACKUP IMAGE 
  210. DELETE NOPROMPT OBSOLETE; 
  211. #DELETE EXPIRED BACKUP IMAGE 
  212. DELETE NOPROMPT EXPIRED BACKUP;  
  213. BACKUP 
  214.     $BACKUP_TYPE 
  215.     SKIP INACCESSIBLE 
  216.     TAG hot_db_bk_level0 
  217.     FILESPERSET 2 
  218.     # recommended format 
  219.     FORMAT '${RMAN_IMAGE_DIR}/bk_%s_%p_%t' 
  220.     DATABASE
  221.     sql 'alter system archive log current'
  222. RELEASE CHANNEL ch00; 
  223. RELEASE CHANNEL ch01; 
  224. # backup all archive logs 
  225. ALLOCATE CHANNEL ch00 TYPE DISK; 
  226. ALLOCATE CHANNEL ch01 TYPE DISK; 
  227. BACKUP 
  228.    filesperset 20 
  229.    FORMAT '${RMAN_IMAGE_DIR}/al_%s_%p_%t' 
  230.    ARCHIVELOG ALL DELETE INPUT; 
  231. RELEASE CHANNEL ch00; 
  232. RELEASE CHANNEL ch01; 
  233. # Note: During the process of backing up the database, RMAN also backs up the 
  234. # control file.  This version of the control file does not contain the 
  235. # information about the current backup because "nocatalog" has been specified. 
  236. To include the information about the current backup, the control file should 
  237. # be backed up as the last step of the RMAN section.  This step would not be 
  238. # necessary if we were using a recovery catalog or auto control file backups. 
  239. ALLOCATE CHANNEL ch00 TYPE DISK; 
  240. BACKUP 
  241.     # recommended format 
  242.     FORMAT '${RMAN_IMAGE_DIR}/cntrl_%s_%p_%t' 
  243.     CURRENT CONTROLFILE; 
  244. RELEASE CHANNEL ch00; 
  245. exit 
  246. EOF 
  247.  
  248. --------------------------------------------------------------------------- 
  249. # Print date to logfile 
  250. --------------------------------------------------------------------------- 
  251.  
  252. echo Script $0 >> $RMAN_LOG_FILE 
  253. echo ==== end on `date` ==== >> $RMAN_LOG_FILE 
  254. echo >> $RMAN_LOG_FILE 

把脚本增加到crontab中

[oracle@11rac1 ~]$ crontab -l
1 3 * * * ksh /home/oracle/hot_rman_backup.sh >>/home/oracle/log/hot_rman_backup_crontab_`date "+\%Y\%m\%d\%H\%M"`.log 2>&1

每天早上的3点进行备份,另外我们还需要的就是在RMAN中配置备份的保留时间与controlfile与spfile的自己备份。

 



     本文转自7343696 51CTO博客,原文链接:http://blog.51cto.com/luoping/1009594,如需转载请自行联系原作者




相关文章
|
12天前
|
SQL Oracle 关系型数据库
【Oracle】玩转Oracle数据库(一):装上去,飞起来!
【Oracle】玩转Oracle数据库(一):装上去,飞起来!
52 7
|
29天前
|
Oracle 关系型数据库 数据库
Oracle数据库基本概念理解(3)
Oracle数据库基本概念理解(3)
18 2
|
12天前
|
SQL Oracle 关系型数据库
【Oracle】玩转Oracle数据库(七):RMAN恢复管理器
【Oracle】玩转Oracle数据库(七):RMAN恢复管理器
40 5
|
29天前
|
Oracle 关系型数据库 数据库
Oracle数据库基本概念理解(2)
Oracle数据库基本概念理解(2)
13 1
|
4天前
|
存储 Oracle 关系型数据库
Oracle的模式与模式对象:数据库的“城市规划师”
【4月更文挑战第19天】在Oracle数据库中,模式是用户对象的集合,相当于数据库的城市规划,包含表、视图、索引等模式对象。模式对象是数据存储结构,如表用于存储数据,视图提供不同查看角度,索引加速数据定位。良好的模式与模式对象设计关乎数据效率、安全和稳定性。规划时需考虑业务需求、性能、安全和可扩展性,以构建高效数据库环境,支持企业业务发展。
|
12天前
|
存储 SQL Oracle
【Oracle】玩转Oracle数据库(二):体系结构、存储结构与各类参数
【Oracle】玩转Oracle数据库(二):体系结构、存储结构与各类参数
35 7
|
4天前
|
关系型数据库 MySQL 分布式数据库
《MySQL 简易速速上手小册》第6章:MySQL 复制和分布式数据库(2024 最新版)
《MySQL 简易速速上手小册》第6章:MySQL 复制和分布式数据库(2024 最新版)
31 2
|
20天前
|
SQL 数据可视化 关系型数据库
轻松入门MySQL:深入探究MySQL的ER模型,数据库设计的利器与挑战(22)
轻松入门MySQL:深入探究MySQL的ER模型,数据库设计的利器与挑战(22)
104 0
|
20天前
|
存储 关系型数据库 MySQL
轻松入门MySQL:数据库设计之范式规范,优化企业管理系统效率(21)
轻松入门MySQL:数据库设计之范式规范,优化企业管理系统效率(21)
|
20天前
|
关系型数据库 MySQL 数据库
轻松入门MySQL:精准查询,巧用WHERE与HAVING,数据库查询如虎添翼(7)
轻松入门MySQL:精准查询,巧用WHERE与HAVING,数据库查询如虎添翼(7)

推荐镜像

更多