Oracle11g Rman备份备忘录

0x00  rman是什么

Recovery Manager (RMAN) rman就是oracle官方推出的一种物理备份工具,以oracle block为读取单位

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

0x01 使用条件

需要开启归档

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;
#如果使用快速恢复区的话一定要注意快速恢复区的大小

0x02 rman常用基础命令

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

0x03 备份命令

#备份全库会备份包含参数文件和数据文件
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-21

RMAN> 
RMAN> restore spfile to '/home/oracle/backup/pfile.ora';

Starting restore at 14-DEC-21
using channel ORA_DISK_1

channel 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-21

RMAN> restore controlfile to '/home/oracle/backup/control01.ctl';

Starting restore at 14-DEC-21
using channel ORA_DISK_1

channel 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-21

RMAN> 
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';
#压缩备份归档日志

0x04 删除命令

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副本;

0x05 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; 
#报告多余的备份;

0x06 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; 
#概述可用的备份;

0x07 crosscheck命令

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

0x08 全库备份脚本

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 oracle

chown +x /home/oracle/backup/rman_backup_full.sh

crontab -e
* 22 * * *  /home/oracle/backup/rman_backup_full.sh > /home/oracle/backup/rman_backup_full.log

 

posted @ 2023-01-06 21:09  BlackData  阅读(165)  评论(0编辑  收藏  举报