--------------------------- 7-17_上午_oracle_熱備份_01 -------------
-------------- 修改爲 archive log 模式 --------------
archive log list;
show parameter archive;
alter system set log_archive_format='%s_%t_%r.arc' scope=spfile;
alter system set log_archive_dest_1='location=/arc'; -- 請先以建立arc目錄,並給屬主改爲:oracle
shutdown immediate;
startup mount;
alter database archivelog; -- alter database noarchivelog; 可關閉archive 模式;
alter database open;
--------------- 切換日期,以使得 archive log 生成 ----------------------------
select * from v$log;
alter system switch logfile;
/
/
select * from v$log;
--------------- 進行熱備份與恢復 ------------------
select file#, name, checkpoint_change# from v$datafile; -- 可查看本數據的數據文件位置、checkpoint 檢查點的情況;
alter tablespace users begin backup; -- 告訴 user 表空間 要開始備份了, 我要開始備份了; 1.存盤,2.凍結表空間的塊頭;
select file#, name, checkpoint_change# from v$datafile; -- 再看錶空間users 數據文件 checkpoint_chang# 變化了
host cp /u01/app/oracle/oradata/orcl/users01.dbf /bk -- 進行熱備份
startup force; -- 啓動不了,報錯,說 need media recovery
select file#, name, checkpoint_change# from v$datafile;
alter database end backup; -- 結束 備份; 4號文件變爲, NOT ACTIVE , 才能正常啓動;因爲表空間是邏輯層的,到mount狀態後,數據文件都檢查完,但到open時,就要檢查表空間了,而表空間又是本地管理,所以就報錯;
alter database open; -- 可以正常啓動;
------------ 7-17_上午_oracle_熱備份_03_驗證日誌增長 ------
---- 1 window
conn scott/tiger;
select * from tab;
drop table t1;
create table t1 as emp;
insert into t1 select * from t1;
/
/
/
/
/
commit;
-- 2.window
sqlplus / as sysdba;
conn / as sysdba;
SELECT * from v$sysstat where class=2;
select value from v$sysstat where STATISTIC#=178; -- 178 是上條語句查出的 redo size 對應的 statistic# 號
SQL> SELECT value from v$sysstat where STATISTIC#=178;
VALUE
----------
3776244
-- 1.window
update t1 set sal=sal+1 where empno=7900;
commit;
-- 2.window
SELECT value from v$sysstat where STATISTIC#=178; -- 注意比剛纔大了; 約幾M ,這裏假定 3M
alter tablespace users begin backup; -- 凍結表空間;
-- 1.window
update t1 set sal=sal+1 where empno=7900;
commit;
-- 2.window
SELECT value from v$sysstat where STATISTIC#=178; -- 注意第一次update大好; 增長一倍
------------ 7-17_上午_oracle_熱備份_04_備份與恢復
sqlplus / as sysdba
SQL> select name from v$datafile;
NAME
---------------------------------------------
/u01/app/oracle/oradata/orcl/system01.dbf
/u01/app/oracle/oradata/orcl/sysaux01.dbf
/u01/app/oracle/oradata/orcl/undotbs01.dbf
/u01/app/oracle/oradata/orcl/users01.dbf
/u01/app/oracle/oradata/orcl/tbsp1.dbf
SQL> !cp /u01/app/oracle/oradata/orcl/users01.dbf /bk/users01.dbf.bk2 -- 備份數據文件
update scott.emp set sal=sal+1;
commit;
alter system switch logfile;
-- 多執行幾次
--- 銷燬文件
save /bk/1.txt;
select * from scott.emp;
SQL> !cp /bk/1.txt /u01/app/oracle/oradata/orcl/users01.dbf; -- 模擬文件損壞
select * from scott.emp; --- 有可能看見,因爲在buffer_cache中
alter system flush buffer_cache;
alter system flush check_point; -- 注意 11G 一刷數據庫就自動關掉了,,,,
startup force; -- 起不來,報錯
select * from scott.emp; --- 看不見了.....報錯..
SQL> select * from v$recover_file; -- 查找 需要恢復文件
!cp /bk/users01.dbf.bk2 /u01/app/oracle/oradata/orcl/users01.dbf; -- 取備份,恢復正確的文件回來;
SQL> recover datafile 4; -- 從archive log 日誌中恢復數據文件; archive log下,一路回車,只要備份文件在;
alter tablespace users online; -- 或是 startup force;