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. For user initiated backups, BACKUP_TYPE defaults to incremental 
  119. level 0 (full).  To change the default for a user initiated 
  120. # backup to incremental or incremental cumulative, uncomment 
  121. # one of the following two lines. 
  122. # BACKUP_TYPE="INCREMENTAL LEVEL=1" 
  123. # BACKUP_TYPE="INCREMENTAL LEVEL=1 CUMULATIVE" 
  124. # Note that we use incremental level 0 to specify full backups. 
  125. # That is because, although they are identical in content, only 
  126. # the incremental level 0 backup can have incremental backups of 
  127. level > 0 applied to it. 
  128. --------------------------------------------------------------------------- 
  129. INCR_DATA=`date '+%A'
  130.  
  131. if [ "$INCR_DATA" = "Sunday" ] 
  132. then 
  133.         echo "Full backup requested" >> $RMAN_LOG_FILE 
  134.         BACKUP_TYPE="INCREMENTAL LEVEL=0" 
  135.  
  136. else 
  137.         echo "Differential incremental backup requested" >> $RMAN_LOG_FILE 
  138.         BACKUP_TYPE="INCREMENTAL LEVEL=1" 
  139.  
  140. #elif [ "$NB_ORA_CINC" = "1" ] 
  141. #then 
  142. #        echo "Cumulative incremental backup requested" >> $RMAN_LOG_FILE 
  143. #        BACKUP_TYPE="INCREMENTAL LEVEL=1 CUMULATIVE" 
  144. #elif [ "$BACKUP_TYPE" = "" ] 
  145. #then 
  146. #        echo "Default - Full backup requested" >> $RMAN_LOG_FILE 
  147. #        BACKUP_TYPE="INCREMENTAL LEVEL=0" 
  148. fi 
  149.  
  150. echo "BACKUP_TYPE">> $RMAN_LOG_FILE 
  151. echo ${BACKUP_TYPE} >> $RMAN_LOG_FILE 
  152. --------------------------------------------------------------------------- 
  153. # Call Recovery Manager to initiate the backup. This example does not use a 
  154. # Recovery Catalog. If you choose to use one, replace the option 'nocatalog' 
  155. from the rman command line below with the 
  156. 'catalog <userid>/<passwd>@<net service name>' statement. 
  157. # Note: Any environment variables needed at run time by RMAN 
  158. #       must be set and exported within the switch user (su) command. 
  159. --------------------------------------------------------------------------- 
  160. #  Backs up the whole database.  This backup is part of the incremental 
  161. #  strategy (this means it can have incremental backups of levels > 0 
  162. #  applied to it). 
  163. #  We do not need to explicitly request the control file to be included 
  164. in this backup, as it is automatically included each time file 1 of 
  165. #  the system tablespace is backed up (the inference: as it is a whole 
  166. database backup, file 1 of the system tablespace will be backed up, 
  167. #  hence the controlfile will also be included automatically). 
  168. #  Typically, a level 0 backup would be done at least once a week. 
  169. #  The scenario assumes: 
  170. #     o you are backing your database up to two tape drives 
  171. #     o you want each backup set to include a maximum of 5 files 
  172. #     o you wish to include offline datafiles, and read-only tablespaces, 
  173. #       in the backup 
  174. #     o you want the backup to continue if any files are inaccessible. 
  175. #     o you are not using a Recovery Catalog 
  176. #     o you are explicitly backing up the control file.  Since you are 
  177. #       specifying nocatalog, the controlfile backup that occurs 
  178. #       automatically as the result of backing up the system file is 
  179. #       not sufficient; it will not contain records for the backup that 
  180. #       is currently in progress. 
  181. #     o you want to archive the current log, back up all the 
  182. #       archive logs using two channels, putting a maximum of 20 logs 
  183. #       in a backup setand deleting them once the backup is complete. 
  184. #  Note that the format string is constructed to guarantee uniqueness and 
  185. to enhance NetBackup for Oracle backup and restore performance. 
  186. #  NOTE WHEN USING NET SERVICE NAMEWhen connecting to a database 
  187. #  using a net service name, you must use a send command or a parms operand to 
  188. #  specify environment variables.  In other words, when accessing a database 
  189. #  through a listener, the environment variables set at the system level are not 
  190. #  visible when RMAN is running.  For more information on the environment 
  191. #  variables, please refer to the NetBackup for Oracle Admin. Guide. 
  192. --------------------------------------------------------------------------- 
  193.  
  194. ORACLE_HOME=$ORACLE_HOME 
  195. export ORACLE_HOME 
  196. ORACLE_SID=$ORACLE_SID 
  197. export ORACLE_SID 
  198. $RMAN target $TARGET_CONNECT_STR nocatalog msglog $RMAN_LOG_FILE append << EOF 
  199. RUN { 
  200. ALLOCATE CHANNEL ch00 TYPE DISK; 
  201. ALLOCATE CHANNEL ch01 TYPE DISK; 
  202. # crosscheck archivelog 
  203. CROSSCHECK ARCHIVELOG ALL
  204. # crosscheck backup image 
  205. CROSSCHECK BACKUP; 
  206. #DELETE OBSOLETE BACKUP IMAGE 
  207. DELETE NOPROMPT OBSOLETE; 
  208. #DELETE EXPIRED BACKUP IMAGE 
  209. DELETE NOPROMPT EXPIRED BACKUP; 
  210. BACKUP 
  211.     $BACKUP_TYPE 
  212.     SKIP INACCESSIBLE 
  213.     TAG hot_db_bk_level0 
  214.     FILESPERSET 2 
  215.     # recommended format 
  216.     FORMAT '${RMAN_IMAGE_DIR}/bk_%s_%p_%t' 
  217.     DATABASE
  218.     sql 'alter system archive log current'
  219. RELEASE CHANNEL ch00; 
  220. RELEASE CHANNEL ch01; 
  221. # backup all archive logs 
  222. ALLOCATE CHANNEL ch00 TYPE DISK; 
  223. ALLOCATE CHANNEL ch01 TYPE DISK; 
  224. BACKUP 
  225.    filesperset 20 
  226.    FORMAT '${RMAN_IMAGE_DIR}/al_%s_%p_%t' 
  227.    ARCHIVELOG ALL DELETE INPUT; 
  228. RELEASE CHANNEL ch00; 
  229. RELEASE CHANNEL ch01; 
  230. # Note: During the process of backing up the database, RMAN also backs up the 
  231. # control file.  This version of the control file does not contain the 
  232. # information about the current backup because "nocatalog" has been specified. 
  233. To include the information about the current backup, the control file should 
  234. # be backed up as the last step of the RMAN section.  This step would not be 
  235. # necessary if we were using a recovery catalog or auto control file backups. 
  236. ALLOCATE CHANNEL ch00 TYPE DISK; 
  237. BACKUP 
  238.     # recommended format 
  239.     FORMAT '${RMAN_IMAGE_DIR}/cntrl_%s_%p_%t' 
  240.     CURRENT CONTROLFILE; 
  241. RELEASE CHANNEL ch00; 
  242. exit 
  243. EOF 
  244.  
  245. --------------------------------------------------------------------------- 
  246. # Print date to logfile 
  247. --------------------------------------------------------------------------- 
  248.  
  249. echo Script $0 >> $RMAN_LOG_FILE 
  250. echo ==== end on `date` ==== >> $RMAN_LOG_FILE 
  251. 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的自己备份。

相关内容