一、Oracle数据库开启归档日志
Oracle恢复演练,由于备份需要,需要开启归档日志。
1.1归档日志
归档日志(Archive Log)是非活动的重做日志备份.通过使用归档日志,可以保留所有重做历史记录,当数据库处于ARCHIVELOG模式并进行日志切换式,后台进程ARCH会将重做日志的内容保存到归档日志中.当数据库出现介质失败时,使用数据文件备份,归档日志和重做日志可以完全恢复数据库。
1.2ARCHIVELOG模式
当ORACLE数据库运行在ARCHIVELOG模式时,所有的事务重做日志都将保存.这意味着对数据库进行的所有事务都留有一个备份,尽管重做日志以循环方式工作,但在一个重做日志被覆盖前均将为其建立一个副本.在重做日志文件复制完成之前,ORACLE数据库将停止一切新的操作,在旧的事务记录完成之前ORACLE不对其进行覆盖.有了所有事务的副本,数据库就可以从所有类型的失败中恢复,包括用户错误或磁盘崩溃.这是一种最安全的数据库工作方式。
1.3启动归档日志
1.登录sysdba账户,本例子中的Oracle是绑定的服务器,直接在对应服务器上用下面的命令登录:
sqlplus / as sysdba
2.查看归档信息
SQL> archive log list
//输出结果
Database log mode No Archive Mode
Automatic archival Disabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 241
Current log sequence 255
No Archive Mode表示当前数据库未开启归档模式
3. 设置归档目录,该修改重启后生效
oracle默认使用闪回恢复区DB_RECOVERY_FILE_DEST作为归档路径,但是闪回恢复区在安装过程中有限制其大小,如果闪回恢复区满了,归档日志会因为无法写入而导致数据库阻塞。解决办法是可以通过增加闪回区大小或者修改归档日志的路径。修改log_archive_dest_1参数即可修改归档日志路径(pfile/spfile中参数db_recovery_file_dest指定的目录将无效),从Oracle 10g开始,可以生成多份一样的日志,保存多个位置,加强安全性,多个位置通过设置不同的log_archive_dest_{n}参数实现,archive log list输出中的Archive destination路径只显示最新修改的路径。
SQL> show parameter log_archive_dest_1
//输出结果
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
log_archive_dest_1 string
SQL> alter system set log_archive_dest_1='location=/oradata/archivelog' scope=spfile;
//输出结果
System altered.
4. 修改归档日志格式,该修改重启后生效
SQL> alter system set log_archive_format='ARC%S_%R.%T_%D.dbf' scope=spfile;
//输出结果
System altered.
5. 开启归档日志
需要在mount状态下开始数据库归档,重启至mount
SQL> shutdown immediate
//输出结果
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.
Total System Global Area 1603411968 bytes
Fixed Size 2213776 bytes
Variable Size 989857904 bytes
Database Buffers 603979776 bytes
Redo Buffers 7360512 bytes
Database mounted.
开启数据库归档:
SQL> alter database archivelog;
//输出结果
Database altered.
查看归档结果:
SQL> archive log list
//输出结果
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /u01/app/oracle/archive_log
Oldest online log sequence 244
Next log sequence to archive 246
Current log sequence 246
开启强制归档:
ALTER DATABASE FORCE LOGGING;
打开数据库:
SQL> alter database open;
Database altered.
6.确认数据库为归档模式
SQL> select log_mode from v$database;
//输出结果
LOG_MODE
------------
ARCHIVELOG
SQL> select archiver from v$instance;
ARCHIVE
-------
STARTED
7.强制日志切换,确认是否开启归档成功
先查看当前归档日志视图和归档日志路径:
SQL> select name from v$archived_log;
//输出结果
no rows selected
[oracle@zml-rhel6 archive_log]$ ll
//输出结果
total 0
如结果所示,没有归档日志,下面执行强制日志切换:
SQL> alter system switch logfile;
//输出结果
System altered.
再次查看视图
SQL> select name from v$archived_log;
//输出结果
NAME
--------------------------------------------------------------------------------
/oradata/archivelog/ARC0000002429_0993570162.0001_e7cda172.dbf
有归档日志产生,验证归档日志开启成功
1.4归档日志的清理
归档日志非常占用物理空间,应定时清理,归档日志的清理分为两部分:
1.物理删除归档日志
该方式是利用linux的脚本和crontab定时任务来查找n天前的日志进行删除,首先编写日志删除脚本:
[oracle@i-F44CBDC1 shell]$ vim deleteArchiveLog.sh
//输入如下指令,意为删除5天前的所有文件
find /oradata/archivelog -mtime +5 -name "*.*" -exec rm -Rf {} ;
保存脚本后,根据实际情况赋予权限或组别,然后操作crontab,本例子均在oracle用户下创建定时任务:
[root@i-F44CBDC1 ~]# crontab -e
//输入如下指令,意为每天0点35分执行删除归档日志脚本,并将脚本的执行记录输出到log文件中
35 0 * * * sh /usr/local/shell/deleteArchiveLog.sh >>/var/local/deletearchivelog.log
保存后,定时任务会到时间执行脚本。
2. 虽然物理删除了归档日志文件,但是控制文件中依然会有这些日志的记录,下面需要在控制文件中清理这些归档日志,利用RMAN进行清理。
//rman登录
rman target/
查看过期日志:
RMAN> list expired archivelog all;
//输出结果
specification does not match any archived log in the repository
信息中显示没有过期日志,这是因为需要先检查归档日志的有效性,再进行删除:
RMAN> crosscheck archivelog all;
//输出结果
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=70 device type=DISK
validation succeeded for archived log
archived log file name=/oradata/archivelog/ARC0000002448_0993570162.0001_e7cda172.dbf RECID=415 STAMP=1026952512
validation succeeded for archived log
archived log file name=/oradata/archivelog/ARC0000002449_0993570162.0001_e7cda172.dbf RECID=416 STAMP=1026952520
validation succeeded for archived log
archived log file name=/oradata/archivelog/ARC0000002450_0993570162.0001_e7cda172.dbf RECID=417 STAMP=1026952542
validation succeeded for archived log
再次查看过期的归档日志,rm删除的文件便被标记为了过期日志:
RMAN> list expired archivelog all;
//输出结果
List of Archived Log Copies for database with db_unique_name ORCLWHS
=====================================================================
Key Thrd Seq S Low Time
------- ---- ------- - ---------
1 606 X 04-JUN-18
Name: /oradata/archivelog/ARC0000002609_0993570162.0001_e7cda172.dbf
1 607 X 04-JUN-18
Name: /oradata/archivelog/ARC0000002610_0993570162.0001_e7cda172.dbf
1 608 X 04-JUN-18
Name: /oradata/archivelog/ARC0000002611_0993570162.0001_e7cda172.dbf
删除过期的归档日志:
RMAN> delete expired archivelog all;
//输出结果
released channel: ORA_DISK_1
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=446 device type=DISK
List of Archived Log Copies for database with db_unique_name ORCLWHS
=====================================================================
Key Thrd Seq S Low Time
------- ---- ------- - ---------
1 606 X 04-JUN-18
Name: /oradata/archivelog/ARC0000002609_0993570162.0001_e7cda172.dbf
1 607 X 04-JUN-18
Name: /oradata/archivelog/ARC0000002610_0993570162.0001_e7cda172.dbf
1 608 X 04-JUN-18
Name: /oradata/archivelog/ARC0000002611_0993570162.0001_e7cda172.dbf
//询问是否要删除上述文件,输入YES
Do you really want to delete the above objects (enter YES or NO)? yes
deleted archived log
archived log file name=/oradata/archivelog/ARC0000002609_0993570162.0001_e7cda172.dbf RECID=1 STAMP=977949943
deleted archived log
archived log file name=/oradata/archivelog/ARC0000002610_0993570162.0001_e7cda172.dbf RECID=2 STAMP=977954446
deleted archived log
archived log file name=/oradata/archivelog/ARC0000002611_0993570162.0001_e7cda172.dbf RECID=3 STAMP=977954551
Deleted 3 EXPIRED objects
再次查询过期的归档日志:
RMAN> list expired archivelog all;
//输出结果
specification does not match any archived log in the repository
我们可以将上述流程封装为shell脚本,用定时任务执行,首先创建脚本:
[oracle@i-F44CBDC1 shell]$ vim clearArchiveLog.sh
//输入如下内容
PATH=$PATH:$HOME/bin
export PATH
export NLS_LANG=AMERICAN_AMERICA.zhs16gbk
export ORACLE_SID=zjfwjyptdb
export ORACLE_BASE=/oracle/app
export ORACLE_HOME=/oracle/app/database/ora11g
export SQLPATH=/home/oracle/scripts
export ORA_NLS33=$ORACLE_HOME/ocommon/nls/admin/data
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib:/usr/local/lib:
export PATH=$ORACLE_HOME/bin:/usr/bin:/usr/ccs/bin:/etc:/bin:/usr/openwin/bin:
/usr/local/bin:/usr/X11R6/bin:/oracle/crs/bin:$PATH
umask 022
$ORACLE_HOME/bin/rman target / log=/home/oracle/del_arch$(date +%Y-%m-%d).log <<EOF
crosscheck archivelog all;
delete noprompt expired archivelog all;
delete noprompt archivelog all completed before 'sysdate - 5';
exit;
EOF
保存脚本,然后创建定时任务:
[oracle@i-F44CBDC1 shell]$ crontab -e
//输入如下内容,意为0点40执行clearArchiveLog.sh,并将执行信息输出到archivelogrmanlog.log文件中
40 0 * * * sh /usr/local/shell/clearArchiveLog.sh >>/var/local/archivelogrmanlog.log
保存退出,这样就可以定时清理控制文件中的归档日志记录了。
参考文章:https://www.cnblogs.com/ZeroTiny/p/9135247.html
二、rman查看备份情况操作命令
查看备份集:
RMAN> list backupset;
List of Backup Sets
===================
查看SCHEMA统计信息:
RMAN> report schema;
Report of database schema for database with db_unique_name ORCL
List of Permanent Datafiles
===========================
查看表空间的备份情况:
RMAN> list backup of tablespace users;
List of Backup Sets
===================
查看数据文件的备份情况:
RMAN> list backup of datafile 1;
List of Backup Sets
===================
查看控制文件的备份情况:
RMAN> list backup of controlfile;
List of Backup Sets
===================
查看设备类型为“Disk”的备份:(## 查看磁带上的备份可以使用“list device type sbt backup;”命令)
RMAN> list device type disk backup;
List of Backup Sets
===================
查看归档日志及其备份情况:
注:## 查看归档日志的命令为:list archivelog all; ## 查看未备份的备份集;
## 查看归档日志备份集的命令为:list backup of archivelog all; ## 查看已备份的备份集;
RMAN> list archivelog all;
List of Archived Log Copies for database with db_unique_name ORCL
=====================================================================
查看系统中不存在(或者说是不在指定路径下)的备份:
RMAN> list expired backup;
specification does not match any backup in the repository
注:
1.以上命令## 查看的是不存在的备份,如备份集用操作系统命令删除之后,crosscheck之后该备份集就为expired(X)状态。
2.跟obsolete概念区分,该参数是针对不满足retention policy条件(可以按照存放份数,也可以按照存放日期)的备份。
查看过期的备份:
RMAN> report obsolete;
查看几天前的schema状态
RMAN> report schema at time 'SYSDATE-1';
查看需要备份的文件:
RMAN> report need backup;
查看违反规则的备份:
RMAN> report obsolete;
RMAN retention policy will be applied to the command
RMAN retention policy is set to redundancy 1
no obsolete backups found
参考:http://blog.chinaunix.net/uid-20802110-id-3849567.html