欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

oracle备份之备份测试脚本的方法(冷备、热备、rman)

程序员文章站 2022-06-14 23:12:59
1、数据库环境数据库dbid及打开模式sql> select dbid,open_mode from v$database; dbid open_mode---------- ---------...

1、数据库环境

数据库dbid及打开模式

sql> select dbid,open_mode from v$database; 
dbid open_mode
---------- ----------
4106451204 read write

数据文件:

sql> select file#,status,enabled,name from v$datafile;

file# status enabled name
---------- ------- ---------- --------------------------------------------------------------------------------
1 system read write /u01/app/oracle/oradata/ocmdb/dfile/system01.dbf
2 online read write /u01/app/oracle/oradata/ocmdb/undotbs01.dbf
3 online read write /u01/app/oracle/oradata/ocmdb/dfile/sysaux01.dbf
4 online read write /u01/app/oracle/oradata/ocmdb/dfile/tbs_1.dbf
5 online read write /u01/app/oracle/oradata/ocmdb/dfile/tbs3.dbf
6 online read write /u01/app/oracle/oradata/ocmdb/dfile/tbs_big01.dbf
7 online read write /u01/app/oracle/oradata/ocmdb/dfile/rman.dbf

控制文件:

sql> select status,name from v$controlfile;

status name
------- --------------------------------------------------------------------------------
/u01/app/oracle/ocmdb/cfile/ora_control1
/u01/app/oracle/ocmdb/cfile/ora_control2
/u01/app/oracle/ocmdb/cfile/control3

联机日志:

sql> select group#,status,type,member from v$logfile;

group# status type member
---------- ------- ------- --------------------------------------------------------------------------------
1 online /u01/app/oracle/oradata/ocmdb/lfile/redo01.log
1 online /u01/app/oracle/oradata/ocmdb/lfile/redo11.log
3 online /u01/app/oracle/oradata/ocmdb/lfile/redo03.log
2 online /u01/app/oracle/oradata/ocmdb/lfile/redo02.log
5 online /u01/app/oracle/oradata/ocmdb/lfile/redo05.log
5 online /u01/app/oracle/oradata/ocmdb/lfile/redo55.log
4 online /u01/app/oracle/oradata/ocmdb/lfile/redo04.log
4 online /u01/app/oracle/oradata/ocmdb/lfile/redo44.log


2、数据库备份脚本

冷备份脚本

#rem script:coldbak.sh
#rem creater:chon
#rem date:2011
#rem desc:offline full backup database

