Linux中OracleDatabase Backup方法

來源:互聯網
上載者:User

先來介紹一些不使用指令碼我們直接使用命令備份與還原oracle資料庫

Oracle資料備份:

步驟 1 備份使用者資料。

1.使用linux系統下的資料庫管理員帳號串連linux終端。

2. 執行以下語句,建立“bak_dir”檔案夾。

 代碼如下 複製代碼
 mkdir bak_dir

3. 執行以下語句,為“bak_dir”檔案夾賦予讀、寫和執行許可權。

 代碼如下 複製代碼
 chmod 777 bak_dir

4. 執行以下語句,以sysdba使用者登入oracle資料庫伺服器。
 sqlplus 資料庫管理員帳號/密碼@資料庫執行個體名 as sysdba
5. 執行以下語句,將“bak_dir”指定為“/opt/oracle/bak_dir”。

 代碼如下 複製代碼
 create or replace directory bak_dir as '/opt/oracle/bak_dir'
 commit

6. 執行以下語句,將“bak_dir”的讀、寫和執行許可權賦給xx(資料使用者名稱)使用者。

 代碼如下 複製代碼
 grant all on directory bak_dir to xx

 commit
7. 執行以下語句,退出oracle資料庫伺服器。

 代碼如下 複製代碼
 quit

執行以下語句,將sysdb使用者的表備份到“bak_dir”目錄下。
8.

 代碼如下 複製代碼

expdp xx(要匯出表所在的資料庫使用者名稱)/xx(密碼)@資料庫執行個體名 directory=bak_dir dumpfile=expdb_xx.dmp logfile=expdb_xxlog tables=表名

 

Oracle資料還原:

1.在命令列輸入:

 代碼如下 複製代碼
sqlplus "/as sysdba"

2. 執行以下語句登入xx資料庫,使用者名稱:xx,密碼:xx(請輸入當地實際密碼)
 conn xx/xx;

3.如果恢複的表中有涉及到觸發器的請停止觸發器,例:A表

 代碼如下 複製代碼

alter table A disable all triggers;

commit;

4. 執行以下語句退出目前使用者
 quit;
5. 執行以下語句,恢複使用者資料。

 代碼如下 複製代碼
 impdp xx/xx@資料庫執行個體名 directory=bak_dir table_exists_action=truncate dumpfile=expdb_xx.dmp logfile=impdb_xx.log

5.在命令列輸入:sqlplus "/as sysdba"

6. 執行以下語句登入xx資料庫,使用者名稱:xx,密碼:xx(請輸入當地實際密碼)
 conn xx/xx;

7. 執行以下語句開啟被禁止的觸發器

 代碼如下 複製代碼

alter table A enable all triggers;

commit;

上面方法是可以實現我們想要的,但但大型WEB伺服器肯定是要自動定時進行備份的。

例

 代碼如下 複製代碼


1.--建立資料資料表空間
2.create tablespace test_data
3.logging
4.datafile '/u01/app/oradata/test/TEST.dbf'
5.size 32m
6.autoextend on
7.next 32m maxsize 2048m
8.extent management local;
9.
10.--建立使用者並指定資料表空間
11.create user TEST identified by 123
12.default tablespace test_data
13.temporary tablespace temp;
14.
15.--給使用者授予許可權
16.grant connect,resource to TEST;

用Test使用者登入,建立一個表,並插入兩條資料:

 代碼如下 複製代碼

1.create table t1(
2.       Id varchar(50) primary key,
3.       title varchar(50)
4.);
5.
6.insert into t1 values(sys_guid(),'t1');
7.insert into t1 values(sys_guid(),'t2');
8.commit;

先寫一個匯出的指令檔:

 代碼如下 複製代碼

1.export ORACLE_BASE=/u01/app
2.export ORACLE_HOME=/u01/app/oracle
3.export ORACLE_SID=TEST
4.export PATH=$ORACLE_HOME/bin:$PATH
5.d=$(date '+%Y%m%d')
6.exp TEST/123@TEST file=/home/oracle/backup/$d.dmp log=/home/oracle/backup/$d.log owner=TEST
7.zip -m /home/oracle/backup/$d.zip /home/oracle/backup/$d.dmp /home/oracle/backup/$d.log

