RMAN oracle 11g

news/2024/10/21 3:35:25/

oracle 11g已经是个使用了很多年的版本了,很多单位都使用oracle 11g,而有些单位因为没人懂oracle,部署应用的厂家也因为服务终止停止了维护。导致一些重要的数据库连个基本的备份都没有。 

当遇到故障的时候,除了需要高额的数据恢复服务外,往往还会丢失部分数据。虽然出故障之后才开始重视起来,但是已经丢了的数据是找不回来的。

虽然救火就意味着报酬,但是数据是无价的,尤其是一些重要的行业。备份,是一种最廉价,但是最有效的保护数据安全的一种方式, 不用您去采购备份一体机或者存储镜像等昂贵的设备,只需要您预留一部分空间,执行rman定时备份或者数据泵备份,再通过ftp定时传输到异地的一台闲置的服务器上,可以保证您至少有一份数据在,如果归档日志也在的话,可以实现数据零丢失。

如果您恰好是某个机构的数据库管理员,正好当前没有什么备份手段,那么不妨了解一下,部署一个定时备份任务保证数据的完整。

--1.rman是什么 ?

Recovery Manager (RMAN)

--rman就是oracle官方推出的一种物理备份工具,以oracle block为读取单位。

--可以结合expdp使用做备份,可以实现更小粒度的备份。rman11g之前最小是表空间级别。12c之后可以恢复表。

--2.使用条件

--需要开启归档

archive log list;
shutdown immediate;
startup mount;
alter database archivelog;
alter system set log_archive_dest_1='location=/arp/oraarp/archive';
alter database open;
archive log list;
--如果使用快速恢复区的话一定要注意快速恢复区的大小

--3.rman常用命令

--3.1.登录命令

rman target / 
--登录本地用ORACLE_SID环境变量标识,用操作系统账户进行验证身份
rman target sys/oracle 
--登录本地,用数据库密码文件验证身份
rman target sys/oracle@orclz
--登录远程数据库,使用网络连接远程数据库
show all;
--列出rman配置,可以配置备份路径和控制文件是否自动备份以及备份路径

2. 配置控制文件自动备份
你可以使用 CONFIGURE CONTROLFILE AUTOBACKUP 命令来启用或禁用控制文件的自动备份。例如,要启用自动备份并将备份保存在与数据文件相同的目录中,你可以这样做:

 

sql复制代码

CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/backup/rman/controlfil

CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/u04/f4db_rmanbk/control/%F'

CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/u04/f4db_rmanbk/control/%d_%F'

CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/u04/f4db_rmanbk/control/ctl_%d_%t_%u.bak'

CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT   '/u04/f4db_rmanbk/%d_%t_%u.bak';

CONFIGURE SNAPSHOT CONTROLFILE NAME TO '/u04/f4db_rmanbk/dbs/snapcf_ORCL.f';

CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 5 DAYS;

--3.2.备份命令

--备份全库

/***备份全库会包含参数文件和控制文件***/

