淺談oracle邏輯備份、資料泵備份及冷備份

來源:互聯網
上載者:User

標籤:淺談oracle邏輯備份、資料泵備份及冷備份

邏輯備份(資料移轉):
以邏輯結構為為單位進行的備份
跨使用者移動資料
跨資料庫移動資料庫
為測試儲存原始的資料狀態
對資料庫進行版本升級

邏輯匯出的注意事項:
exp程式在目錄中發現同名檔案時會直接覆蓋,不提示!!
exp無法備份無段的空表
執行邏輯匯出時一定要注意字元集!最好使用包含中文的小表做測試!!
匯入時的資料和匯出時的資料一模一樣,匯出之後資料庫中表的資料變化全都丟失!!

邏輯匯出:所有版本都可用,伺服器端和用戶端都可用

mkdir -p /home/oracle/expbk
SQL> create table scott.t01 as select * from dba_objects;Table created.Elapsed: 00:00:00.32
SQL> select count(*) from scott.t01;  COUNT(*)----------     86259Elapsed: 00:00:00.01

(1)備份單張表:產生備份路徑及日誌目錄
exp userid=scott/tiger tables=t01 file=/home/oracle/expbk/t01.dmp buffer=1048576 feedback=10000 log=/home/oracle/expbk/t01.log

[[email protected] expbk]$ lltotal 9748-rw-r--r-- 1 oracle oinstall 9977856 Sep 26 13:25 t01.dmp-rw-r--r-- 1 oracle oinstall     552 Sep 26 13:25 t01.log

邏輯匯入:
drop table t01 purge;

SQL> drop table scott.t01 purge;Table dropped.Elapsed: 00:00:00.11
SQL> select * from scott.t01;select * from scott.t01                    *ERROR at line 1:ORA-00942: table or view does not existElapsed: 00:00:00.00

imp userid=scott/tiger tables=t01 file=/home/oracle/expbk/t01.dmp buffer=1048576 feedback=10000 log=/home/oracle/expbk/t01.log

SQL> select count(*) from scott.t01;  COUNT(*)----------     86259Elapsed: 00:00:00.00

(2)通過網路向遠程匯入:
 環境:準備2個oracle伺服器
 網路設定如下:
 vim /u01/app/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora

 75 =  (DESCRIPTION =    (ADDRESS = (PROTOCOL = TCP)(HOST = 10.201.xx.75)(PORT = 1521))    (CONNECT_DATA =      (SERVER = DEDICATED)      (SERVICE_NAME = orcl)    )  )74 =  (DESCRIPTION =    (ADDRESS = (PROTOCOL = TCP)(HOST = 10.201.xx.74)(PORT = 1521))    (CONNECT_DATA =      (SERVER = DEDICATED)      (SERVICE_NAME = orcl)    )  )

 
 #####################
vim /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora

LISTENER =  (DESCRIPTION_LIST =    (DESCRIPTION =      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))      (ADDRESS = (PROTOCOL = TCP)(HOST = s74sit)(PORT = 1521))    )  )

 
 串連到75,明顯的是s75sit沒有這張表scott.t01,如下:

 [[email protected] ~]$ sqlplus scott/[email protected] SQL*Plus: Release 11.2.0.4.0 Production on Mon Sep 26 14:19:17 2016Copyright (c) 1982, 2013, Oracle.  All rights reserved.Connected to:Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit ProductionWith the Partitioning, OLAP, Data Mining and Real Application Testing optionsSQL> select count(*) from scott.t01;select count(*) from scott.t01                           *ERROR at line 1:ORA-00942: table or view does not existElapsed: 00:00:00.00SQL>

遠程匯入到75這台伺服器
imp userid=scott/[email protected] tables=t01 file=/home/oracle/expbk/t01.dmp buffer=1048576 feedback=10000 log=/home/oracle/expbk/t01.log

在查看

SQL> select count(*) from scott.t01;  COUNT(*)----------     86259Elapsed: 00:00:00.00

