级别:0,1,2,3,4
企业一般设成2以下
0级:相当于全备
1级:在0级的基础上哪些数据块发生变化,备份哪些
2级:在1级的基础上哪些数据块发生变化,备份哪些
增量备份
backup incremental level = 0 database
backup incremental level 0 database
增量备份归档日志单路删除
备份完归档再删除原数据库单路归档
增量备份-归档日志多路删除
备份完归档再删除原数据库多路归档
增量备份是针对数据文件的
#累计增量备份的语法:
RMAN>backup incremetal level 2 cumulative database;
进行增量备份的前提
每当数据块发生变化,会在文件里注册,下次备份时会从此文件获取消息
#启用块更改跟踪
SQL> alter database enable block change tracking using file '/dat/DBData/orcl/orcl_block_change.fil';
Database altered.
可以查看视图v$block_change_tracking来确定是否启用了块更改跟踪
这个文件的大小一般不需要太大,它和数据库的大小有关,一般为数据库的1/30000.这个文件的增长是以10M的大小增长的
#RMAN下备份
开启块跟踪
backup incremental level 1 database
backup incremental level 0 database
#sqlplus下模拟灾难
模拟灾难场景--删除一个数据文件
启动报错
#RMAN下恢复
restore database; #数据库自动判断是否启用增量备份
recover database;
alter database open;
#RMAN下模拟灾难2--日志,参数文件完好无损,控制文件和数据文件丢失
alter system switch logfile;
exit
rman target sys/123456
shutdown immediate
exit
rm -rf *.ctl #删掉所有路径下的控制文件
rm -rf 1.dbf
rm -rf 2.df
#还原控制文件
rman target sys/123456
restore controlfile from '备份片路径';
#控制文件备份放到闪回恢复区,可以用restore controlfile from autobackup;
#控制文件放到人为指定的地方,则用restore controlfile from '备份片路径';
alter database mount;
restore datafile;
recover database;
alter database open resetlogs;
马上做一次数据库全备
会从闪回拿很旧的控制文件回来,恢复起来非常复杂
因此↓需要格外注意
restore controlfile from autobackup;
restore controlfile from ‘备份片路径’;;
分区坏了,不能把控制文件还原到原来分区上
control,spfile都支持把备份拿到新分区
restore controlfile to ‘/’ from ‘备份片’
数据库全备
操作系统命令创建一个文件夹ttt
把数据文件1.dbf移到ttt
删除库路径下的数据文件1.dbf
mount状态下执行以下run代码块
run{
set newname for datafile '/dat/DBData/SHIDA/shi01.dbf' to '/dat/DBData/SHIDA/SHIMING/shi01.dbf';
set newname for datafile '/dat/DBData/SHIDA/ming01.dbf' to '/dat/DBData/SHIDA/SHIMING/ming01.dbf';
restore database;
switch datafile all; #让数据库识别数据文件的新分区
recover database;
alter database open;
}
#set newname for datafile '/dat/DBData/SHIDA/shi01.dbf' 旧分区 to '/dat/DBData/SHIDA/SHIMING/shi01.dbf' 新分区
建立恢复点,执行不完全恢复RMAN>run{
Set until time “to_date(‘14/03/14 15:00:00’,’dd/mm/yy hh24:mi:ss’)”;
Restore database;
Recover database;
alter database open resetlogs;
}
前提:RMAN备份
shutdown immediate;
startup mount;
把数据库恢复到创建表之前
实行run代码块
补充:
不用润代码块的情况
补充:
基于scn的恢复
基于scn的不完全恢复Startup mount;
Restore database until scn 2257708;
Recover database until scn 2257708;
Alter database open resetlogs;
1.创建表空间
create tablespace lian datafile ‘/dat/DBData/orcl/lian.dbf’ size 50m;
2.创建用户
create user lian identified by lian default tablespace lian;
grant connect,resource to lian;
conn lian/lian;
3.创建表
create table lian(id number,name varchar2(10));
4.插入数据
insert into lian values (2,‘dalian’);
commit;
conn sys/123456;
alter system flush buffer_cache;
exit
5.RMAN备份数据库
backup database;
6.破坏数据文件
vi
7查询后发现坏块
7.1RMAN检查坏块
validate check logical database
显示数据头部损坏
7.1.1断电后进行介质恢复
rm -rf 损坏的数据文件
RMAN mount状态下还原数据文件 :restore database
alter database open;
7.2块级别的恢复:
blockrecover datafile 12 block 130;
RMAN> blockrecover datafile 12 block 130;
Starting recover at 15-MAR-20
using channel ORA_DISK_1
channel ORA_DISK_1: restoring block(s)
channel ORA_DISK_1: specifying block(s) to restore from backup set
restoring blocks of datafile 00012
channel ORA_DISK_1: reading from backup piece /home/oracle/rmanbk/db0_ORCL_20200315_54
channel ORA_DISK_1: piece handle=/home/oracle/rmanbk/db0_ORCL_20200315_54 tag=DB0
channel ORA_DISK_1: restored block(s) from backup piece 1
channel ORA_DISK_1: block restore complete, elapsed time: 00:00:01
starting media recovery
media recovery complete, elapsed time: 00:00:03
Finished recover at 15-MAR-20
库路径下创建脚本
touch backuplevel0.sh
touch backuplevel1.sh
赋予文件可执行权限(root)
chmod 777 backuplevel0.sh
chmod 777 backuplevel1.sh
编辑脚本
vi backuplevel0.sh
vi backuplevel1.sh
1级增量备份的脚本:
#!/bin/bash
# script.:bakuplevel0.sh
# creater:liu shiming
# date:2020/3/15
# desc:backup level0 database datafile in archive with rman
# connect database
export ORACLE_BASE=/dat/DBSoftware/oracle
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/dbhome_1
export ORACLE_SID=SHIDA
export PATH=$ORACLE_HOME/bin:$PATH
rman target sys/shida << EOF_RMAN
run{
allocate channel c1 type disk;
backup incremental level 1 tag 'db1' format '/home/oracle/rmanbk/db1_%d_%T_%s' database include current controlfile;
sql'alter system archive log current';
backup filesperset 3 format '/home/oracle/rmanbk/arch1_%d_%T_%s' archivelog all delete input;
release channel c1;
}
# end
------
0级增量备份的脚本:
#!/bin/bash
# script.:bakuplevel0.sh
# creater:lilianhong
# date:2020/3/15
# desc:backup level0 database datafile in archive with rman
# connect database
export ORACLE_BASE=/dat/DBSoftware/oracle
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/dbhome_1
export ORACLE_SID=orcl
export PATH=$ORACLE_HOME/bin:$PATH
rman target sys/123456 << EOF_RMAN
run{
allocate channel c1 type disk;
backup incremental level 1 tag 'db0' format '/home/oracle/rmanbk/db0_%d_%T_%s' database include current controlfile;
sql'alter system archive log current';
backup filesperset 3 format '/dat/bak/arch0_%d_%T_%s' archivelog all delete input; # 每三个归档日志作为一个备份片备份
release channel c1;
}
# end
执行脚本进行测试
./backuplevel0.sh
./backuplevel1.sh
sqlplus下创建job:
begin
dbms_scheduler.create_job(
job_name=>'shida_job00', #自定义,脚本名
job_type=>'executable',
job_action=>'/dat/DBData/orcl/backuplevel0.sh', #脚本路径
repeat_interval=>'freq=weekly;byday=SUN,WED;byhour=15;byminute=04');
end;
/
启用job:
SQL> exec dbms_scheduler.enable(‘shida_job00’);
补充:
运行job
SQL> exec dbms_scheduler.run_job(‘shida_job00’);
使job失效:
SQL> exec dbms_scheduler.disable(‘shida_job0’);
删除job
SQL> exec dbms_scheduler.drop_job(‘shida_job0’);
与job相关的数据字典:
DBA_SCHEDULER_JOBS;
查看备份路径是否备份成功
cd /home/oracle/rmanbk/
ll
如何进行增量备份
模拟日志文件完好无损,丢失所有的控制文件及一个或多个数据文件
在不同的路径下还原数据库
基于时间点/SCN/sequence不完全恢复
基于坏块的恢复
配置自动增量备份
1.RMAN会检测和报告损坏的数据块
2.不需要将表空间置于热备份模式
3.RMAN会自动跟踪新的数据文件和表空间
4.RMAN能备份使用过的数据块
5.RMAN提供备份映像的实际压缩
6.RMAN支持增量备份
7.RMAN提供自动且方便的备份和恢复操作
8.可以使用RMAN来测试备份而不需要实际还原它们
列出可用备份;
RMAN>list backup summary;
按照备份类型列出备份
list backup by file;
列出常用的备份信息
list backup;
列出恢复的备份
list recoverable backup
列出过期的备份
list expired backup;
验证归档日志
crosscheck archivelog all;
delete expired archivelog all;
存在于磁盘介质中,但是过期了
crosscheck backup;
delete expired backup;
找出不存在于磁盘介质中,控制文件中还有记录的备份文件
report obsolete;
补充
备份失败
将不存在的归档日志记录从control file中清楚掉,否则备份数据库时,备份会失败
crosscheck archivelog all;
delete expired archivelog all;
报告数据库模式
report schema
能闪回到过去多长时间
show parameter db_flashback_retention_target;
默认分钟1440,24小时
确认闪回是否开启
select FLASHBACK_ON from v$database;
开启闪回
mount
alter database flashback on;
alter database open;
※ 开启后
闪回恢复区下多一个闪回日志,
该日志受参数db_flashback_retention_target;制约;
闪回数据库语法:flashback database to scn 1227661;
闪回表语法:SQL> flashback table mytest2 to scn 1404479;
事先要启动行移动:SQL> alter table mytest2 enable row movement;
应用场景:
一个人删除数据,不确定删没删错
想确认删除之前的表里的数据
select * from mytest as of scn 128726;
create table d as select * from mytest as of scn 128726;