您的位置:首页 > 数据库 > Oracle

Linux环境Oracle数据库 RMAN备份

2017-07-27 22:43 465 查看
1 创建备份文件夹

[root@dbtest /]# cd /

[root@dbtest /]# ls

bin dev lib media net root srv tmp var

boot etc lib64 misc opt sbin sys u01

date3.txt home lost+found mnt proc selinux tftpboot usr

[root@dbtest /]# mkdir backup

[root@dbtest /]# ls

backup date3.txt home lost+found mnt proc selinux tftpboot usr

bin dev lib media net root srv tmp var

boot etc lib64 misc opt sbin sys u01

[root@dbtest /]#

[root@dbtest backup]# ls

[root@dbtest backup]# mkdir controlfileback

[root@dbtest backup]# mkdir log

[root@dbtest backup]# mkdir logback

[root@dbtest backup]# mkdir dbback

[root@dbtest backup]# ls

controlfileback dbback log logback

backup 存放备份文件的根目录

controlfileback 存放备份的控制文件

dbback 存放备份的数据文件

log 存放备份生成的日志,坚持该日志查看备份是否成功

logback 存放归档日志文件

2 赋权限给ORACLE用户

[root@dbtest backup]# chown -R oracle:oinstall /backup

[root@dbtest backup]# ll

总计 32

drwxr-xr-x 2 oracle oinstall 4096 07-27 20:40 controlfileback

drwxr-xr-x 2 oracle oinstall 4096 07-27 20:41 dbback

drwxr-xr-x 2 oracle oinstall 4096 07-27 20:41 log

drwxr-xr-x 2 oracle oinstall 4096 07-27 20:41 logback

3 查看日志是否归档

[root@dbtest backup]# su - oracle

[oracle@dbtest ~]$ sqlplus /nolog

SQL*Plus: Release 11.2.0.1.0 Production on Thu Jul 27 20:57:50 2017

Copyright (c) 1982, 2009, Oracle. All rights reserved.

SQL> conn / as sysdba

Connected.

SQL> s^H

SP2-0042: unknown command “” - rest of line ignored.

SQL> archive log list;

Database log mode Archive Mode

Automatic archival Enabled

Archive destination /u01/app/oracle/archdata

Oldest online log sequence 19

Next log sequence to archive 21

Current log sequence 21

SQL> exit;

#关闭数据库
shutdown immediate
#开启归档
startup mount
#开启归档模式


alter databasearchivelog;

打开数据库

alter databaseopen;

4 查看RMAN配置

[oracle@dbtest ~]$ rman target /

Recovery Manager: Release 11.2.0.1.0 - Production on Thu Jul 27 21:00:22 2017

Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.

connected to target database: ORCL (DBID=1422785319)

RMAN> show all;

using target database control file instead of recovery catalog

#使用控制文件来代替恢复目录数据库存放rman信息


RMAN configuration parameters for database with db_unique_name ORCL are:

CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;

CONFIGURE BACKUP OPTIMIZATION OFF; # default

CONFIGURE DEFAULT DEVICE TYPE TO DISK; # default

CONFIGURE CONTROLFILE AUTOBACKUP ON;

CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO ‘/u01/app/oracle/control/cf_%F’;

CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default

CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default

CONFIGURE ARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default

CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT ‘/u01/app/oracle/DBbackup/DB_%U’;

CONFIGURE MAXSETSIZE TO UNLIMITED; # default

CONFIGURE ENCRYPTION FOR DATABASE OFF; # default

CONFIGURE ENCRYPTION ALGORITHM ‘AES128’; # default

CONFIGURE COMPRESSION ALGORITHM ‘BASIC’ AS OF RELEASE ‘DEFAULT’ OPTIMIZE FOR LOAD TRUE ; # default

CONFIGURE ARCHIVELOG DELETION POLICY TO NONE; # default

CONFIGURE SNAPSHOT CONTROLFILE NAME TO ‘/u01/app/oracle/product/11.2.0/db_1/dbs/snapcf_orcl.f’; # default

5 更改配置

RMAN>

#配置RMAN默认备份介质保存目录 /backup/dbback/


configure channel device type disk format'/backup/dbback/DB_%U';
MAN> configure channel device type disk format'/backup/dbback/DB_%U';