RMAN> backup database  format '/home/oracle/backup/rman/fulldb_%s_%p';Starting backup at 14-DEC-21
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=51 device type=DISK
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00001 name=/u01/app/oracle/oradata/orcl/system01.dbf
input datafile file number=00002 name=/u01/app/oracle/oradata/orcl/sysaux01.dbf
input datafile file number=00005 name=/u01/app/oracle/oradata/orcl/example01.dbf
input datafile file number=00003 name=/u01/app/oracle/oradata/orcl/undotbs01.dbf
input datafile file number=00006 name=/u01/app/oracle/oradata/orcl/ogg01.dbf
input datafile file number=00004 name=/u01/app/oracle/oradata/orcl/users01.dbf
channel ORA_DISK_1: starting piece 1 at 14-DEC-21
channel ORA_DISK_1: finished piece 1 at 14-DEC-21
piece handle=/home/oracle/backup/rman/fulldb_80_1 tag=TAG20211214T155842 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:15
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including current control file in backup set
including current SPFILE in backup set
channel ORA_DISK_1: starting piece 1 at 14-DEC-21
channel ORA_DISK_1: finished piece 1 at 14-DEC-21
piece handle=/home/oracle/backup/rman/fulldb_81_1 tag=TAG20211214T155842 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 14-DEC-21RMAN> 
RMAN> restore spfile to '/home/oracle/backup/pfile.ora';Starting restore at 14-DEC-21
using channel ORA_DISK_1channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: restoring SPFILE
output file name=/home/oracle/backup/pfile.ora
channel ORA_DISK_1: reading from backup piece /home/oracle/backup/rman/fulldb_81_1
channel ORA_DISK_1: piece handle=/home/oracle/backup/rman/fulldb_81_1 tag=TAG20211214T155842
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
Finished restore at 14-DEC-21RMAN> restore controlfile to '/home/oracle/backup/control01.ctl';Starting restore at 14-DEC-21
using channel ORA_DISK_1channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: restoring control file
output file name=/home/oracle/backup/control01.ctl
channel ORA_DISK_1: reading from backup piece /home/oracle/backup/rman/fulldb_81_1
channel ORA_DISK_1: piece handle=/home/oracle/backup/rman/fulldb_81_1 tag=TAG20211214T155842
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
Finished restore at 14-DEC-21RMAN> backup database  format 'fulldbcom_%s_%p' tag="full_data";
--压缩备份全库
backup archivelog all format '/home/oracle/backup/rman/archive_%s_%p';
--备份归档日志
backup as compressed backupset archivelog all delete input format '/Data/arch/arch1/archivelog_%d_%I_%s_%p_%T';
--压缩备份归档日志

--3.3.删除命令

delete backup;
--删除所有备份
delete noprompt backup; 
--不用确认删除所有备份,用在脚本里面,脚本里面没法输入YES
delete archivelog all;
--删除所有归档日志
delete noprompt archivelog all;
--不用确认删除所有归档日志
delete noprompt archivelog until time 'sysdate-7';
--删除7天前的归档
delete obsolete; 
--删除陈旧备份;
delete expired backup; 
--删除EXPIRED备份;
delete expired copy; 
删除EXPIRED副本;

--3.4.report命令

report schema;
--报告目标数据库的物理结构; 
report need backup days=3; 
--报告最近3天没有被备份的数据文件;
report need backup days 3 tablespace users; 
--在USERS表空间上3天未备份的数据 文件; 
report need backup incremental 3; 
--报告恢复数据文件需要的增量备份个数超过3次 的数据文件; 
report need backup redundancy 2 database; 
--报告备份文件低于2份的所有数据文 件;RMAN>report need backup redundancy=2; 
report need backup recovery window of 6 days; 
--报告文件报表的恢复需要超过6天 的归档日志的数据文件; 
report unrecoverable; 
--报告数据库所有不可恢复的数据文件; 
report obsolete redunndancy 2; 
--报告备份次数超过2次的陈旧备份; 
report obsolete; 
--报告多余的备份;

--3.5.list命令

list backup 列出详细备份;
list expired backup; 
--列出 过期备份; 
list backup of database; 
--列出所有数据文件的备份集;
list backup of tablespace user; 
--列出特定表空间的所有数据文件备份集;
list backup of controlfile; 
--列出控制制文件备份集; 
list backup of archivelog all; 
--列出归档日志备份集详细信息; 
list archivelog all; 
--列出归档日志备份集简要信息 
list backup of spfile; 
--列出SPFILE备份集; 
list copy of datafile; 
--列出数据文件映像副本; 
list copy of controlfile; 
--列出控制文件映像副本; 
list copy of archivelog all; 
--列出归档日志映像副本; 
list incarnation of database; 
--列出对应物/列出数据库副本; 
list backup summary; 
--概述可用的备份;

--3.6.crosscheck命令

crosscheck backup; 
--核对所有备份集;
crosscheck backup of database; 
--核对所有数据文件的备份集;
crosscheck archivelog all;
--核对所有归档;

4.全库备份脚本实战