#--connect database
sqlplus / as sysdba <<eof
#--shutdown database
shutdown immediate;
#--copy data file
!cp /u01/app/oracle/oradata/ocmdb/dfile/*.dbf /backup
!cp /u01/app/oracle/oradata/ocmdb/undotbs01.dbf /backup
#--copy control file
!cp /u01/app/oracle/ocmdb/cfile/* /backup
#--copy log file
!cp /u01/app/oracle/oradata/ocmdb/lfile/*.log /backup
#--copy archive log
!cp -r /u01/app/oracle/rmanbak/ocmdb/archivelog/* /backup
#--startup database
startup;

说明:
1、以上脚本在数据库关闭状态下备份数据库所有的数据文件,联机日志,控制文件,归档日志(在一个目录下),如果成功备份,所有文件是一致的。
2、没有备份参数文件,参数文件可以另外备份,没有必要每次都备份,只需要在改变设置后备份一次。
3、如果以上命令没有成功依次执行,那么备份将是无效的,如连接数据库不成功,那么肯定关闭数据库也不成功,那么备份则无效
4、冷备份建议下人工干预下执行。

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

os热备份
查看表空间和数据文件对应关系

sql> select file_id,tablespace_name,file_name from dba_data_files;

file_id tablespace_name file_name
---------- ------------------------------ --------------------------------------------------------------------------------
7 rman_ts /u01/app/oracle/oradata/ocmdb/dfile/rman.dbf
6 big_tbs /u01/app/oracle/oradata/ocmdb/dfile/tbs_big01.dbf
5 tbs3 /u01/app/oracle/oradata/ocmdb/dfile/tbs3.dbf
4 tbs_1 /u01/app/oracle/oradata/ocmdb/dfile/tbs_1.dbf
3 sysaux /u01/app/oracle/oradata/ocmdb/dfile/sysaux01.dbf
2 undotbs /u01/app/oracle/oradata/ocmdb/undotbs01.dbf
1 system /u01/app/oracle/oradata/ocmdb/dfile/system01.dbf


数据库os热全备份脚本

#rem script:hotbak.sh
#rem creater:chon
#rem date:2011
#rem desc:backup all database datafile in archive

#--connect database
sqlplus / as sysdba <<eof

#--archive
alter system archive log current;

#--start hotbak
alter tablespace system begin backup;
!cp /u01/app/oracle/oradata/ocmdb/dfile/system01.dbf /backup
alter tablespace system end backup;

alter tablespace undotbs begin backup;
!cp /u01/app/oracle/oradata/ocmdb/undotbs01.dbf /backup
alter tablespace undotbs end backup;

alter tablespace sysaux begin backup;
!cp /u01/app/oracle/oradata/ocmdb/dfile/sysaux01.dbf /backup
alter tablespace sysaux end backup;

alter tablespace tbs_1 begin backup;
!cp /u01/app/oracle/oradata/ocmdb/dfile/tbs_1.dbf /backup
alter tablespace tbs_1 end backup;

alter tablespace tbs3 begin backup;
!cp /u01/app/oracle/oradata/ocmdb/dfile/tbs3.dbf /backup
alter tablespace tbs3 end backup;

alter tablespace big_tbs begin backup;
!cp /u01/app/oracle/oradata/ocmdb/dfile/tbs_big01.dbf /backup
alter tablespace big_tbs end backup;

alter tablespace rman_ts begin backup;
!cp /u01/app/oracle/oradata/ocmdb/dfile/rman.dbf /backup
alter tablespace rman_ts end backup;
#--end

#--bak control file
#--binary
alter database backup controlfile to '/backup/controlbinbak.000';
#--ascii
alter database backup controlfile to trace;

alter system archive log current;

说明:
1、热备份必须在数据库归档方式下才可以运行
2、以上脚本可以在数据库运行状态下备份数据库所有的数据文件(除了临时数据文件),没有必要备份联机日志。
3、归档日志至少需要一次完整备份之后的所有日志。
4、如果以上命令没有成功依次执行,那么备份也是无效的,如连接数据库不成功,那么备份则无效


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

rman备份脚本
--本地

创建文件夹

mkdir /home/oracle/backup
mkdir /home/oracle/backup/script
mkdir /home/oracle/backup/log
mkdir /home/oracle/backup/export


--备份目的地

mkdir /home/oracle/rman_backup

创建备份表空间

create tablespace rman_tbs datafile '/oradata/luke/rman_tbs01.dbf' size 1024m;

创建备份用户

create user rman identified by rman default tablespace rman_tbs temporary tablespace temp;
grant connect,resource ,recovery_catalog_owner to rman;

rman catalog rman/rman
create catalog tablespace rman_tbs;
connect target sys/lukewhx@luke
register database;
report schema;

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

--倒出rman用户数据脚本exp_rman.par (即备份catalog库)
userid=rman/rman
file=/home/oracle/backup/export/rman.dmp
log=/home/oracle/backup/log/rman.log

-- 倒出rman数据shell脚本exp_rman.sh
#!/bin/bash
cd $home
. .bash_profile
cd $home/backup/script
exp parfile=exp_rman.par

-- 零级备份rman脚本level0_backup.rcv
connect catalog rman/rman
connect target sys/unumall@unu2
run {

configure retention policy to recovery window of 14 days; 
configure controlfile autobackup on; 
allocate channel d1 type disk;
allocate channel d2 type disk;
backup incremental level 0 database format '/home/oracle/rman_backup/level0_%d_%s_%p_%u.bak'
tag='level 0' include current controlfile;
sql 'alter system archive log current';
backup archivelog all format '/home/oracle/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;

--零级备份shell脚本的level0_backup.sh
#!/bin/bash
cd $home
. .bash_profile
cd $home/backup/script
rman cmdfile=level0_backup.rcv msglog=$home/backup/log/level0_backup.log
. /home/oracle/backup/script/exp_rman.sh


--一级差异增量备份rman脚本 level1_backup.rcv
connect catalog rman/rman
connect target sys/luke@luke
run {
allocate channel d1 type disk;
backup incremental level 1 format '/home/oracle/rman_backup/level1_%d_%s_%p_%u.bak' tag = 'level 1' database;
sql 'alter system archive log current';
backup archivelog all format '/home/oracle/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;


--一级差异增量备份shell脚本level1_backup.sh


#!/bin/bash
cd $home
. .bash_profile
cd /home/oracle/backup/script
rman cmdfile=level1_backup.rcv msglog=$home/backup/log/level1.log
. /home/oracle/backup/script/exp_rman.sh


--二级差异增量备份rman脚本 level2_backup.rcv
connect catalog rman/rman
connect target sys/luke@luke
run {
allocate channel d1 type disk;
backup incremental level 2 format '/home/oracle/rman_backup/level2_%d_%s_%p_%u.bak' tag = 'level 2' database;
sql 'alter system archive log current';
backup archivelog all format '/home/oracle/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;

--二级差异增量备份shell脚本level2_backup.sh

#!/bin/bash
cd $home
. .bash_profile
cd /home/oracle/backup/script
rman cmdfile=level2_backup.rcv msglog=$home/backup/log/level2.log
. /home/oracle/backup/script/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;

linux下定时执行备份脚本

crontab -e -u oracle
shell=/bin/bash --以下脚本在bash下执行
mailto=oracle --执行日志以邮件形式邮给oracle用户,可以/var/spool/mail/oracle下查收
10 1 * * 0 /home/oracle/backup/script/level0_backup.sh
10 1 * * 1 /home/oracle/backup/script/level2_backup.sh
10 1 * * 2 /home/oracle/backup/script/level2_backup.sh
10 1 * * 3 /home/oracle/backup/script/level1_backup.sh
10 1 * * 4 /home/oracle/backup/script/level2_backup.sh
10 1 * * 5 /home/oracle/backup/script/level2_backup.sh
10 1 * * 6 /home/oracle/backup/script/level2_backup.sh


一周差异备份策略:
备份目标库和catalog库
周日0级全备,周一周二为2级,周三为1级,周四周五周六为2级。
每天凌晨1点10分开始备份


零级备份

backup incremental level 0 database;


 

一级差异增量 差异增量是默认增量备份方式

backup incremental level 1 database;

一级累计增量

backup incremental level 1 cumulative database;

总结

到此这篇关于oracle备份之备份测试脚本的方法(冷备、热备、rman)的文章就介绍到这了,更多相关oracle备份测试脚本内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!