成功了!!!!!!!!

(3)匯出資料時帶有查詢條件:select * from scott.emp where deptno=30;
exp scott/tiger tables=emp file=/home/oracle/expbk/emp_30.dmp query=\‘where deptno=30\‘ buffer=1000000 log=/home/oracle/expbk/emp_30.log

(4)匯入時追加資料:ignore=y
imp scott/tiger tables=emp file=/home/oracle/expbk/emp_30.dmp ignore=y buffer=1000000 log=/home/oracle/expbk/imp_emp_30.log

(5)只顯示匯入流程,不做匯入操作:show=y
imp scott/tiger tables=emp file=/home/oracle/expbk/emp_30.dmp ignore=y buffer=1000000 log=/home/oracle/expbk/imp_emp_30.log show=y

(6)閃回匯出(依賴於undo資料):system
exp system/uplooking tables=scott.emp file=/home/oracle/expbk/emp_1040.dmp buffer=1000000 flashback_time=\"to_timestamp\(\‘2016-09-24 10:45:00\‘,\‘yyyy-mm-dd hh24:mi:ss\‘\)\" log=/home/oracle/expbk/emp_1040.log

(7)全備份匯入資料:
imp system/uplooking file=/home/oracle/expbk/e03_1015.dmp full=y ignore=y show=y

只匯出表結構(中繼資料)不匯出資料:rows=n 備份模型,不備份資料  
exp scott/tiger tables=ob1 rows=n file=/home/oracle/expbk/ob1_metadata.dmp log=/home/oracle/expbk/ob1_metadata.log

(8)寫指令碼定時備份

vi /home/oracle/expbk/exp.sh

export ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1export ORACLE_SID=orclexport LANG=zh_CN.utf8export NLS_LANG=AMERICAN_AMERICA.WE8MSWIN1252name=`date ‘+%Y%m%d_%H%M%S‘``$ORACLE_HOME/bin/exp userid=scott/tiger tables=ob1 file=/home/oracle/expbk/ob1\_$name.dmp buffer=1048576 feedback=10000 log=/home/oracle/expbk/ob1\_$name.log`

chmod +x /home/oracle/expbk/exp.sh

(9)匯出使用者:owner=scott
exp userid=scott/tiger owner=scott file=/home/oracle/expbk/scott.dmp buffer=1048576 feedback=10000 log=/home/oracle/expbk/scott.log
匯入使用者:匯入使用者資料時,要在資料庫中將使用者建立好!
drop user scott cascade;

create user scott identified by tiger;
imp userid=scott/tiger full=y file=/home/oracle/expbk/scott.dmp buffer=1048576 feedback=10000 log=/home/oracle/expbk/impscott.log

(10)跨使用者匯入資料:scott-->tom
imp userid=system/uplooking file=scott.dmp fromuser=scott touser=tom tables=dept,emp,salgrade buffer=1000000 log=imptom.log

(11)使用主機管道壓縮備份檔案:
mknod /home/oracle/expbk/exp_pipe p
exp userid=scott/tiger owner=scott log=/home/oracle/expbk/scott.log file=/home/oracle/expbk/exp_pipe & gzip </home/oracle/expbk/exp_pipe> scott02.dmp.gz

(12)匯出資料表空間:
exp system/uplooking tablespaces=data01 file=/home/oracle/expbk/exp_data01.dmp buffer=1000000 log=/home/oracle/expbk/exp_data01.log
匯入資料表空間:匯入時原來的資料表空間如果不存在,表會被匯入到使用者的預設永久資料表空間
imp system/uplooking full=y file=/home/oracle/expbk/exp_data01.dmp buffer=1000000 log=/home/oracle/expbk/imp_data01.log