old RMAN configuration parameters:
CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT   '/opt/oracle/backup/DB_%U';
new RMAN configuration parameters:
CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT   '/backup/dbback/DB_%U';
new RMAN configuration parameters are successfully stored
#新RMAN配置参数生效
RMAN>
#配置控制文件自动备份并保存到 /backup/controlfileback/
configure controlfile autobackup on;
#当控制文件内容有变化时会自动触发备份
RMAN> configure controlfile autobackup on;

old RMAN configuration parameters:
CONFIGURE CONTROLFILE AUTOBACKUP ON;
new RMAN configuration parameters:
CONFIGURE CONTROLFILE AUTOBACKUP ON;
new RMAN configuration parameters are successfully stored

RMAN>
#配置控制文件自动备份保存目录和格式
configure controlfile autobackup format for device type disk to '/backup/controlfileback/cf_%F'
RMAN> configure controlfile autobackup format for device type disk to '/backup/controlfileback/cf_%F';

old RMAN configuration parameters:
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/u01/app/oracle/control/cf_%F';
new RMAN configuration parameters:
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/backup/controlfileback/cf_%F';
new RMAN configuration parameters are successfully stored

RMAN>
#配置备份介质保留期为30天
configure retention policy to recovery window of 30 days;
RMAN> configure retention policy to recovery window of 30 days;

old RMAN configuration parameters:
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;
new RMAN configuration parameters:
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 30 DAYS;
new RMAN configuration parameters are successfully stored

RMAN>
RMAN> show all;

RMAN configuration parameters for database with db_unique_name ORCL are:
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 30 DAYS;
CONFIGURE BACKUP OPTIMIZATION OFF; # default
CONFIGURE DEFAULT DEVICE TYPE TO DISK; # default
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/backup/controlfileback/cf_%F';
CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default
CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE ARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT   '/backup/dbback/DB_%U';
CONFIGURE MAXSETSIZE TO UNLIMITED; # default
CONFIGURE ENCRYPTION FOR DATABASE OFF; # default
CONFIGURE ENCRYPTION ALGORITHM 'AES128'; # default
CONFIGURE COMPRESSION ALGORITHM 'BASIC' AS OF RELEASE 'DEFAULT' OPTIMIZE FOR LOAD TRUE ; # default
CONFIGURE ARCHIVELOG DELETION POLICY TO NONE; # default
CONFIGURE SNAPSHOT CONTROLFILE NAME TO '/u01/app/oracle/product/11.2.0/db_1/dbs/snapcf_orcl.f'; # default
6 开始备份
#1 全库压缩备份
backup as compressed backupset full database format
'/backup/full_bk1_%u%p%s.rmn'include current controlfile
plus
archivelog format '/backup/arch_bk1_%u%p%s.rmn' delete all input;

RMAN> backup as compressed backupset full database format
'/backup/full_bk1_%u%p%s.rmn'include current controlfile
plus
archivelog format '/backup/arch_bk1_%u%p%s.rmn' delete all input;2> 3> 4>

Starting backup at 27-JUL-17
current log archived
using channel ORA_DISK_1
channel ORA_DISK_1: starting compressed archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=21 RECID=11 STAMP=950476796
input archived log thread=1 sequence=22 RECID=12 STAMP=950476832
channel ORA_DISK_1: starting piece 1 at 27-JUL-17
channel ORA_DISK_1: finished piece 1 at 27-JUL-17
piece handle=/backup/arch_bk1_0msae811122.rmn tag=TAG20170727T212032 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
channel ORA_DISK_1: deleting archived log(s)
archived log file name=/u01/app/oracle/archdata/1_21_895004586.dbf RECID=11 STAMP=950476796
archived log file name=/u01/app/oracle/archdata/1_22_895004586.dbf RECID=12 STAMP=950476832
Finished backup at 27-JUL-17