cd /home/oracle/backup
vi rman_backup_full.sh#!/bin/bash
source ~/.bash_profile
export backup_date=`date +%Y%m%d`
export backup_path=/home/oracle/backup/rman
rman target / nocatalog << EOF1
run {
allocate channel c1 type disk;
allocate channel c2 type disk;
allocate channel c3 type disk;
allocate channel c4 type disk;
backup (database  format '$backup_path/${backup_date}_data_%s_%p' tag="full_data");
backup (archivelog all delete all input format '/$backup_path/${backup_date}_arc_%s_%p' tag="full_arc");
release channel c1;
release channel c2;
release channel c3;
release channel c4;
}
exit
EOF1--如果你的空间只能存放一份备份,比如空间只有50G,备份有30G,只够存放一份备份,那么可以在备份之前删除所有备份,然后执行备份。
#!/bin/bash
source ~/.bash_profile
export backup_date=`date +%Y%m%d`
export backup_path=/home/oracle/backup/rman
rman target / nocatalog << EOF1
delete noprompt backup;
run {
allocate channel c1 type disk;
allocate channel c2 type disk;
allocate channel c3 type disk;
allocate channel c4 type disk;
backup (database  format '$backup_path/${backup_date}_data_%s_%p' tag="full_data");
backup (archivelog all delete all input format '/$backup_path/${backup_date}_arc_%s_%p' tag="full_arc");
release channel c1;
release channel c2;
release channel c3;
release channel c4;
}
exit
EOF1--如果你的归档目录比较大,而且你有adg,需要应用归档日志,那么可以把归档保留一定的时间,而不是备份之后就删除--delete all input。
#!/bin/bash
source ~/.bash_profile
export backup_date=`date +%Y%m%d`
export backup_path=/home/oracle/backup/rman
rman target / nocatalog << EOF1
crosscheck backup;
crosscheck archivelog all;
delete noprompt archivelog until time 'sysdate-7';
delete noprompt expired backup;
delete noprompt expired backupset;
delete noprompt obsolete;
run {
allocate channel c1 type disk;
allocate channel c2 type disk;
allocate channel c3 type disk;
allocate channel c4 type disk;
backup (database  format '$backup_path/${backup_date}_data_%s_%p' tag="full_data");
backup (archivelog all  format '/$backup_path/${backup_date}_arc_%s_%p' tag="full_arc");
release channel c1;
release channel c2;
release channel c3;
release channel c4;
}
exit
EOF1[oracle@11g rman]$ crontab -l
/var/spool/cron/oracle: Permission denied
su - root
chmod u+s /usr/bin/crontab 
[root@11g ~]# chmod u+s /usr/bin/crontab 
[root@11g ~]#  ll /usr/bin/crontab 
-rwsrwxrwx. 1 root root 47520 Mar  4  2011 /usr/bin/crontab
[root@11g ~]# su - oracle
[oracle@11g ~]$ crontab -l
no crontab for oraclechown +x /home/oracle/backup/rman_backup_full.shcrontab -e
* 22 * * *  /home/oracle/backup/rman_backup_full.sh > /home/oracle/backup/rman_backup_full.log 

1、在主库进行全库备份,并传输到备库服务器上
[oracle@orcl1 ~]$ rman target /
run
{
allocate channel c1 type disk;
allocate channel c2 type disk;
allocate channel c3 type disk;
backup database format '/u01/rmanbak/full_%d_%T_%s_%p';
sql 'alter system archive log current';
sql 'alter system archive log current';
sql 'alter system archive log current';
backup archivelog all format '/u01/rmanbak/arch_%d_%T_%s_%p';
backup current controlfile for standby format '/u01/rmanbak/control_std.ctl'; --rman方法配置备库使用的控制文件
release channel c1;
release channel c2;
release channel c3;
}

2、备库恢复控制文件
--方法一:通过rman恢复控制文件
rman target /
restore standby controlfile from '/home/oracle/control_std.ctl'; --根据拷贝到备端控制文件路径名称修改
alter database mount;

3、进行rman恢复

