你好,游客 登录
背景:
阅读新闻

大数据备份和恢复应用案例--通过分区表备份和恢复数据

[日期:2014-11-20] 来源:51CTO  作者: [字体:大 中 小]

海量数据备份和恢复方案

     对于OLAP的数据库的业务特点,是将批量的数据加载入库,然后对这些数据进行分析处理,比如报表或者数据挖掘,最后给业务提供一种决策支持;另外,这类数据库的数据实时性非常高,一旦这些数据处理完毕后,就很少再次使用(有时,也需要对这类数据进行查询)。

 对于OLAP数据库的备份和恢复可以考虑这样几种方案:

1、使用分布式数据库

    将数据分布到多个库里,当数据库恢复时,只需要恢复单个库的数据,大大节省恢复时间。

wKiom1Rr-0eyHB5SAAJmv2cd82o348.jpg


2、结合分区技术,以传输表空间方式进行备份和恢复

 

[sql] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. 1、建立分区表,将分区存储在不同的表空间 
  2. [oracle@RH6 ~]$sqlplus '/as sysdba' 
  3. SQL*Plus: Release 11.2.0.1.0 Production on Tue Nov 18 17:15:47 2014 
  4. Copyright (c) 1982, 2009, Oracle.  All rights reserved. 
  5. Connected to: 
  6. Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production 
  7. With the Partitioning, OLAP, Data Mining and Real Application Testing options 
  8. 17:15:47 SYS@ prod >create tablespace tbs1 
  9. 17:16:03   2  datafile '/dsk1/oradata/prod/tbs1.dbf' size 10m; 
  10. Tablespace created. 
  11.    
  12. 17:17:00 SYS@ prod >create tablespace tbs2 
  13. 17:17:11   2  datafile '/dsk2/oradata/prod/tbs2.dbf' size 10m; 
  14. Tablespace created. 
  15.    
  16. 17:17:49 SYS@ prod >create tablespace tbs3 
  17. 17:17:57   2  datafile '/dsk3/oradata/prod/tbs3.dbf' size 10m; 
  18. Tablespace created. 
  19.    
  20. 17:18:35 SYS@ prod >create tablespace tbs1_indx 
  21. 17:18:49   2  datafile '/dsk1/oradata/prod/tbs1_indx.dbf' size 10m; 
  22. Tablespace created. 
  23.    
  24. 17:19:43 SYS@ prod >create tablespace tbs2_indx 
  25. 17:19:54   2  datafile '/dsk2/oradata/prod/tbs2_indx.dbf' size 10m; 
  26. Tablespace created. 
  27.    
  28. 17:20:18 SYS@ prod >create tablespace tbs3_indx 
  29. 17:20:30   2  datafile '/dsk3/oradata/prod/tbs3_indx.dbf' size 10m; 
  30. Tablespace created. 
  31.    
  32. 17:22:12 SYS@ prod >select file_id,file_name,tablespace_name from dba_data_files 
  33.    FILE_ID FILE_NAME                                          TABLESPACE_NAME 
  34. ---------- -------------------------------------------------- ------------------------------ 
  35.         11 /dsk1/oradata/prod/tbs1.dbf                        TBS1 
  36.         12 /dsk2/oradata/prod/tbs2.dbf                        TBS2 
  37.         13 /dsk3/oradata/prod/tbs3.dbf                        TBS3 
  38.          4 /u01/app/oracle/oradata/prod/users01.dbf           USERS 
  39.          3 /u01/app/oracle/oradata/prod/undotbs01.dbf         UNDOTBS1 
  40.          2 /u01/app/oracle/oradata/prod/sysaux01.dbf          SYSAUX 
  41.          1 /u01/app/oracle/oradata/prod/system01.dbf          SYSTEM 
  42.          5 /u01/app/oracle/oradata/prod/example01.dbf         EXAMPLE 
  43.          6 /u01/app/oracle/oradata/prod/users02.dbf           USERS 
  44.          7 /u01/app/oracle/oradata/prod/catatbs1.dbf          CATATBS 
  45.          8 /u01/app/oracle/oradata/prod/perfertbs1.dbf        PERFERTBS 
  46.          9 /u01/app/oracle/oradata/prod/oggtbs1.dbf           OGG_TBS 
  47.         10 /u01/app/oracle/oradata/prod/test1.dbf             TEST1 
  48.         14 /dsk1/oradata/prod/tbs1_indx.dbf                   TBS1_INDX 
  49.         15 /dsk2/oradata/prod/tbs2_indx.dbf                   TBS2_INDX 
  50.         16 /dsk3/oradata/prod/tbs3_indx.dbf                   TBS3_INDX 
  51.    
  52. 建立分区表及索引: 
  53. 17:26:41 SCOTT@ prod >create table t1(id int,name varchar2(1000)) 
  54. 17:26:57   2   partition by range(id) 
  55. 17:27:01   3  (partition p1 values less than(1000) tablespace tbs1, 
  56. 17:27:13   4  partition p2 values less than(2000) tablespace tbs2, 
  57. 17:27:23   5  partition p3 values less than(maxvalue) tablespace tbs3); 
  58. Table created. 
  59.    
  60. 17:30:33 SCOTT@ prod >create index t1_indx on t1(id) local 
  61.   2  ( 
  62.   3  partition p1 tablespace tbs1_indx, 
  63.   4  partition p2 tablespace tbs2_indx, 
  64.   5* partition p3 tablespace tbs3_indx ) 
  65. / 
  66.    
  67. 17:30:37 SCOTT@ prod >select partition_name,tablespace_name from user_segments where segment_name='T1'; 
  68. PARTITION_NAME                 TABLESPACE_NAME 
  69. ------------------------------ ------------------------------ 
  70. P1                             TBS1 
  71. P2                             TBS2 
  72. P3                             TBS3 
  73.    
  74. 17:31:33 SCOTT@ prod >select partition_name,tablespace_name from user_segments where segment_name='T1_INDX'; 
  75. PARTITION_NAME                 TABLESPACE_NAME 
  76. ------------------------------ ------------------------------ 
  77. P1                             TBS1_INDX 
  78. P2                             TBS2_INDX 
  79. P3                             TBS3_INDX 
  80.    
  81. 插入数据: 
  82. 17:34:09 SYS@ prod >begin 
  83. 17:34:26   2  for i in 1..3 loop 
  84. 17:34:32   3  insert into scott.t1 select object_id*i,object_name from dba_objects where object_id <1000; 
  85. 17:34:43   4  end loop; 
  86. 17:34:51   5  commit; 
  87. 17:34:57   6  end; 
  88. 17:35:02   7  / 
  89. PL/SQL procedure successfully completed. 
  90.    
  91. 17:32:08 SCOTT@ prod >select count(*) from t1; 
  92.   COUNT(*) 
  93. ---------- 
  94.       2826 
  95.    
  96. 17:36:52 SCOTT@ prod >select 'p1',count(*) from t1 partition(p1) 
  97. 17:37:42   2  union 
  98. 17:37:47   3  select 'p2',count(*) from t1 partition(p2) 
  99. 17:38:11   4  union 
  100. 17:38:13   5  select 'p3',count(*) from t1 partition(p3); 
  101. 'P1'                               COUNT(*) 
  102. -------------------------------- ---------- 
  103. p1                                     1740 
  104. p2                                      774 
  105. p3                                      312 
  106.    
  107. 2、传输表空间 
  108. 17:35:04 SYS@ prod >alter tablespace tbs1 read only; 
  109. Tablespace altered. 
  110.    
  111. 17:41:02 SYS@ prod >alter tablespace tbs1_indx read only; 
  112. Tablespace altered. 
  113.    
  114. 17:39:14 SYS@ prod >create directory tbs_dir as '/home/oracle/data'; 
  115. Directory created. 
  116.    
  117. 17:40:30 SYS@ prod >grant read,write on directory tbs_dir to scott; 
  118. Grant succeeded. 
  119.    
  120. [oracle@RH6 data]$ expdp system/oracle directory=tbs_dir dumpfile=p1.dmp transport_tablespaces=tbs1,tbs1_indx logfile=p1.log 
  121. Export: Release 11.2.0.1.0 - Production on Tue Nov 18 17:44:25 2014 
  122. Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved. 
  123. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production 
  124. With the Partitioning, OLAP, Data Mining and Real Application Testing options 
  125. Starting "SYSTEM"."SYS_EXPORT_TRANSPORTABLE_01":  system/******** directory=tbs_dir dumpfile=p1.dmp transport_tablespaces=tbs1,tbs1_indx logfile=p1.log 
  126. ORA-39123: Data Pump transportable tablespace job aborted 
  127. ORA-39187: The transportable set is not self-contained, violation list is 
  128. ORA-39901: Partitioned table SCOTT.T1 is partially contained in the transportable set. 
  129. ORA-39921: Default Partition (Table) Tablespace USERS for T1 not contained in transportable set. 
  130. Job "SYSTEM"."SYS_EXPORT_TRANSPORTABLE_01" stopped due to fatal error at 17:44:49 
  131. 传输表空间出错,表空间处于非自包含模式: 
  132. 18:14:47 SYS@ prod >exec dbms_tts.transport_set_check('TBS1',true); 
  133. PL/SQL procedure successfully completed. 
  134.    
  135. 18:17:49 SYS@ prod >select * from transport_set_violations; 
  136. VIOLATIONS 
  137. ------------------------------------------------------------------------------------------------------------------------ 
  138. ORA-39921: Default Partition (Table) Tablespace USERS for T1 not contained in transportable set. 
  139. . 
  140. ORA-39901: Partitioned table SCOTT.T1 is partially contained in the transportable set. 
  141.    
  142. 解决方法,需要创建一个临时表和一个临时表索引,将分区和分区索引交换到临时表和临时表索引表空间上,然后到处临时表和临时表索引。由于临时表不是分区表,它们呢所在的表空间符合自包含条件。 
  143. 17:45:37 SCOTT@ prod >create table t1_tmp as select * from t1 where 1=3; 
  144. Table created. 
  145. Elapsed: 00:00:00.20 
  146. 17:45:58 SCOTT@ prod >create index t1_tmp_indx on t1_tmp(id); 
  147. Index created. 
  148. 17:46:33 SCOTT@ prod >select segment_name,tablespace_name from user_segments 
  149. 17:47:18   2   where segment_name in ('T1_TMP','T1_TMP_INDX'); 
  150. SEGMENT_NAME                                                                      TABLESPACE_NAME 
  151. --------------------------------------------------------------------------------- ------------------------------ 
  152. T1_TMP                                                                            USERS 
  153. T1_TMP_INDX                                                                       USERS 
  154.    
  155. 将分区表交换到临时表: 
  156. 17:48:32 SCOTT@ prod >alter table t1 exchange partition p1 with table t1_tmp including indexes; 
  157. Table altered. 
  158.    
  159. 17:49:02 SCOTT@ prod >select segment_name,tablespace_name from user_segments 
  160. 17:49:35   2   where segment_name in ('T1_TMP','T1_TMP_INDX'); 
  161. SEGMENT_NAME                                                                      TABLESPACE_NAME 
  162. --------------------------------------------------------------------------------- ------------------------------ 
  163. T1_TMP                                                                                 TBS1 
  164. T1_TMP_INDX                                                                       TBS1_INDX 
  165.    
  166. 17:50:44 SYS@ prod >exec dbms_tts.transport_set_check('TBS1',true); 
  167. PL/SQL procedure successfully completed. 
  168.    
  169. 17:51:59 SYS@ prod >select * from transport_set_violations; 
  170. no rows selected 
  171. 已经符合自包含条件 
  172.    
  173. [oracle@RH6 data]$ expdp system/oracle directory=tbs_dir dumpfile=p1.dmp transport_tablespaces=tbs1,tbs1_indx logfile=p1.log 
  174. Export: Release 11.2.0.1.0 - Production on Tue Nov 18 17:52:55 2014 
  175. Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved. 
  176. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production 
  177. With the Partitioning, OLAP, Data Mining and Real Application Testing options 
  178. Starting "SYSTEM"."SYS_EXPORT_TRANSPORTABLE_01":  system/******** directory=tbs_dir dumpfile=p1.dmp transport_tablespaces=tbs1,tbs1_indx logfile=p1.log 
  179. Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK 
  180. Processing object type TRANSPORTABLE_EXPORT/TABLE 
  181. Processing object type TRANSPORTABLE_EXPORT/INDEX 
  182. Processing object type TRANSPORTABLE_EXPORT/INDEX_STATISTICS 
  183. Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK 
  184. Master table "SYSTEM"."SYS_EXPORT_TRANSPORTABLE_01" successfully loaded/unloaded 
  185. ****************************************************************************** 
  186. Dump file set for SYSTEM.SYS_EXPORT_TRANSPORTABLE_01 is: 
  187.   /home/oracle/data/p1.dmp 
  188. ****************************************************************************** 
  189. Datafiles required for transportable tablespace TBS1: 
  190.   /dsk1/oradata/prod/tbs1.dbf 
  191. Datafiles required for transportable tablespace TBS1_INDX: 
  192.   /dsk1/oradata/prod/tbs1_indx.dbf 
  193. Job "SYSTEM"."SYS_EXPORT_TRANSPORTABLE_01" successfully completed at 17:54:17 
  194. 表空间导出成功! 
  195. 17:56:16 SYS@ prod >select file_name,tablespace_name from dba_data_files where tablespace_name in ('TBS1','TBS1_INDX'); 
  196. FILE_NAME                                          TABLESPACE_NAME 
  197. -------------------------------------------------- ------------------------------ 
  198. /dsk1/oradata/prod/tbs1.dbf                        TBS1 
  199. /dsk1/oradata/prod/tbs1_indx.dbf                   TBS1_INDX 
  200.    
  201. [oracle@RH6 ~]$ cp /dsk1/oradata/prod/tbs1* /home/oracle/data 
  202. [oracle@RH6 ~]$ ls -lh /home/oracle/data 
  203. total 21M 
  204. -rw-r----- 1 oracle oinstall  92K Nov 18 17:54 p1.dmp 
  205. -rw-r--r-- 1 oracle oinstall 1.4K Nov 18 17:54 p1.log 
  206. -rw-r----- 1 oracle oinstall  11M Nov 18 17:57 tbs1.dbf 
  207. -rw-r----- 1 oracle oinstall  11M Nov 18 17:57 tbs1_indx.dbf 
  208. 然后再将表空间的数据文件进行备份,由于表空间传输,只是导出了metadata,所以数据量非常小,速度非常快。 
  209.    
  210. 3、数据恢复 
  211. 17:58:29 SYS@ prod >drop tablespace tbs1 including contents and datafiles; 
  212. Tablespace dropped. 
  213.    
  214. 17:58:55 SYS@ prod >drop tablespace tbs1_indx  including contents and datafiles; 
  215. Tablespace dropped. 
  216.    
  217. 17:59:12 SYS@ prod >col segment_name for a20 
  218. 17:59:42 SYS@ prod >col partition_name for a10 
  219. 17:59:49 SYS@ prod >col tablespace_name for a10 
  220. 17:59:59 SYS@ prod >select segment_name,partition_name,tablespace_name from dba_segments 
  221. 18:00:32   2   where segment_name in ('T1','T1_INDX') order by 2; 
  222. SEGMENT_NAME         PARTITION_ TABLESPACE 
  223. -------------------- ---------- ---------- 
  224. T1                   P1         USERS 
  225. T1_INDX              P1         USERS 
  226. T1_INDX              P2         TBS2_INDX 
  227. T1                   P2         TBS2 
  228. T1_INDX              P3         TBS3_INDX 
  229. T1                   P3         TBS3 
  230. 6 rows selected. 
  231.    
  232. 拷贝备份数据文件到数据库下,进行数据导入 
  233. [oracle@RH6 oradata]$ cp /home/oracle/data/tbs1*.dbf /u01/app/oracle/oradata/prod/ 
  234.    
  235. [oracle@RH6 data]$ impdp system/oracle directory=tbs_dir dumpfile=p1.dmp transport_datafiles='/u01/app/oracle/oradata/prod/tbs1.dbf','/u01/app/oracle/oradata/prod/tbs1_indx.dbf' logfile=imp.log 
  236.    
  237. Import: Release 11.2.0.1.0 - Production on Tue Nov 18 18:06:22 2014 
  238. Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved. 
  239. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production 
  240. With the Partitioning, OLAP, Data Mining and Real Application Testing options 
  241. Master table "SYSTEM"."SYS_IMPORT_TRANSPORTABLE_01" successfully loaded/unloaded 
  242. Starting "SYSTEM"."SYS_IMPORT_TRANSPORTABLE_01":  system/******** directory=tbs_dir dumpfile=p1.dmp transport_datafiles=/u01/app/oracle/oradata/prod/tbs1.dbf,/u01/app/oracle/oradata/prod/tbs1_indx.dbf logfile=imp.log 
  243. Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK 
  244. Processing object type TRANSPORTABLE_EXPORT/TABLE 
  245. Processing object type TRANSPORTABLE_EXPORT/INDEX 
  246. Processing object type TRANSPORTABLE_EXPORT/INDEX_STATISTICS 
  247. Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK 
  248. Job "SYSTEM"."SYS_IMPORT_TRANSPORTABLE_01" successfully completed at 18:06:37 
  249.    
  250. 数据导入成功 
  251. 18:01:03 SYS@ prod >select segment_name,partition_name,tablespace_name from dba_segments 
  252. 18:07:37   2  where segment_name in ('T1_TMP','T1_TMP_INDX'); 
  253. SEGMENT_NAME         PARTITION_ TABLESPACE 
  254. -------------------- ---------- ---------- 
  255. T1_TMP                          TBS1 
  256. T1_TMP_INDX                     TBS1_INDX 
  257.    
  258. 18:09:40 SCOTT@ prod >alter table t1 exchange partition p1 with table t1_tmp including indexes; 
  259. Table altered. 
  260.    
  261. 18:08:15 SYS@ prod >select segment_name,partition_name,tablespace_name from dba_segments 
  262. 18:10:46   2  where segment_name in ('T1','T1_INDX') order by 2; 
  263. SEGMENT_NAME         PARTITION_ TABLESPACE 
  264. -------------------- ---------- ---------- 
  265. T1                   P1         TBS1 
  266. T1_INDX              P1         TBS1_INDX 
  267. T1_INDX              P2         TBS2_INDX 
  268. T1                   P2         TBS2 
  269. T1_INDX              P3         TBS3_INDX 
  270. T1                   P3         TBS3 
  271. 6 rows selected. 
  272.    
  273. 访问正常(索引亦导入成功) 
  274. 18:12:07 SCOTT@ prod >col name for a50 
  275. 18:12:19 SCOTT@ prod >r 
  276.   1* select * from t1 where id=4 
  277.         ID NAME 
  278. ---------- -------------------------------------------------- 
  279.          4 C_OBJ# 
  280.          4 TAB$ 
  281. Elapsed: 00:00:00.00 
  282. Execution Plan 
  283. ---------------------------------------------------------- 
  284. Plan hash value: 1229066337 
  285. -------------------------------------------------------------------------------------------------------------- 
  286. | Id  | Operation                          | Name    | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop | 
  287. -------------------------------------------------------------------------------------------------------------- 
  288. |   0 | SELECT STATEMENT                   |         |     2 |  1030 |     1   (0)| 00:00:01 |       |       | 
  289. |   1 |  PARTITION RANGE SINGLE            |         |     2 |  1030 |     1   (0)| 00:00:01 |     1 |     1 | 
  290. |   2 |   TABLE ACCESS BY LOCAL INDEX ROWID| T1      |     2 |  1030 |     1   (0)| 00:00:01 |     1 |     1 | 
  291. |*  3 |    INDEX RANGE SCAN                | T1_INDX |     1 |       |     1   (0)| 00:00:01 |     1 |     1 | 
  292. -------------------------------------------------------------------------------------------------------------- 
  293. Predicate Information (identified by operation id): 
  294. --------------------------------------------------- 
  295.    3 - access("ID"=4) 
  296. Note 
  297. ----- 
  298.    - dynamic sampling used for this statement (level=2) 
  299. Statistics 
  300. ---------------------------------------------------------- 
  301.           0  recursive calls 
  302.           0  db block gets 
  303.           5  consistent gets 
  304.           0  physical reads 
  305.           0  redo size 
  306.         524  bytes sent via SQL*Net to client 
  307.         419  bytes received via SQL*Net from client 
  308.           2  SQL*Net roundtrips to/from client 
  309.           0  sorts (memory) 
  310.           0  sorts (disk) 
  311.           2  rows processed 
  312.              
  313. 18:11:05 SYS@ prod >alter tablespace tbs1 read write; 
  314. Tablespace altered. 
  315. Elapsed: 00:00:02.10 
  316. 18:14:34 SYS@ prod >alter tablespace tbs1_indx read write; 
  317. Tablespace altered. 

 

三、备份载入的原介质

wKioL1RsASeisT3lAAEZkx474KQ032.jpg


wKiom1RsARmQiCSkAAG2w6nkODw112.jpg  以上文档参考:谭怀远《让Oracle跑的更快2-基于海量数据的数据库设计和优化》,感谢作者!





收藏 推荐 打印 | 录入: | 阅读: 次
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数
点评:
       
评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款