(13)傳輸資料表空間模式:資料庫版本必須一致,字元集必須一致
create tablespace teach10 datafile ‘/home/oracle/teach10.dbf‘ size 10m;
create table blake.crm (x int) tablespace teach10;
insert into blake.crm values (1990);
commit;
資料表空間需要在唯讀模式下
alter tablespace teach10 read only;
匯出資料表空間中繼資料
exp \‘sys/uplooking as sysdba\‘ tablespaces=teach10 transport_tablespace=y file=/home/oracle/expbk/teach10.dmp log=/home/oracle/expbk/teach10.log
將資料檔案傳到遠程
scp /home/oracle/teach10.dbf [email protected]:/u01/app/oracle/oradata/zccdb/
建立使用者
sqlplus sys/[email protected] as sysdba
grant connect,resource to blake identified by blake;
將中繼資料匯入到遠端資料庫
imp \‘sys/[email protected] as sysdba\‘ tablespaces=teach10 transport_tablespace=y file=/home/oracle/expbk/teach10.dmp datafiles=\‘/u01/app/oracle/oradata/zccdb/teach10.dbf\‘ log=/home/oracle/expbk/imp_teach10.log
資料表空間read write:
alter tablespace TEACH10 READ write;

(14)全庫模式:
exp system/uplooking full=y file=full.dmp buffer=10000000 log=full.log

create table scott.t2
(
id number(10),
name1 varchar2(15),
name2 varchar2(15),
name3 varchar2(15),
name4 varchar2(15),
name5 varchar2(15),
name6 varchar2(15),
name7 varchar2(15),
name8 varchar2(15),
name9 varchar2(15),
name10 varchar2(15),
name11 varchar2(15),
name12 varchar2(15),
name13 varchar2(15),
name14 varchar2(15),
name15 varchar2(15),
name16 varchar2(15),
name17 varchar2(15),
name18 varchar2(15),
name19 varchar2(15),
name20 varchar2(15),
name21 varchar2(15),
name22 varchar2(15),
name23 varchar2(15),
name24 varchar2(15),
name25 varchar2(15),
name26 varchar2(15),
name27 varchar2(15),
name28 varchar2(15),
name29 varchar2(15),
name30 varchar2(15),
name31 varchar2(15),
name32 varchar2(15),
name33 varchar2(15),
name34 varchar2(15),
name35 varchar2(15),
name36 varchar2(15),
name37 varchar2(15),
name38 varchar2(15),
name39 varchar2(15),
name40 varchar2(15),
name41 varchar2(15),
name42 varchar2(15),
name43 varchar2(15),
name44 varchar2(15),
name45 varchar2(15),
name46 varchar2(15),
name47 varchar2(15),
name48 varchar2(15),
name49 varchar2(15),
name50 varchar2(15));

begin
  for i in 1..100000 loop
    insert into scott.t2(id,name1) values (i,‘A‘);
  end loop;
  commit;
end;
/

##########################################################################################
資料泵:oracle 10g之後的版本可用,只有在服務端可用
1.建立邏輯目錄,使用邏輯目錄儲存資料泵的備份檔案
conn / as sysdba
select * from all_directories;
create or replace directory expbk as ‘/home/oracle/expbk‘;

2.授予scott使用者讀寫邏輯目錄的許可權
grant read,write on directory expbk to scott;

3.使用資料泵備份資料

只備份中繼資料資訊
expdp scott/tiger job_name=exp_ob1 directory=expbk tables=ob1 dumpfile=ob1_metadata.dmp content=metadata_only logfile=ob1_metadata.log

只資料資訊
expdp scott/tiger job_name=exp_ob1 directory=expbk tables=ob1 dumpfile=ob1_data.dmp content=data_only logfile=ob1_data.log

兩者都備份
expdp scott/tiger job_name=exp_ob1 directory=expbk tables=ob1 dumpfile=ob1_all.dmp content=all logfile=ob1_all.log


impdp scott/tiger directory=expbk dumpfile=ob1.dmp logfile=imp_ob1.log

expdp scott/tiger directory=expbk dumpfile=scott.dmp parallel=8 job_name=scott_job schemas=scott content=all logfile=scott.log