--备库启动到mount状态进行恢复:
[oracle@orcl2 ~]$ rman target /
--注册备份集到控制文件
catalog start with '/u01/rmanbak/';
--备份集信息检查校验
list backup;
crosscheck backup; --进行检验,因为控制文件记录的有源库的信息,必须执行,否则 restore 会识别到生产库目录的备份文件
delete expired backup; --删除控制文件中记录的源库的备份集信息
crosscheck archivelog all;
delete expired archivelog all;
--进行还原
run
{
allocate channel c1 device type disk;
allocate channel c2 device type disk;
allocate channel c3 device type disk;
SET NEWNAME FOR DATAFILE '/u01/app/oracle/oradata/orcl/users01.dbf' to '/u01/app/oracle/oradata_std/orcl/users01.dbf';
SET NEWNAME FOR DATAFILE '/u01/app/oracle/oradata/orcl/undotbs01.dbf' to '/u01/app/oracle/oradata_std/orcl/undotbs01.dbf';
SET NEWNAME FOR DATAFILE '/u01/app/oracle/oradata/orcl/sysaux01.dbf' to '/u01/app/oracle/oradata_std/orcl/sysaux01.dbf';
SET NEWNAME FOR DATAFILE '/u01/app/oracle/oradata/orcl/system01.dbf' to '/u01/app/oracle/oradata_std/orcl/system01.dbf';
restore database;
switch datafile all;
release channel c1;
release channel c2;
release channel c3;
}
--进行恢复直到报错
recover database;

RMAN>backup archivelog sequencebetween50and120thread1deleteinput;

RMAN> backup archivelog from time"sysdate-15"until time"sysdate-7";

Rman>backup archivelog all;--备份所有归档日志

Rman>backup archivelog sequence 580 --备份580这个归档日志

Rman>backup archivelog sequence between 580 and 584;--备份580到584的归档日志

Rman>backup archivelog from sequence 580;--备份580开始的所有归档日志

Rman>backup archivelog from sequence 580 until sequence 584;--备份580到584的归档日志

Rman>restore archivelog all;

Rman>restore archivelog sequence 580;

Rman>restore archivelog sequence between 580 and 584;

Rman>restore archivelog from sequence 580;

Rman> restore archivelog from sequence 580 until sequence 584;

standby在standby状态下的备份方式如下,生成一个控制文件备份control_1,一个数据文件备份full_data
RMAN> backup database format '/iso/backup/XX_%U';
 
 
primary的备份方式如下,并拷贝到standby的对应目录
RMAN> backup current controlfile format '/iso/backup/control_2';
RMAN> backup current controlfile for standby format '/iso/backup/control_3';
 
得出结论:
主库backup current controlfile带上了for standby的话,使用该控制文件备份,无论怎么样还原都是standby状态
主库backup current controlfile没有带for standby的话
restore standby controlfile后是standby状态
restore controlfile后是primary状态
备库backup current controlfile带不带for standby,使用该控制文件备份,无论怎么样还原都是standby状态
 
 
恢复方法(使用control_1,备库控制文件正常备份方式),备库还是备库状态
1.备库关机并启动至startup nomount状态
2.备库restore controlfile from '/iso/backup/control_1';
3.备库restore database
4.备库recover database until sequence XX
5.备库alter database open resetlogs
ORA-01666: control file is for a standby database
6.alter database open;
7.alter database recover managed standby database disconnect from session;
 
 
恢复方法(使用control_2,主库控制文件正常备份方式),备库脱离备库成主库状态
1.备库关机并启动至startup nomount状态
2.备库restore controlfile from '/iso/backup/control_2';
3.备库restore database(先做catalog backuppiece full_data的前提下 )
4.备库recover database until sequence XX
5.备库alter database open resetlogs
 
 
恢复方法(使用control_2,主库控制文件正常备份方式,但是恢复时使用了standby),备库还是备库状态
1.备库关机并启动至startup nomount状态
2.备库restore standby controlfile from '/iso/backup/control_2';
3.备库restore database(先做catalog backuppiece full_data的前提下 )
4.备库recover database until sequence XX
5.备库alter database open resetlogs
ORA-01666: control file is for a standby database
6.alter database open;
7.alter database recover managed standby database disconnect from session;
 
 
恢复方法(使用control_3,主库控制文件standby备份方式),备库还是备库状态
1.备库关机并启动至startup nomount状态
2.备库restore controlfile from '/iso/backup/control_3'
3.备库restore database(先做catalog backuppiece full_data的前提下 )
4.备库recover database until sequence XX
5.备库alter database open resetlogs
ORA-01666: control file is for a standby database
6.alter database open;
7.alter database recover managed standby database disconnect from session;

