本文是《RMAN备份策略与异机恢复一例》的续篇,继续实验验证,最终实现两个需求:
- 1.异机恢复临时测试的小库
- 2.传输归档时,实现增量传输
1.异机恢复临时测试的小库
之前异机恢复的需求已经演练成功,现在需要保持之前恢复的库orcl不动,全新恢复一个用于临时测试指定时间点的小库,实例名称规划为testdb。
注意:风险点在于一定要注意所有步骤操作仔细,不能影响到现有环境orcl。
知识点:
- a.如何恢复指定时间点的数据库
- b.如何重定向数据库各文件的路径
- c.如何验证数据是否是自己需要的
查询数据库全备有两个日期的,一个是2018-06-19的全备,一个是2018-06-21的全备。
RMAN> list backup of database summary;
List of Backups
===============
Key TY LV S Device Type Completion Time #Pieces #Copies Compressed Tag
------- -- -- - ----------- --------------- ------- ------- ---------- ---
45 B F A DISK 21-JUN-18 1 1 YES TAG20180621T073703
46 B F A DISK 19-JUN-18 1 1 YES TAG20180619T170003
54 B F A DISK 21-JUN-18 1 1 YES TAG20180621T073703
58 B F A DISK 21-JUN-18 1 1 YES TAG20180621T073703
60 B F A DISK 21-JUN-18 1 1 YES TAG20180621T073703
若直接restore database,默认会从最新的全备转储恢复,那如何可以用到之前的全备?
RMAN> sql "alter session set nls_date_format=''yyyy-mm-dd hh24:mi:ss''";
sql statement: alter session set nls_date_format=''yyyy-mm-dd hh24:mi:ss''
RMAN> restore database until time '2018-06-20 18:00:00' validate;
Starting restore at 21-JUN-18
using channel ORA_DISK_1
using channel ORA_DISK_2
using channel ORA_DISK_3
using channel ORA_DISK_4
using channel ORA_DISK_5
using channel ORA_DISK_6
channel ORA_DISK_1: starting validation of datafile backup set
channel ORA_DISK_1: reading from backup piece /tmp/FULLBAK_ORCL_20180619_18_1.DBFILE
channel ORA_DISK_1: piece handle=/tmp/FULLBAK_ORCL_20180619_18_1.DBFILE tag=TAG20180619T170003
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: validation complete, elapsed time: 00:00:45
Finished restore at 21-JUN-18
RMAN>
可以看到只要指定了合适的until time参数,就自动会从之前合适的备份集中转储(这里实验就是20180619了)。
现在来恢复小库到2018-06-20 18:00:00这个时间点,实例名为testdb:
[oracle@jystdrac1 ~]$ export ORACLE_SID=testdb
[oracle@jystdrac1 ~]$ echo $ORACLE_SID
testdb
[oracle@jystdrac1 ~]$ mkdir -p /u01/oradata/testdb
参数文件,适当减少内存参数,增加db_unique_name参数:
[oracle@jystdrac1 tmp]$ strings ORCL.46.1.20180621.SPFILE
[oracle@jystdrac1 testdb]$ vi pfile.ora
*.audit_file_dest='/u01/app/oracle/admin/testdb/adump'
*.audit_trail='db'
*.compatible='11.2.0.4.0'
*.control_files='/u01/oradata/testdb/control01.ctl','/u01/oradata/testdb/control02.ctl'
*.db_block_size=8192
*.db_domain=''
*.db_name='orcl'
*.db_unique_name='testdb'
*.diagnostic_dest='/u01/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=testdbXDB)'
*.log_archive_dest_1='LOCATION=/u01/arch'
*.memory_target=334173952
*.open_cursors=300
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.undo_tablespace='UNDOTBS1'
[oracle@jystdrac1 testdb]$ mkdir -p /u01/app/oracle/admin/testdb/adump
启动到nomount:
SQL> startup nomount pfile='/u01/oradata/testdb/pfile.ora';
恢复控制文件:
[oracle@jystdrac1 tmp]$ rman target /
RMAN> restore controlfile from '/tmp/ORCL.45.1.20180621.CTL';
RMAN> alter database mount;
select name from v$datafile union all
select name from v$tempfile union all
select member from v$logfile;
NAME
------------------------------------------------------
/u01/oradata/orcl/system01.dbf
/u01/oradata/orcl/sysaux01.dbf
/u01/oradata/orcl/undotbs01.dbf
/u01/oradata/orcl/users01.dbf
/u01/oradata/orcl/temp01.dbf
/u01/oradata/orcl/redo03.log
/u01/oradata/orcl/redo02.log
/u01/oradata/orcl/redo01.log
8 rows selected.
set linesize 140 pagesize 100
select 'alter database rename file '''||name||''' to '''||name||''';' from v$tempfile union all
select 'alter database rename file '''||member||''' to '''||member||''';' from v$logfile;
替换规则:`to '/u01/oradata/orcl` => to `'/u01/oradata/testdb`
alter database rename file '/u01/oradata/orcl/temp01.dbf' to '/u01/oradata/testdb/temp01.dbf';
alter database rename file '/u01/oradata/orcl/redo03.log' to '/u01/oradata/testdb/redo03.log';
alter database rename file '/u01/oradata/orcl/redo02.log' to '/u01/oradata/testdb/redo02.log';
alter database rename file '/u01/oradata/orcl/redo01.log' to '/u01/oradata/testdb/redo01.log';
--注意如果这里尝试rename数据文件,由于指定的数据文件还不存在,是会报错的,类似这样的错误:
SQL> alter database rename file '/u01/oradata/orcl/sysaux01.dbf' to '/u01/oradata/testdb/sysaux01.dbf'
*
ERROR at line 1:
ORA-01511: error in renaming log/data files
ORA-01141: error renaming data file 2 - new file '/u01/oradata/testdb/sysaux01.dbf' not found
ORA-01110: data file 2: '/u01/oradata/orcl/sysaux01.dbf'
ORA-27037: unable to obtain file status
Linux-x86_64 Error: 2: No such file or directory
Additional information: 3
检查备份和归档:
crosscheck backup;
crosscheck archivelog all;
delete noprompt expired backup;
delete noprompt expired archivelog all;
catalog start with '/tmp/';
crosscheck backup;
重定向数据文件-方法1:
run {
set newname for datafile '/u01/oradata/orcl/system01.dbf' to '/u01/oradata/testdb/system01.dbf';
set newname for datafile '/u01/oradata/orcl/sysaux01.dbf' to '/u01/oradata/testdb/sysaux01.dbf';
set newname for datafile '/u01/oradata/orcl/undotbs01.dbf' to '/u01/oradata/testdb/undotbs01.dbf';
set newname for datafile '/u01/oradata/orcl/users01.dbf' to '/u01/oradata/testdb/users01.dbf';
sql "alter session set nls_date_format=''yyyy-mm-dd hh24:mi:ss''";
set until time '2018-06-20 18:00:00';
restore database;
switch datafile all;
}
RMAN> recover database until time '2018-06-20 18:00:00';
重定向数据文件-方法2:
--这里语法要求一定要指定%f或者%U,通常选择%U更易识别一些,这里测试使用%f只有文件号,不够友好。
run {
set newname for database to '/u01/oradata/testdb/%f';
sql "alter session set nls_date_format=''yyyy-mm-dd hh24:mi:ss''";
set until time '2018-06-20 18:00:00';
restore database;
switch datafile all;
}
RMAN> recover database until time '2018-06-20 18:00:00';
此时一定要重新确认下临时文件和redo日志文件的位置,如果没有更改正确,务必改正,否则会造成灾难性后果!!!
SQL> select name from v$datafile union all
2 select name from v$tempfile union all
3 select member from v$logfile;
NAME
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/u01/oradata/testdb/1
/u01/oradata/testdb/2
/u01/oradata/testdb/3
/u01/oradata/testdb/4
/u01/oradata/orcl/temp01.dbf
/u01/oradata/orcl/redo03.log
/u01/oradata/orcl/redo02.log
/u01/oradata/orcl/redo01.log
8 rows selected.
--如果不对,需要rename修改,然后再次查询确认:
alter database rename file '/u01/oradata/orcl/temp01.dbf' to '/u01/oradata/testdb/temp01.dbf';
alter database rename file '/u01/oradata/orcl/redo03.log' to '/u01/oradata/testdb/redo03.log';
alter database rename file '/u01/oradata/orcl/redo02.log' to '/u01/oradata/testdb/redo02.log';
alter database rename file '/u01/oradata/orcl/redo01.log' to '/u01/oradata/testdb/redo01.log';
汇总一下本节开头提出的问题:
a.如何恢复指定时间点的数据库
RMAN>
sql "alter session set nls_date_format=''yyyy-mm-dd hh24:mi:ss''";
restore database until time '2018-06-20 18:00:00';
recover database until time '2018-06-20 18:00:00';
b.如何重定向数据库各文件的路径
--方法1:
RMAN>
run {
set newname for datafile '/u01/oradata/orcl/system01.dbf' to '/u01/oradata/testdb/system01.dbf';
restore...
switch datafile all;
}
--方法2:
RMAN>
run {
set newname for database to '/u01/oradata/testdb/';
restore...
switch database to copy;
}
--注意:已经反复强调过多次,在open resetlogs前都需要检查文件位置是否正确,尤其是redo文件的位置!这里需要将redo和temp文件都重定向到新的目录下:
alter database rename file '/u01/oradata/orcl/temp01.dbf' to '/u01/oradata/testdb/temp01.dbf';
alter database rename file '/u01/oradata/orcl/redo03.log' to '/u01/oradata/testdb/redo03.log';
alter database rename file '/u01/oradata/orcl/redo02.log' to '/u01/oradata/testdb/redo02.log';
alter database rename file '/u01/oradata/orcl/redo01.log' to '/u01/oradata/testdb/redo01.log';
c.如何验证数据是否是自己需要的
SQL> alter database open read only;
Check Your DATA...
SQL> shutdown abort;
SQL> startup mount;
SQL> alter database open resetlogs;
2.传输归档时,实现增量传输
之前传输归档时,当天内的文件是scp直接传输的,现在需要实现增量传输。
知识点:
- a.如何在Linux实现增量传输文件的功能
之前windows是通过 xcopy /y /d来实现这个需求的:
/y
禁止提示确认要覆盖已存在的目标文件。
/d [:MM-DD-YYYY]
只复制那些在指定日期或指定日期之后更改过的源文件。如果不包括“MM-DD-YYYY”值,“xcopy”会复制比现有“Destination”文件新的所有“Source”文件。该命令行选项使您可以更新更改过的文件。
实际测试将之前的scp复制部分替换成rsync --ignore-existing,之前的scp部分注释掉,添加rsync部分:
--全备脚本:
#echo "Begin scp at : `date`" >>${1}/backup_full.log
#scp ${1}/*`date +%Y%m%d`* [email protected]:/tmp/
#echo "End scp at : `date`" >>${1}/backup_full.log
echo "Begin rsync at : `date`" >>${1}/backup_full.log
rsync --ignore-existing ${1}/*`date +%Y%m%d`* [email protected]:/u01/orabak/
echo "End rsync at : `date`" >>${1}/backup_full.log
--归档备份脚本:
#echo "Begin scp at : `date`" >>${1}/backup_arch.log
#scp ${1}/*`date +%Y%m%d`*.{CTL,SPFILE,ARC} [email protected]:/tmp/
#echo "End scp at : `date`" >>${1}/backup_arch.log
echo "Begin rsync at : `date`" >>${1}/backup_arch.log
rsync --ignore-existing ${1}/*`date +%Y%m%d`*.{CTL,SPFILE,ARC} [email protected]:/u01/orabak/
echo "End rsync at : `date`" >>${1}/backup_arch.log
测试,定时任务调整为每个小时在整点时备份归档:
[ora11204@OEL-ASM orabak]$ crontab -l
0 1 * * * /u01/orabak/scripts/backup.sh /u01/orabak
0 * * * * /u01/orabak/scripts/backuparch.sh /u01/orabak
去异机192.168.1.61去观察已经传输过来的文件修改时间,确认每小时确实只复制传输那些新文件:
-rw-r----- 1 oracle oinstall 9.5M Jun 21 13:00 ORCL.79.1.20180621.CTL
-rw-r----- 1 oracle oinstall 96K Jun 21 13:00 ORCL.80.1.20180621.SPFILE
-rw-r----- 1 oracle oinstall 2.5K Jun 21 13:00 ORCL.82.1.20180621.ARC
-rw-r----- 1 oracle oinstall 15K Jun 21 13:00 ORCL.81.1.20180621.ARC
-rw-r----- 1 oracle oinstall 9.5M Jun 21 14:00 ORCL.83.1.20180621.CTL
-rw-r----- 1 oracle oinstall 96K Jun 21 14:00 ORCL.84.1.20180621.SPFILE
-rw-r----- 1 oracle oinstall 2.5K Jun 21 14:00 ORCL.86.1.20180621.ARC
-rw-r----- 1 oracle oinstall 1.2M Jun 21 14:00 ORCL.85.1.20180621.ARC
-rw-r----- 1 oracle oinstall 9.5M Jun 21 15:00 ORCL.87.1.20180621.CTL
-rw-r----- 1 oracle oinstall 96K Jun 21 15:00 ORCL.88.1.20180621.SPFILE
-rw-r----- 1 oracle oinstall 2.5K Jun 21 15:00 ORCL.90.1.20180621.ARC
-rw-r----- 1 oracle oinstall 1.2M Jun 21 15:00 ORCL.89.1.20180621.ARC
-rw-r----- 1 oracle oinstall 9.5M Jun 21 16:00 ORCL.91.1.20180621.CTL
-rw-r----- 1 oracle oinstall 96K Jun 21 16:00 ORCL.92.1.20180621.SPFILE
-rw-r----- 1 oracle oinstall 2.5K Jun 21 16:00 ORCL.94.1.20180621.ARC
-rw-r----- 1 oracle oinstall 1.2M Jun 21 16:00 ORCL.93.1.20180621.ARC
最终考虑到一些特殊情况,比如有些文件同步一半突然断电,所以更偏向于使用rsync -t -v
同步满足需求,这样也更加匹配之前windows的xcopy /y /d
方式。
至此,通过rsync命令实现了增量传输文件的需求。