前4句是設定環境變數,因為crontab定時調用時,不會使用oracle使用者的環境變數,所以要先把它們導進來。第6行以當前日期作為匯出的檔案名稱,例如20120626.dmp和20120626.log。第7行把這兩個檔案打成一個zip包,並刪掉這兩個檔案。

要用chmod命令把這個sh標記為可執行:

 代碼如下 複製代碼

1.chmod +x backup.sh 

用oracle使用者,輸入crontab -e命令,編輯oracle使用者的任務計劃:

 代碼如下 複製代碼

1.[oracle@localhost backup]$ crontab -e
2.42 13 * * * /home/oracle/backup/backup.sh 這樣就添加了一個計劃,在每天的13點42分運行/home/oracle/backup/backup.sh。

這樣就可以了,利用linux計劃任務就實現了。

備份策略:

星期天 0 級
星期一,二,四,五,六 2 級
星期三 1 級

 

 代碼如下 複製代碼

--建立本地管理路徑

mkdir -p /dinglp/ora_managed/backup
mkdir -p /dinglp/ora_managed/backup
mkdir -p /dinglp/ora_managed/backup/export-
 

mkdir -p /dinglp/ora_managed/backup/log
mkdir -p /dinglp/ora_managed/backup/rman_backup
mkdir -p /dinglp/ora_managed/scripts

--建立rman資料表空間和rman使用者

create tablespace rman_tbs datafile '/oradata/luke/rman_tbs01.dbf' size 1024M;
create user rman_dlp identified by dlp default tablespace rman_tbs temporary tablespace temp;
grant connect,resource ,recovery_catalog_owner to rman;

--註冊catalog 資料庫

rman catalog rman_dlp/dlp
create catalog tablespace rman_tbs;
connect target sys/dg@priamry
register database;
report schema;

--設定備份參數

configure retention policy to redundancy 2;
configure retention policy to recovery window of 7 days;

--以下是備份指令碼(可以通過vi進行編輯)
dlp-> touch exp_rman.par
dlp-> touch exp_rman.sh
dlp-> touch rman_bk_LEVEL0.rcv   (資料庫0級備份)
dlp-> touch rman_bk_LEVEL0.sh
dlp-> touch rman_bk_LEVEL1.rcv (資料庫1級備份)
dlp-> touch rman_bk_LEVEL1.sh
dlp-> touch rman_bk_LEVEL2.rcv   (資料庫2級備份www.linuxidc.com)
dlp-> touch rman_bk_LEVEL2.sh

--倒出RMAN使用者資料指令碼exp_rman.par
##################################################
###               exp_rman.par                 ###
##################################################
userid=rman_dlp/dlp
file=/dinglp/ora_managed/backup/export/rman.dmp
log=/dinglp/ora_managed/backup/log/rman.log

--倒出RMAN資料SHELL指令碼exp_rman.sh
##################################################
###                 exp_rman.sh                ###
##################################################
#!/bin/bash
source /home/Oracle/.bash_profile
cd /dinglp/ora_managed/scripts
exp parfile=exp_rman.par

--零級備份RMAN指令碼rman_bk_LEVEL0.rcv
connect catalog rman_dlp/dlp
connect target sys/dg@primary
run {
allocate channel d1 type disk;
allocate channel d2 type disk;
backup incremental level 0 database format '/dinglp/ora_managed/backup/rman_backup/level0_%d_%s_%p_%u.bak'
tag='level 0' include current controlfile;
sql 'alter system archive log current';
backup archivelog all format '/dinglp/ora_managed/backup/rman_backup/log_%d_%s_%p_%u.bak' delete all input;
release channel d2;
release channel d1;
}
crosscheck backup;
delete noprompt expired backup;
delete noprompt obsolete;
resync catalog;
exit;

--零級備份SHELL指令碼的rman_bk_LEVEL0.sh
#####################################################################
###                   rman_bk_LEVEL0.sh                           ###
#####################################################################
#!/bin/bash
source /home/Oracle/.bash_profile
cd /dinglp/ora_managed/scripts
rman cmdfile=rman_bk_LEVEL0.rcv msglog=$HOME/backup/log/rman_bk_LEVEL0.log
./dinglp/ora_managed/script/exp_rman.sh

