首页 > 代码库 > 手工热备份
手工热备份
1、首先要开归档,因为是热备份
(1)创建表空间
SQL> create tablespace lxtbs datafile ‘/u01/app/oracle/oradata/PROD/disk3/lxtbs01.dbf‘ size 50m;
SQL> select tablespace_name,contents,status from dba_tablespaces;
TABLESPACE_NAME CONTENTS STATUS
------------------------------ --------- ---------
SYSTEM PERMANENT ONLINE
UNDOTBS UNDO ONLINE
SYSAUX PERMANENT ONLINE
TEMPTS TEMPORARY ONLINE
LXTBS PERMANENT ONLINE
SQL> select file#,name from v$datafile;
FILE# NAME
---------- --------------------------------------------------
1 /u01/app/oracle/oradata/PROD/disk3/system01.dbf
2 /u01/app/oracle/oradata/PROD/disk4/undotbs01.dbf
3 /u01/app/oracle/oradata/PROD/disk4/sysaux01.dbf
4 /u01/app/oracle/oradata/PROD/disk3/lxtbs01.dbf
(2)让数据文件的scn号一致
SQL> select file#,checkpoint_change# from v$datafile;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 298164
2 298164
3 298164
4 299134 --不一致
SQL> alter system checkpoint; --生成全局检查点,写脏块
SQL> select file#,checkpoint_change# from v$datafile; --一致
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 299378
2 299378
3 299378
4 299378
(3)将之前创建的表空间设为只读
SQL> alter tablespace lxtbs read only; --只读
SQL> alter system checkpoint; --生成全局检查点
SQL> select file#,checkpoint_change# from v$datafile; --发现scn不一致,只有读写状态下才会一致
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 299473
2 299473
3 299473
4 299440
(4)备份失败的情况
SQL> alter tablespace lxtbs begin backup; --只读状态下备份,报错,因为它对数据文件加不上锁
SQL> alter tablespace lxtbs read write; --将表空间变为可写
SQL> alter system checkpoint; --生成全局检查点
SQL> select file#,checkpoint_change# from v$datafile; --一致
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 299628
2 299628
3 299628
4 299628
SQL> alter tablespace lxtbs begin backup; --开备份
SQL> shutdown immediate --不让关库
SQL> shutdown abort; --可以用abort关库
SQL> startup --启动失败,需要恢复表空间lxtbs;
SQL> recover tablespace lxtbs; --恢复表空间
SQL> alter database open; --启库
(5)备份成功
SQL> alter tablespace lxtbs begin backup; --备份
[oracle@gc1 ~]$ mkdir tbs_bak --创建目录
[oracle@gc1 ~]$ cd tbs_bak
[oracle@gc1 tbs_bak]$ pwd --路径
/home/oracle/tbs_bak
[oracle@gc1 tbs_bak]$ cp /u01/app/oracle/oradata/PROD/disk3/lxtbs01.dbf /home/oracle/tbs_bak --拷贝表空间文件
[oracle@gc1 tbs_bak]$ ls --查看
lxtbs01.dbf
SQL> alter tablespace lxtbs end backup; --结束备份
(6)假如在此时断电,文件也坏了,对其恢复:
SQL> shutdown abort --断电
[oracle@gc1 tbs_bak]$ rm /u01/app/oracle/oradata/PROD/disk3/lxtbs01.dbf
SQL> startup --启库失败
SQL> select file#,error from v$recover_file; --检查错误发生的范围
FILE# ERROR
---------- -----------------------------------------------------------------
4 FILE NOT FOUND
[oracle@gc1 tbs_bak]$ cp lxtbs01.dbf /u01/app/oracle/oradata/PROD/disk3/lxtbs01.dbf --备份转储
SQL> select file#,checkpoint_change# from v$datafile; --发现数据文件的scn号不样
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 340671
2 340671
3 340671
4 340797
SQL> recover datafile 4; --对数据文件进行恢复
SQL> alter database open; --启库
2、手工热备份实验
(1)开归档
(2)查看表空间
SQL> select tablespace_name,contents,status from dba_tablespaces;
TABLESPACE_NAME CONTENTS STATUS
--------------- ------------------ ------------------
SYSTEM PERMANENT ONLINE
UNDOTBS1 UNDO ONLINE
SYSAUX PERMANENT ONLINE
TEMP TEMPORARY ONLINE
USERS PERMANENT ONLINE
EXAMPLE PERMANENT ONLINE
GOLDENGATE PERMANENT ONLINE
TBS_GGUSER PERMANENT ONLINE
SQL> select tablespace_name from dba_tablespaces where tablespace_name not like ‘TMP%‘ and status=‘ONLINE‘;
TABLESPACE_NAME
---------------
SYSTEM
UNDOTBS1
SYSAUX
TEMP
USERS
EXAMPLE
GOLDENGATE
TBS_GGUSER
我们要备份的是除临时表空间外的所有的online表空间
创建目录参考冷备
(3)写备份脚本
[oracle@gc2 prod_bak]$ vi hot_bak.sql
set echo off trimspool off heading off feedback off verify off time off
set pagesize 0 linesize 200
define bakdir=‘/home/oracle/prod_bak/hot_bak‘
define bakscp=‘/home/oracle/prod_bak/hot_cmd.sql‘
set serveroutput on
spool &bakscp
prompt alter system switch logfile;;
declare
cursor cu_tablespace is
select tablespace_name from dba_tablespaces where contents not like ‘TEMP%‘ and status=‘ONLINE‘;
cursor cu_datafile(name varchar2) is
select file_name from dba_data_files where tablespace_name=name;
begin
for i in cu_tablespace loop
dbms_output.put_line(‘alter tablespace ‘||i.tablespace_name||‘ begin backup;‘);
for j in cu_datafile(i.tablespace_name) loop
dbms_output.put_line(‘host cp ‘||j.file_name||‘ &bakdir ‘);
end loop;
dbms_output.put_line(‘alter tablespace ‘||i.tablespace_name||‘ end backup;‘);
end loop;
dbms_output.put_line(‘alter database backup controlfile to trace;‘);
dbms_output.put_line(‘alter database backup controlfile to ‘‘&bakdir/control01.ctl‘‘;‘);
end;
/
spool off
@&bakscp
(4)跑脚本
SQL> @/home/oracle/prod_bak/hot_bak
可以看到生成了备份的中间脚本
[oracle@gc2 prod_bak]$ ls
hot_bak hot_bak.sql hot_cmd.sql
备份成功。
本文出自 “柚子” 博客,请务必保留此出处http://6205351.blog.51cto.com/6195351/1530762