手机版
你好,游客 登录 注册
背景:
阅读新闻

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

[日期:2012-10-02] 来源:Linux社区  作者:luoping [字体:大 中 小]

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

linux
相关资讯       RMAN 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊���网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款