--一級差異增量備份RMAN指令碼rman_bk_LEVEL1.rcv
connect catalog rman_dlp/dlp
connect target sys/dg@primary
run {
allocate channel d1 type disk;
backup incremental level 1 format '/dinglp/ora_managed/backup/rman_backup/level1_%d_%s_%p_%u.bak' tag = 'level 1' database;
sql 'alter system archive log current';
backup archivelog all format '/dinglp/ora_managed/backup/rman_backup/log_%d_%s_%p_%u.bak' delete all input;
release channel d1;
}
crosscheck backup;
delete noprompt expired backup;
delete noprompt obsolete;
resync catalog;
exit;

--一級差異增量備份SHELL指令碼rman_bk_LEVEL1.sh
#####################################################################
###                   rman_bk_LEVEL1.sh                           ###
#####################################################################
#!/bin/bash
source /home/Oracle/.bash_profile
cd /dinglp/ora_managed/scripts
rman cmdfile=rman_bk_LEVEL1.rcv msglog=/dinglp/ora_managed/backup/log/rman_bk_LEVEL1.log
. /dinglp/ora_managed/scripts/exp_rman.sh

--二級差異增量備份RMAN指令碼rman_bk_LEVEL2.rcv
connect catalog rman_dlp/dlp
connect target sys/dg@primary
run {
allocate channel d1 type disk;
backup incremental level 2 format '/dinglp/ora_managed/backup/rman_backup/level2_%d_%s_%p_%u.bak' tag = 'level 2' database;
sql 'alter system archive log current';
backup archivelog all format '/dinglp/ora_managed/backup/rman_backup/log_%d_%s_%p_%u.bak' delete all input;
release channel d1;
}
crosscheck backup;
delete noprompt expired backup;
delete noprompt obsolete;
resync catalog;
exit;

--二級差異增量備份SHELL指令碼rman_bk_LEVEL2.sh
#####################################################################
###                   rman_bk_LEVEL2.sh                           ###
#####################################################################
#!/bin/bash
source /home/Oracle/.bash_profile
cd /dinglp/ora_managed/scripts
rman cmdfile=rman_bk_LEVEL2.rcv msglog=/dinglp/ora_managed/backup/log/rman_bk_LEVEL2.log
. /dinglp/ora_managed/scripts/exp_rman.sh

--提高RMAN增量備份效能

alter database enable block change tracking using file '/u01/app/Oracle/admin/devdb/bdump/luke.log';

desc v$block_change_tracking;

--RMAN 動態視圖
V$ARCHIVED_LOG             顯示在資料庫中已經建立、備份或清除的歸檔檔案。
V$BACKUP_CORRUPTION    顯示在備份組的備份過程中找到的損壞塊。
V$COPY_CORRUPTION    顯示映像複製過程中找到的損壞塊。
V$BACKUP_DATAFILE    用於通過確定各資料檔案中的塊數來建立大小相同的備份組。通過它也可以找出資料檔案中已損壞的塊數。    V$BACKUP_REDOLOG    顯示在備份組中儲存的歸檔日誌。
V$BACKUP_SET     顯示已經建立的備份組。
V$BACKUP_PIECE    顯示為備份組建立的備份片。

--如何監視複製進程
使用 SET COMMAND ID 命令可將伺服器會話與通道聯絡起來。
查詢 V$PROCESS 和 V$SESSION,可以確定會話與哪些 RMAN 通道對應。
查詢 V$SESSION_LONGOPS,可以監視備份和複製的進度。

--linux下自動運行備份指令碼
crontab格式簡介
第1列分鐘1~59
第2列小時1~23(0表示子夜)
第3列日1~31
第4列月1~12
第5列星期0~6(0表示星期天)
第6列要啟動並執行命令

[root@dlp ~]# vi /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
00 22 * * 0 root /dinglp/ora_managed/scripts/rman_bk_LEVEL0.sh
00 22 * * 3 root /dinglp/ora_managed/scripts/rman_bk_LEVEL1.sh
00 22 * * 1,2,4,5,6 root /dinglp/ora_managed/scripts/rman_bk_LEVEL2.sh

--完畢,RYOHEI,2010-08-04。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.