Starting backup at 27-JUL-17
using channel ORA_DISK_1
channel ORA_DISK_1: starting compressed 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=00003 name=/u01/app/oracle/oradata/orcl/undotbs01.dbf
input datafile file number=00005 name=/u01/app/oracle/oradata/orcl/example01.dbf
input datafile file number=00006 name=/u01/app/oracle/product/11.2.0/db_1/dbs/hdm_data.dbf
input datafile file number=00007 name=/u01/app/oracle/product/11.2.0/db_1/dbs/hy_ts_data.dbf
input datafile file number=00008 name=/u01/app/oracle/product/11.2.0/db_1/dbs/hy_ts_idx.dbf
input datafile file number=00004 name=/u01/app/oracle/oradata/orcl/users01.dbf
channel ORA_DISK_1: starting piece 1 at 27-JUL-17
channel ORA_DISK_1: finished piece 1 at 27-JUL-17
piece handle=/backup/full_bk1_0nsae812123.rmn tag=TAG20170727T212034 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:01:25
channel ORA_DISK_1: starting compressed full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including current control file in backup set
channel ORA_DISK_1: starting piece 1 at 27-JUL-17
channel ORA_DISK_1: finished piece 1 at 27-JUL-17
piece handle=/backup/full_bk1_0osae83n124.rmn tag=TAG20170727T212034 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 27-JUL-17

Starting backup at 27-JUL-17
current log archived
using channel ORA_DISK_1
channel ORA_DISK_1: starting compressed archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=23 RECID=13 STAMP=950476921
channel ORA_DISK_1: starting piece 1 at 27-JUL-17
channel ORA_DISK_1: finished piece 1 at 27-JUL-17
piece handle=/backup/arch_bk1_0psae83q125.rmn tag=TAG20170727T212201 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
channel ORA_DISK_1: deleting archived log(s)
archived log file name=/u01/app/oracle/archdata/1_23_895004586.dbf RECID=13 STAMP=950476921
Finished backup at 27-JUL-17

Starting Control File and SPFILE Autobackup at 27-JUL-17
piece handle=/backup/controlfileback/cf_c-1422785319-20170727-02 comment=NONE
Finished Control File and SPFILE Autobackup at 27-JUL-17

RMAN>
2 全库非压缩备份脚本
backup full database format
'/backup/full_bk1_%u%p%s.rmn'include current controlfile
plus
archivelog format '/backup/arch_bk1_%u%p%s.rmn'delete all input;

RMAN> backup full database format
'/backup/full_bk1_%u%p%s.rmn'include current controlfile
plus
archivelog format '/backup/arch_bk1_%u%p%s.rmn'delete all input;2> 3> 4>

# 备份开始时间
Starting backup at 27-JUL-17
# 一般都从归档日志备份
current log archived
#分配默认通道
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log backup set
# 归档日志列表
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=26 RECID=16 STAMP=950477478
channel ORA_DISK_1: starting piece 1 at 27-JUL-17
channel ORA_DISK_1: finished piece 1 at 27-JUL-17
#备份片名称 arch_bk1_10sae8l6132.rmn
piece handle=/backup/arch_bk1_10sae8l6132.rmn tag=TAG20170727T213118 comment=NONE
# 用时1秒
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
#删除备份过的归档日志
channel ORA_DISK_1: deleting archived log(s)
archived log file name=/u01/app/oracle/archdata/1_26_895004586.dbf RECID=16 STAMP=950477478
Finished backup at 27-JUL-17

Starting backup at 27-JUL-17
using channel ORA_DISK_1
#备份数据文件
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=00003 name=/u01/app/oracle/oradata/orcl/undotbs01.dbf
input datafile file number=00005 name=/u01/app/oracle/oradata/orcl/example01.dbf
input datafile file number=00006 name=/u01/app/oracle/product/11.2.0/db_1/dbs/hdm_data.dbf
input datafile file number=00007 name=/u01/app/oracle/product/11.2.0/db_1/dbs/hy_ts_data.dbf
input datafile file number=00008 name=/u01/app/oracle/product/11.2.0/db_1/dbs/hy_ts_idx.dbf
input datafile file number=00004 name=/u01/app/oracle/oradata/orcl/users01.dbf
channel ORA_DISK_1: starting piece 1 at 27-JUL-17
channel ORA_DISK_1: finished piece 1 at 27-JUL-17
#备份片名称 full_bk1_11sae8l7133.rmn
piece handle=/backup/full_bk1_11sae8l7133.rmn tag=TAG20170727T213119 comment=NONE
#用时1分26秒
channel ORA_DISK_1: backup set complete, elapsed time: 00:01:26
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
channel ORA_DISK_1: starting piece 1 at 27-JUL-17
channel ORA_DISK_1: finished piece 1 at 27-JUL-17
#备份片名称 full_bk1_12sae8nt134.rmn
piece handle=/backup/full_bk1_12sae8nt134.rmn tag=TAG20170727T213119 comment=NONE
#用时3秒
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:03
Finished backup at 27-JUL-17