内容仅限于自己学习的历程,如有不对的地方,请不吝赐教!如有疑问,也欢迎一起讨论! 非常感谢!


http://www.ppmy.cn/news/1426863.html

相关文章

设备连接IoT云平台指南

一、简介 设备与IoT云间的通讯协议包含了MQTT&#xff0c;LwM2M/CoAP&#xff0c;HTTP/HTTP2&#xff0c;Modbus&#xff0c;OPC-UA&#xff0c;OPC-DA。而我们设备端与云端通讯主要用的协议是MQTT。那么设备端与IoT云间是如何创建通信的呢&#xff1f;以连接华为云IoT平台为例…

ARM_day7:实现三个按键中断

程序代码&#xff1a; mykey.h: #ifndef __MYKEY_H__ #define __MYKEY_H__ #include "stm32mp1xx_rcc.h" #include "stm32mp1xx_gpio.h" #include "stm32mp1xx_exti.h" #include "stm32mp1xx_gic.h" extern void printf(const char …

【全开源】多功能完美运营版商城 虚拟商品全功能商城 全能商城小程序 智慧商城系统 全品类百货商城

内容目录 一、详细介绍二、效果展示1.部分代码2.效果图展示 三、学习资料下载 一、详细介绍 完美运营版商城/拼团/团购/秒杀/积分/砍价/实物商品/虚拟商品等全功能商城 干干净净 没有一丝多余收据 还没过手其他站 还没乱七八走的广告和后门 后台可以自由拖曳修改前端UI页面 …

短视频流媒体平台的系统设计

1. 功能需求: 我们的系统有两类参与者 内容创作者 •上传任何类型的视频&#xff08;格式编解码器&#xff09;•视频可以被删除•视频元数据•必填项: 标题&#xff0c;作者&#xff0c;描述•选填项: 分类/标签列表•可以随时更新•当视频对观众可用时&#xff0c;向内容创作…

socket编程——tcp

在我这篇博客&#xff1a;网络——socket编程中介绍了关于socket编程的一些必要的知识&#xff0c;以及介绍了使用套接字在udp协议下如何通信&#xff0c;这篇博客中&#xff0c;我将会介绍如何使用套接字以及tcp协议进行网络通信。 1. 前置准备 在进行编写代码之前&#xff…

呼市经开区建设服务项目水、电能耗监测 数采案例

一、项目背景及需求 项目地点位于内蒙古呼和浩特市&#xff0c;呼市数字经开区建设服务项目。属于企业用能数据采集、能耗监测板块子项目。 针对水、电能耗数据采集&#xff0c;结合现场客观因素制约&#xff0c;数据采集方面存在较大难度。大多数国网电表485接口由于封签限制…

为硬刚小米SU7,华为智界S7整出了「梅开二度」操作

如今国产中大型新能源轿车市场&#xff0c;在小米 SU7 加入后&#xff0c;可算彻底活了过来。 过去几年&#xff0c;咱们自主新能源品牌在 20-30 万元级轿车上发力明显不足&#xff0c;老牌车厂比亚迪汉几乎以一己之力扛起销量担当。 随着新能源汽车消费升级、竞争加剧&#x…

Linux中进程和计划任务管理(2)

一.进程命令 1.lsof lsof 命令&#xff0c;“list opened files”的缩写&#xff0c;直译过来&#xff0c;就是列举系统中已经被打开的文件。通过 lsof 命令&#xff0c;我们就可以根据文件找到对应的进程信息&#xff0c;也可以根据进程信息找到进程打开的文件。 格式&…