expdp system/oracle directory=expbk dumpfile=users.dmp parallel=8 job_name=users_job tablespaces=users content=all logfile=users.log
~~~~~~~~~~~~
alter tablespace teach10 read only;
expdp \‘sys/oracle as sysdba\‘ directory=expbk dumpfile=teach10.dmp tablespaces=teach10 transport_tablespace=y logfile=teach10.log

scp teach10.dmp [email protected]:/home/oracle/
scp /home/oracle/teach10.dbf [email protected]:/demo/teach10.dbf

create or replace directory expbk as ‘/home/oracle‘;

impdp \‘sys/oracle as sysdba\‘ directory=expbk dumpfile=teach10.dmp logfile=impteach10.log transport_datafiles=\‘/demo/teach10.dbf\‘
alter tablespace teach10 read write;
alter tablespace teach10 read write;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
資料泵備份全庫模式:

expdp system/oracle directory=expbk dumpfile=orcl.dmp parallel=8 job_name=orcl_job full=y logfile=orcl.log

expdp scott/tiger job_name=exp_e directory=expbk include=table:\"like \‘E%\‘\" dumpfile=e.dmp logfile=e.log

expdp scott/tiger job_name=exp_e directory=expbk include=view dumpfile=v.dmp
expdp scott/tiger job_name=exp_e directory=expbk include=procedure,package,function dumpfile=p.dmp

expdp scott/tiger job_name=exp_e directory=expbk exclude=table:\"like \‘E%\‘\" dumpfile=scott_not_e.dmp logfile=e.log
########################################################################################
使用者管理的物理備份:一停二拷三啟動
1.冷備份(datafile offline backup)
資料庫在shutdown狀態進行的備份

2.熱備份(datafile online backup)
需要歸檔模式支援

冷備份:

mkdir -p /home/oracle/coldbk/
vi /home/oracle/coldbk/shut.txtconn / as sysdbashutdown immediateexit
vi /home/oracle/coldbk/start.txtconn / as sysdbastartupexit

select ‘cp -v ‘||name||‘ /home/oracle/coldbk/‘
from
(select name from v$controlfile
union all
select name from v$datafile
union all
select member from v$logfile);

vi /home/oracle/coldbk/bk.sh

export ORACLE_SID=orclsqlplus /nolog @/home/oracle/coldbk/shut.txtcp -v $ORACLE_HOME/dbs/orapw$ORACLE_SID /home/oracle/coldbk/cp -v $ORACLE_HOME/dbs/spfile$ORACLE_SID.ora /home/oracle/coldbk/cp -v $ORACLE_HOME/dbs/init$ORACLE_SID.ora /home/oracle/coldbk/cp -v /u01/app/oracle/oradata/orcl/control01.ctl /home/oracle/coldbk/cp -v /u01/app/oracle/fast_recovery_area/orcl/control02.ctl /home/oracle/coldbk/cp -v /u01/app/oracle/oradata/orcl/system01.dbf /home/oracle/coldbk/cp -v /u01/app/oracle/oradata/orcl/sysaux01.dbf /home/oracle/coldbk/cp -v /u01/app/oracle/oradata/orcl/undotbs01.dbf /home/oracle/coldbk/cp -v /u01/app/oracle/oradata/orcl/users01.dbf /home/oracle/coldbk/cp -v /u01/app/oracle/oradata/orcl/redo03.log /home/oracle/coldbk/cp -v /u01/app/oracle/oradata/orcl/redo02.log /home/oracle/coldbk/cp -v /u01/app/oracle/oradata/orcl/redo01.log /home/oracle/coldbk/sqlplus /nolog @/home/oracle/coldbk/start.txt

chmod +x /home/oracle/coldbk/bk.sh


後面繼續講熱備份及rman備份,請多多關照,寫得不好,有問題請指出,謝謝!!!!

本文出自 “梁小明的部落格” 部落格,請務必保留此出處http://7038006.blog.51cto.com/7028006/1856658

淺談oracle邏輯備份、資料泵備份及冷備份

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.