Oracle数据库数据恢复、性能优化

找回密码
注册
搜索
热搜: 活动 交友 discuz
发新帖

6

积分

0

好友

0

主题
1#
发表于 2012-4-27 09:48:31 | 查看: 5020| 回复: 1
oracle11g r1 如何实现DG SWITCHOVER自动切换                                                                                                                                                                                                                                                                                                                        数据库为单实例;数据库版本:oracle 11g r1,
需要写一个shell脚本;实现primarydb to standbydb switchover 自动切换!
哪位仁兄有好的建议或者有类似的shell脚本,请添加我的个人QQ(251411209)!
非常感谢!
2#
发表于 2012-4-27 23:14:01
参考 Data Guard Switchover Unix shell script  http://gavinsoorma.com/2009/07/870/


On the machine where the Primary Database is running we need to run the following scripts
  1. $ cd /var/opt/oracle/dataguard
  2. $ ./pre_switchover_check.sh
  3. $ ./make_me_standby.sh

  4. On the machine where the Standby Database is running we need to run the following script:

  5. $ cd /var/opt/oracle/dataguard
  6. $ ./make_me_primary.sh

  7. After the switchover is completed, we need to run the following script on the machine where the former Primary (now new Standby) database is running:

  8. $ cd /var/opt/oracle/dataguard
  9. $ ./start_recovery.sh

  10. pre_switchover_check.sh

  11. !/bin/ksh

  12. if [ "$1" = "" ]
  13. then
  14. echo ""
  15. echo "##################################################################"
  16. echo "# PLEASE ENTER ORACLE_SID AS THE FIRST COMMAND LINE ARGUMENT     #"
  17. echo "#      SCRIPT USAGE : pre_switchover_check.sh        #"
  18. echo "##################################################################"
  19. echo ""
  20. exit
  21. fi

  22. ORACLE_SID=`echo $1 |tr '[A-Z]' '[a-z]'`; export ORACLE_SID;

  23. ls -l /var/opt/oracle/cronjobs/set$ORACLE_SID > /dev/null
  24. if [ $? != 0 ]
  25. then
  26. echo ""
  27. echo "##################################################################"
  28. echo "#           ENVIRONMENT SETTING SET FILE NOT FOUND...!!!         #"
  29. echo "#           /var/opt/oracle/cronjobs/set$ORACLE_SID              #"
  30. echo "##################################################################"
  31. echo ""
  32. exit
  33. fi

  34. . /var/opt/oracle/cronjobs/set$ORACLE_SID

  35. DB=`echo $1 |tr '[a-z]' '[A-Z]'`; export DB;

  36. echo ""
  37. echo "##################################################################"
  38. echo "#            PERFORMING PRE-SWITCHOVER CHECKS FOR $DB            #"
  39. echo "##################################################################"
  40. echo ""

  41. sqlplus  -s /nolog |&
  42. print -p  "connect / as sysdba"
  43. print -p  "set feedback off pause off pagesize 0 heading off verify off linesize 500 term off "
  44. print -p  "set sqlprompt ''"
  45. read  -p   JUNK
  46. print -p  "SELECT DATABASE_ROLE DBROLE FROM V\$DATABASE;"
  47. read  -p   DBROLE
  48. print -p  "SELECT COUNT(FILE#) FILESTAT FROM V\$DATAFILE WHERE STATUS IN ('RECOVER', 'OFFLINE');"
  49. read  -p   FILESTAT
  50. print -p  "SELECT COUNT(FILE#) BKPSTAT FROM V\$BACKUP WHERE STATUS = 'ACTIVE';"
  51. read  -p   BKPSTAT
  52. print -p  "SELECT LOG_ARCHIVED-LOG_APPLIED LOGGAP
  53. FROM (SELECT MAX(SEQUENCE#) LOG_ARCHIVED
  54. FROM V\$ARCHIVED_LOG WHERE DEST_ID=1 AND ARCHIVED='YES'
  55. AND RESETLOGS_CHANGE#=(SELECT MAX(RESETLOGS_CHANGE#)
  56. FROM V\$ARCHIVED_LOG)),
  57. (SELECT MAX(SEQUENCE#) LOG_APPLIED
  58. FROM V\$ARCHIVED_LOG WHERE DEST_ID=2
  59. AND APPLIED='YES'
  60. AND RESETLOGS_CHANGE#=(SELECT MAX(RESETLOGS_CHANGE#) FROM V\$ARCHIVED_LOG)) ;"
  61. read  -p   LOGGAP
  62. print -p  "EXIT"

  63. echo "CHECKING CURRENT DATABASE ROLE..."
  64. if [ "$DBROLE" = "PHYSICAL STANDBY"  ]
  65. then
  66. echo ""
  67. echo "##################################################################"
  68. echo "# ERROR ! ! ! NOT  A PRIMARY DATABASE, PLEASE RUN IT ON PRIMARY  #"
  69. echo "##################################################################"
  70. echo ""
  71. exit
  72. else
  73. echo ""
  74. echo "   OK   "
  75. echo ""
  76. fi

  77. echo "CHECKING LOG GAP BETWEEN PRIMARY & STANDBY..."
  78. if [ $LOGGAP -ne 0 ]
  79. then
  80. echo ""
  81. echo "######################################################################"
  82. echo "# ERROR ! ! !  PRIMARY & STANDBY ARE NOT IN SYNC, LOG GAP = $LOGGAP  #"
  83. echo "######################################################################"
  84. echo ""
  85. exit
  86. else
  87. echo ""
  88. echo "   OK   "
  89. echo ""
  90. fi

  91. echo "CHECKING FILES OFFLINE OR RECOVER STATUS..."
  92. if [ $FILESTAT -ne 0 ]
  93. then
  94. echo ""
  95. echo "##################################################################"
  96. echo "#  ERROR ! ! !  ONE OR MORE FILE/S OFFLINE OR IN RECOVER STATUS  #"
  97. echo "##################################################################"
  98. echo ""
  99. exit
  100. else
  101. echo ""
  102. echo "   OK   "
  103. echo ""
  104. fi

  105. echo "CHECKING FILES IN BACKUP MODE..."
  106. if [ $BKPSTAT -ne 0 ]
  107. then
  108. echo ""
  109. echo "##################################################################"
  110. echo "#      ERROR ! ! !  ONE OR MORE FILE/S ARE IN BACKUP MODE        #"
  111. echo "##################################################################"
  112. echo ""
  113. exit
  114. else
  115. echo ""
  116. echo "   OK   "
  117. echo ""
  118. fi

  119. echo ""
  120. echo "##################################################################"
  121. echo "# ALL PRE-SWITCHOVER CHECKS SUCCEEDED... PROCEED WITH SWITCHOVER #"
  122. echo "##################################################################"
  123. echo ""

  124. make_me_standby.sh

  125. #!/bin/ksh

  126. if [ "$1" = "" ]
  127. then
  128. echo ""
  129. echo "##################################################################"
  130. echo "# PLEASE ENTER ORACLE_SID AS THE FIRST COMMAND LINE ARGUMENT     #"
  131. echo "#        SCRIPT USAGE : make_me_standby.sh           #"
  132. echo "##################################################################"
  133. echo ""
  134. exit
  135. fi

  136. ORACLE_SID=`echo $1 |tr '[A-Z]' '[a-z]'`; export ORACLE_SID;

  137. ls -l /var/opt/oracle/cronjobs/set$ORACLE_SID > /dev/null
  138. if [ $? != 0 ]
  139. then
  140. echo ""
  141. echo "##################################################################"
  142. echo "#           ENVIRONMENT SETTING SET FILE NOT FOUND...!!!         #"
  143. echo "#           /var/opt/oracle/cronjobs/set$ORACLE_SID              #"
  144. echo "##################################################################"
  145. echo ""
  146. exit
  147. fi

  148. . /var/opt/oracle/cronjobs/set$ORACLE_SID

  149. DB=`echo $1 |tr '[a-z]' '[A-Z]'`; export DB;

  150. sqlplus  -s /nolog |&
  151. print -p  "connect / as sysdba"
  152. print -p  "set feedback off pause off pagesize 0 heading off verify off linesize 500 term off"
  153. print -p  "set sqlprompt ''"
  154. read  -p   JUNK
  155. print -p  "SELECT DATABASE_ROLE DBROLE FROM V\$DATABASE;"
  156. read  -p   DBROLE
  157. print -p  "SELECT COUNT(FILE#) FILESTAT FROM V\$DATAFILE WHERE STATUS IN ('RECOVER', 'OFFLINE');"
  158. read  -p   FILESTAT
  159. print -p  "SELECT COUNT(FILE#) BKPSTAT FROM V\$BACKUP WHERE STATUS = 'ACTIVE';"
  160. read  -p   BKPSTAT
  161. print -p  "SELECT LOG_ARCHIVED-LOG_APPLIED LOGGAP
  162. FROM (SELECT MAX(SEQUENCE#) LOG_ARCHIVED
  163. FROM V\$ARCHIVED_LOG WHERE DEST_ID=1 AND ARCHIVED='YES'
  164. AND RESETLOGS_CHANGE#=(SELECT MAX(RESETLOGS_CHANGE#)
  165. FROM V\$ARCHIVED_LOG)),
  166. (SELECT MAX(SEQUENCE#) LOG_APPLIED
  167. FROM V\$ARCHIVED_LOG WHERE DEST_ID=2
  168. AND APPLIED='YES'
  169. AND RESETLOGS_CHANGE#=(SELECT MAX(RESETLOGS_CHANGE#) FROM V\$ARCHIVED_LOG)) ;"
  170. read  -p   LOGGAP
  171. print -p  "EXIT"

  172. if [ "$DBROLE" = "PHYSICAL STANDBY"  ]
  173. then
  174. echo ""
  175. echo "##################################################################"
  176. echo "# ERROR ! ! ! NOT  A PRIMARY DATABASE, PLEASE RUN IT ON PRIMARY  #"
  177. echo "##################################################################"
  178. echo ""
  179. exit
  180. fi

  181. if [ $LOGGAP -ne 0 ]
  182. then
  183. echo ""
  184. echo "######################################################################"
  185. echo "# ERROR ! ! !  PRIMARY & STANDBY ARE NOT IN SYNC, LOG GAP = $LOGGAP  #"
  186. echo "######################################################################"
  187. echo ""
  188. exit
  189. fi

  190. echo "CHECKING FILES OFFLINE OR RECOVER STATUS..."
  191. if [ $FILESTAT -ne 0 ]
  192. then
  193. echo ""
  194. echo "##################################################################"
  195. echo "#  ERROR ! ! !  ONE OR MORE FILE/S OFFLINE OR IN RECOVER STATUS  #"
  196. echo "##################################################################"
  197. echo ""
  198. exit
  199. fi

  200. if [ $BKPSTAT -ne 0 ]
  201. then
  202. echo ""
  203. echo "##################################################################"
  204. echo "#      ERROR ! ! !  ONE OR MORE FILE/S ARE IN BACKUP MODE        #"
  205. echo "##################################################################"
  206. echo ""
  207. exit
  208. fi

  209. echo ""
  210. echo "##################################################################"
  211. echo "#              ALL PRE-SWITCHOVER CHECKS SUCCEEDED...            #"
  212. echo "       SWITCHING $DB TO STANDBY ROLE, PLEASE CONFIRM ...          "
  213. echo "##################################################################"
  214. echo ""
  215. echo ""
  216. echo "Continue? - Please enter 'Y' to proceed & 'N' to exit"
  217. read ans
  218. if [ "$ans" = 'Y' -o "$ans" = 'y' ]
  219. then
  220. sqlplus -s  /nolog < /tmp/make_me_standby.log
  221. connect / as sysdba;
  222. startup force;
  223. alter database commit to switchover to standby with session shutdown;
  224. shutdown immediate;
  225. startup nomount;
  226. alter database mount standby database;
  227. select database_role from v\$database;
  228. EOF
  229. cat /tmp/make_me_standby.log
  230. $ORACLE_HOME/bin/lsnrctl stop PRIMARY_$DB
  231. else
  232. echo "Quitting ....."
  233. exit
  234. fi

  235. echo ""
  236. echo "####################################################################"
  237. echo "# PLEASE CONFIRM THAT THE STATUS OF DATABASE IS NOW "STANDBY"      #"
  238. echo "#                                                                  #"
  239. echo "# On OLD STANDBY Host please run the following script:         #"
  240. echo "# /var/opt/oracle/dataguard/make_me_primary.sh                #"
  241. echo "####################################################################"
  242. echo ""

  243. make_me_primary.sh

  244. #!/bin/ksh

  245. if [ "$1" = "" ]
  246. then
  247. echo ""
  248. echo "##################################################################"
  249. echo "#  PLEASE ENTER ORACLE_SID AS THE FIRST COMMAND LINE ARGUMENT    #"
  250. echo "#         SCRIPT USAGE : make_me_primary.sh          #"
  251. echo "##################################################################"
  252. echo ""
  253. exit
  254. fi

  255. ORACLE_SID=`echo $1 |tr '[A-Z]' '[a-z]'`; export ORACLE_SID;

  256. ls -l /var/opt/oracle/cronjobs/set$ORACLE_SID > /dev/null
  257. if [ $? != 0 ]
  258. then
  259. echo ""
  260. echo "##################################################################"
  261. echo "#           ENVIRONMENT SETTING SET FILE NOT FOUND...!!!         #"
  262. echo "#           /var/opt/oracle/cronjobs/set$ORACLE_SID              #"
  263. echo "##################################################################"
  264. echo ""
  265. exit
  266. fi

  267. . /var/opt/oracle/cronjobs/set$ORACLE_SID

  268. DB=`echo $1 |tr '[a-z]' '[A-Z]'`; export DB;

  269. sqlplus  -s /nolog |&
  270. print -p  "connect / as sysdba"
  271. print -p  "set feedback off pause off pagesize 0 heading off verify off linesize 500 term off"
  272. print -p  "set sqlprompt ''"
  273. read  -p   JUNK
  274. print -p  "SELECT DATABASE_ROLE DBROLE FROM V\$DATABASE;"
  275. read  -p   DBROLE
  276. print -p  "exit"

  277. if [ "$DBROLE" = "PRIMARY"  ]
  278. then
  279. echo ""
  280. echo "##################################################################"
  281. echo "#   ERROR ! ! ! NOT STANDBY DATABASE, PLEASE RUN IT ON STANDBY   #"
  282. echo "##################################################################"
  283. echo ""
  284. exit
  285. fi

  286. echo ""
  287. echo "##################################################################"
  288. echo "#         SWITCHING $DB TO PRIMARY ROLE, PLEASE CONFIRM ...      #"
  289. echo "##################################################################"
  290. echo ""
  291. echo "Continue? - Please enter 'Y' to proceed & 'N' to exit"
  292. read ans
  293. if [ "$ans" = 'Y' -o "$ans" = 'y' ]
  294. then
  295. sqlplus -s /nolog < /tmp/make_me_primary.log
  296. connect / as sysdba;
  297. alter database commit to switchover to primary with session shutdown;
  298. shutdown immediate;
  299. startup mount;
  300. alter system set log_archive_dest_state_2=enable scope=both;
  301. alter database set standby database to maximize performance;
  302. alter database open;
  303. select database_role from v\$database;
  304. EOF
  305. cat /tmp/make_me_primary.log
  306. $ORACLE_HOME/bin/lsnrctl start PRIMARY_$DB
  307. else
  308. echo "Quitting ....."
  309. exit
  310. fi

  311. echo "####################################################################"
  312. echo "# PLEASE CONFIRM THAT THE STATUS OF DATABASE IS NOW "PRIMARY"      #"
  313. echo "#                                                                  #"
  314. echo "# On NEW STANDBY Host please run the following script:             #"
  315. echo "# /var/opt/oracle/dataguard/start_recovery.sh                 #"
  316. echo "####################################################################"

  317. start_recovery.sh

  318. #!/bin/ksh

  319. if [ "$1" = "" ]
  320. then
  321. echo ""
  322. echo "##################################################################"
  323. echo "#   PLEASE ENTER ORACLE_SID AS THE FIRST COMMAND LINE ARGUMENT   #"
  324. echo "#      SCRIPT USAGE : start_recovery.sh              #"
  325. echo "##################################################################"
  326. echo ""
  327. exit
  328. fi

  329. ORACLE_SID=`echo $1 |tr '[A-Z]' '[a-z]'`; export ORACLE_SID;

  330. ls -l /var/opt/oracle/cronjobs/set$ORACLE_SID > /dev/null
  331. if [ $? != 0 ]
  332. then
  333. echo ""
  334. echo "##################################################################"
  335. echo "#           ENVIRONMENT SETTING SET FILE NOT FOUND...!!!         #"
  336. echo "#           /var/opt/oracle/cronjobs/set$ORACLE_SID              #"
  337. echo "##################################################################"
  338. echo ""
  339. exit
  340. fi

  341. . /var/opt/oracle/cronjobs/set$ORACLE_SID

  342. DB=`echo $1 |tr '[a-z]' '[A-Z]'`; export DB;

  343. sqlplus  -s /nolog |&
  344. print -p  "connect / as sysdba"
  345. print -p  "set feedback off pause off pagesize 0 heading off verify off linesize 500 term off"
  346. print -p  "set sqlprompt ''"
  347. read  -p   JUNK
  348. print -p  "SELECT DATABASE_ROLE DBROLE FROM V\$DATABASE;"
  349. read  -p   DBROLE
  350. print -p  "exit"

  351. if [ "$DBROLE" = "PRIMARY"  ]
  352. then
  353. echo ""
  354. echo "##################################################################"
  355. echo "#   ERROR ! ! ! NOT STANDBY DATABASE, PLEASE RUN IT ON STANDBY   #"
  356. echo "##################################################################"
  357. echo ""
  358. exit
  359. fi

  360. echo ""
  361. echo "##################################################################"
  362. echo "#     STARTING RECOVERY FOR $DB  STANDBY, PLEASE CONFIRM ...     #"
  363. echo "##################################################################"
  364. echo ""
  365. echo "Continue? - Please enter 'Y' to proceed & 'N' to exit"
  366. read ans
  367. if [ "$ans" = 'Y' -o "$ans" = 'y' ]
  368. then
  369. sqlplus -s /nolog < /tmp/start_recovery.log
  370. connect / as sysdba;
  371. recover managed standby database disconnect;
  372. alter system set log_archive_dest_state_2=defer scope=both;
  373. EOF
  374. cat /tmp/start_recovery.log
  375. ps -ef | grep ora_mrp0_$ORACLE_SID |grep -v grep > /dev/null
  376. if [ $? != 0 ]
  377. then
  378. echo ""
  379. echo "##################################################################"
  380. echo "#           RECOVERY PROCESS NOT RUNNING... PLEASE CHECK         #"
  381. echo "##################################################################"
  382. echo ""
  383. exit
  384. else
  385. echo "##################################################################"
  386. echo "#           MRP PROCESS SUCESSFULLY STARTED                      #"
  387. echo "##################################################################"
  388. echo ""
  389. echo "####################################################################"
  390. echo "#                 SWITCHOVER COMPLETE                              #"
  391. echo ""
  392. echo "* Perform Database Post-Switchover Checklist!                      "
  393. echo "####################################################################"
  394. fi
  395. else
  396. echo "Quitting ....."
  397. exit
  398. fi
复制代码

回复 只看该作者 道具 举报

您需要登录后才可以回帖 登录 | 注册

QQ|手机版|Archiver|Oracle数据库数据恢复、性能优化

GMT+8, 2024-12-24 10:14 , Processed in 0.049289 second(s), 21 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部
TEL/電話+86 13764045638
Email service@parnassusdata.com
QQ 47079569