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配置,可以配置备份路径和控制文件是否自动备份以及备份路径
- 配置控制文件自动备份 :
你可以使用 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-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';
--压缩备份归档日志
--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 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
内容仅限于自己学习的历程,如有不对的地方,请不吝赐教!如有疑问,也欢迎一起讨论! 非常感谢!