Starting backup at 27-JUL-17
current log archived
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log backup set
#最后收尾在做一次归档日志备份
channel ORA_DISK_1: specifying archived log(s) in backup set
#这里面存放着数据库最后动作的信息
input archived log thread=1 sequence=27 RECID=17 STAMP=950477569
channel ORA_DISK_1: starting piece 1 at 27-JUL-17
channel ORA_DISK_1: finished piece 1 at 27-JUL-17
#备份片名称 arch_bk1_13sae8o1135.rmn
piece handle=/backup/arch_bk1_13sae8o1135.rmn tag=TAG20170727T213249 comment=NONE
#用时1秒
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
channel ORA_DISK_1: deleting archived log(s)
#删除27号归档
archived log file name=/u01/app/oracle/archdata/1_27_895004586.dbf RECID=17 STAMP=950477569
Finished backup at 27-JUL-17
#当数据库结构有变化时,自动触发控制文件和参数文件备份
Starting Control File and SPFILE Autobackup at 27-JUL-17
piece handle=/backup/controlfileback/cf_c-1422785319-20170727-04 comment=NONE
Finished Control File and SPFILE Autobackup at 27-JUL-17


RMAN>

全库备份脚本1

#!/bin/bash
#this is rman auto full backup script
#Copyright by 21og
#date 2012-03-21
#export ORACLE_SID=orcl
#export ORACLE_HOME=/opt/oracle/product/10.2/db_1
#export ORACLE_BASE=/opt/oracle
#export NLS_LANG="SIMPLIFIED CHINESE_CHINA.ZHS16GBK"
backtime=`date +"20%y%m%d%H%M%S"`
cd $ORACLE_HOME/bin
rman target / catalog rman/rman log=/backup/log/backupall_$backtime.log <<EOF
run{
allocate channel c1 device type disk;
allocate channel c2 device type disk;
allocate channel c3 device type disk;
allocate channel c4 device type disk;
allocate channel c5 device type disk;
crosscheck backup;
sql 'alter system archive log current';
backup database format '/opt/oracle/backup/dbback/db_%d_%T_%U';
sql 'alter system archive log current';
backup archivelog all format '/opt/oracle/backup/logback/arc_%t_%s' delete all input;
backup current controlfile format '/opt/oracle/backup/controlfileback/cntrl_%s_%p_%s';
crosscheck archivelog all;
delete noprompt expired backup;
delete noprompt obsolete;
delete noprompt backup of database completed before 'sysdate - 30';
release channel c1;
release channel c2;
release channel c3;
release channel c4;
release channel c5;
}
EOF
echo "backup complete!"


全库备份脚本2

#script.:BackupFull.sh
#creater:blackheart
#date:2017-07-27
#desc:backup full database datafile in archive with rman
#connect database
#export ORACLE_BASE=/u01/app/oracle
#export ORACLE_SID=orcl
#export ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1
#export PATH=$ORACLE_HOME/bin:$PATH
rman target/ << EOF_RMAN
run{
allocate channel c1 type disk;
backup incremental level 0 tag 'db0' format
'/home/oracle/RmanBackup/db0_%d_%T_%s' database include current controlfile;
sql 'alter system archive log current';
crosscheck archivelog all;
delete noprompt expired backup;
delete noprompt obsolete;
delete noprompt backup of database completed before 'sysdate - 30';

delete noprompt obsolete;
release channel c1;
}
# end


#定时任务
#创建定时脚本
vi task.sh
修改权限
chmod 744 task.sh
#设置定时任务
crontab -e
* * * * * /task.sh
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
#59 23 * * *      每天的21:43 执行 /data/app/scripts/monitor/df.sh
#15 05 * * *    每天的05:15 执行 /data/app/scripts/monitor/df.sh
#查看定时任务
crontab -l
#终止任务
crontab -r
#查看cron服务的启动情况
chkconfig --list | grep cron
[root@xiaoluo home]# chkconfig --list | grep cron
#统启动级别如果是1-4,cron服务都会开机自动启动的
crond    0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭
#启动
/sbin/service crond start
#停止
/sbin/service crond stop
#重启服务
/sbin/service crond restart
#重新加载配置
/sbin/service crond reload
#将cron设为在开机自启
vi /etc/rc.d/rc.local
ADD:
/sbin/service crond start
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: