1. 备份策略
每周日做0级备份。
每周一至周六做1级差异增量备份。
2. 设置数据库为归档模式
2.1. RAC数据库
~]# su - oracle
~]$ sqlplus / as sysdba
SQL> alter system set log_archive_dest_1='LOCATION=+FRA' scope=spfile;
SQL> shu immediate;
SQL> startup mount;
SQL> alter database archivelog;
SQL> alter database open;
SQL> archive log list;
2.2. 单实例数据库
~]# mkdir -p /dbData/OracleBack/{archivelog,rmanbak}
~]# chown -R oracle:oinstall /dbData/OracleBack/
~]# su - oracle
~]$ sqlplus / as sysdba
SQL> alter system set log_archive_dest_1='LOCATION=/dbData/OracleBack/archivelog/' scope=spfile;
SQL> shu immediate;
SQL> startup mount;
SQL> alter database archivelog;
SQL> alter database open;
SQL> archive log list;
3. 启/禁用块改变跟踪
3.1. RAC数据库
SQL> alter database enable block change tracking using file '+DATAC1';
Database altered
3.2. 单实例数据库
SQL> alter database enable block change tracking using file '/dbData/OracleBack/rman_change_track.f' reuse;
3.3. 禁用块改变跟踪
ALTER DATABASE DISABLE BLOCK CHANGE TRACKING;
4. 备份脚本
4.1. RAC数据库-使用catalog目录增量备份
cat /dbData/OracleBack/rmanbak/rmanbackup.sh
#!/bin/bash
source /home/oracle/.bash_profile
current_day=`date +%Y%m%d`
bakfile_path='+DATAC1'
archfile_path='+DATAC1'
channel_num=12
backup_log=/dbData/OracleBack/rmanbaklog/rmanbak-$ORACLE_SID-`date +%Y%m%d-%H%M`.log
weekday=`date '+%u'`
mkdir -p /dbData/OracleBack/rmanbak
mkdir -p /dbData/OracleBack/rmanbaklog
$ORACLE_HOME/bin/rman target / catalog '"rco/Hz571!xy00yz@xxx.xxx.xxx.xxx:1521/catpdb"' log=$backup_log << EON
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 8 DAYS;
CONFIGURE CONTROLFILE AUTOBACKUP ON;
#CONFIGURE ARCHIVELOG DELETION POLICY TO APPLIED ON ALL STANDBY;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '$bakfile_path';
CONFIGURE DEVICE TYPE DISK PARALLELISM $channel_num BACKUP TYPE TO BACKUPSET;
EON
case $weekday in
1|2|3|4|5|6)
# Begin backup incremental level 1 database
$ORACLE_HOME/bin/rman target / catalog '"rco/Hz571!xy00yz@xxx.xxx.xxx.xxx:1521/catpdb"' log=$backup_log << EON
run {
BACKUP INCREMENTAL LEVEL 1 DATABASE TAG 'DAILY_1' FORMAT '$bakfile_path' PLUS ARCHIVELOG TAG 'ARC_1' FORMAT '$archfile_path' DELETE ALL INPUT;
sql 'alter system archive log current';
DELETE NOPROMPT OBSOLETE RECOVERY WINDOW OF 7 DAYS;
}
EON
;;
7)
# Begin backup incremental level 0 database
$ORACLE_HOME/bin/rman target / catalog '"rco/Hz571!xy00yz@xxx.xxx.xxx.xxx:1521/catpdb"' log=$backup_log <<EON
run {
BACKUP INCREMENTAL LEVEL 0 DATABASE TAG 'WEEKLY_0' FORMAT '$bakfile_path' PLUS ARCHIVELOG TAG 'ARC_0' FORMAT '$archfile_path' DELETE ALL INPUT;
sql 'alter system archive log current';
DELETE NOPROMPT OBSOLETE RECOVERY WINDOW OF 7 DAYS;
DELETE NOPROMPT EXPIRED BACKUP;
}
EON
;;
esac
4.2. RAC数据库-不使用catalog目录增量备份
~]# cat rmanbackup.sh
#!/bin/bash
source /home/oracle/.bash_profile
current_day=`date +%Y%m%d`
bakfile_path='+BAKDG'
archfile_path='+ARCHDG'
channel_num=12
backup_log=/dbData/OracleBack/rmanbaklog/rmanbak-$ORACLE_SID-`date +%Y%m%d-%H%M`.log
weekday=`date '+%u'`
mkdir -p /dbData/OracleBack/rmanbak
mkdir -p /dbData/OracleBack/rmanbaklog
$ORACLE_HOME/bin/rman target / log=$backup_log << EON
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 5 DAYS;
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE ARCHIVELOG DELETION POLICY TO APPLIED ON ALL STANDBY;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '$bakfile_path';
CONFIGURE DEVICE TYPE DISK PARALLELISM $channel_num BACKUP TYPE TO BACKUPSET;
EON
case $weekday in
1|2|3|4|5|6)
echo "Begin backup incremental level 1 database';"
echo "Please see the backup logfile $backup_log"
echo "Please wait for a minute......................................................."
$ORACLE_HOME/bin/rman target / log=$backup_log <<EON
run {
BACKUP INCREMENTAL LEVEL 1 TAG 'bakLevel1' FORMAT '$bakfile_path' DATABASE;
sql 'alter system archive log current';
BACKUP ARCHIVELOG ALL DELETE INPUT FORMAT '$archfile_path' TAG 'ARCH_BAK';
DELETE NOPROMPT OBSOLETE;
}
EON
;;
7)
# Begin backup incremental level 0 database
echo "Begin backup incremental level 0 database';"
echo "Please see the backup logfile $backup_log"
echo "Please wait for a minute......................................................."
$ORACLE_HOME/bin/rman target / log=$backup_log <<EON
run {
BACKUP INCREMENTAL LEVEL 0 TAG 'bakLevel0' FORMAT '$bakfile_path' DATABASE;
sql 'alter system archive log current';
BACKUP ARCHIVELOG ALL DELETE INPUT FORMAT '$archfile_path' TAG 'ARCH_BAK';
DELETE NOPROMPT OBSOLETE;
DELETE NOPROMPT EXPIRED BACKUP;
}
EON
;;
esac
4.3. 单实例数据库-全量备份数据库脚本
~]$ cat rmanbackup.sh
#!/bin/bash
source /home/oracle/.bash_profile
current_day=`date +%Y%m%d`
bakfile_path='/dbData/OracleBack/rmanbak'
channel_num=4
backup_log=/dbData/OracleBack/rmanbaklog/rmanbak-$ORACLE_SID-`date +%Y%m%d-%H%M`.log
weekday=`date '+%u'`
mkdir -p /dbData/OracleBack/rmanbak
mkdir -p /dbData/OracleBack/rmanbaklog
$ORACLE_HOME/bin/rman target / log=$backup_log << EON
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 5 DAYS;
CONFIGURE CONTROLFILE AUTOBACKUP ON;
#CONFIGURE ARCHIVELOG DELETION POLICY TO APPLIED ON ALL STANDBY;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '$bakfile_path';
CONFIGURE DEVICE TYPE DISK PARALLELISM $channel_num BACKUP TYPE TO BACKUPSET;
EON
$ORACLE_HOME/bin/rman target / log=$backup_log <<EON
run {
BACKUP DATABASE TAG 'DB_FULL' FORMAT '$bakfile_path/dbfull_%T_%U'
PLUS ARCHIVELOG TAG 'ARC_BAK' FORMAT '$bakfile_path/arch_%T_%U' DELETE ALL INPUT;
sql 'alter system archive log current';
}
ALLOCATE CHANNEL FOR MAINTENANCE DEVICE TYPE DISK;
DELETE NOPROMPT OBSOLETE;
DELETE NOPROMPT EXPIRED BACKUP;
LIST BACKUP SUMMARY;
RELEASE CHANNEL;
EON
4.4. 单实例数据库-增量备份数据库脚本
~]$ cat rmanbackup.sh
#!/bin/bash
source /home/oracle/.bash_profile
current_day=`date +%Y%m%d`
bakfile_path='/dbData/OracleBack/rmanbak'
channel_num=4
backup_log=/dbData/OracleBack/rmanbaklog/rmanbak-$ORACLE_SID-`date +%Y%m%d-%H%M`.log
weekday=`date '+%u'`
mkdir -p /dbData/OracleBack/rmanbak
mkdir -p /dbData/OracleBack/rmanbaklog
$ORACLE_HOME/bin/rman target / log=$backup_log << EON
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 5 DAYS;
CONFIGURE CONTROLFILE AUTOBACKUP ON;
#CONFIGURE ARCHIVELOG DELETION POLICY TO APPLIED ON ALL STANDBY;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '$bakfile_path';
CONFIGURE DEVICE TYPE DISK PARALLELISM $channel_num BACKUP TYPE TO BACKUPSET;
EON
case $weekday in
1|2|3|4|5|6)
echo "Begin backup incremental level 1 database';"
echo "Please see the backup logfile $backup_log"
echo "Please wait for a minute......................................................."
$ORACLE_HOME/bin/rman target / log=$backup_log <<EON
run {
BACKUP INCREMENTAL LEVEL 1 TAG 'bakLevel1' FORMAT '$bakfile_path' DATABASE;
sql 'alter system archive log current';
BACKUP ARCHIVELOG ALL DELETE INPUT FORMAT '$archfile_path' TAG 'ARCH_BAK';
DELETE NOPROMPT OBSOLETE;
}
EON
;;
7)
# Begin backup incremental level 0 database
echo "Begin backup incremental level 0 database';"
echo "Please see the backup logfile $backup_log"
echo "Please wait for a minute......................................................."
$ORACLE_HOME/bin/rman target / log=$backup_log <<EON
run {
BACKUP INCREMENTAL LEVEL 0 TAG 'bakLevel0' FORMAT '$bakfile_path' DATABASE;
sql 'alter system archive log current';
BACKUP ARCHIVELOG ALL DELETE INPUT FORMAT '$archfile_path' TAG 'ARCH_BAK';
DELETE NOPROMPT OBSOLETE;
DELETE NOPROMPT EXPIRED BACKUP;
}
EON
;;
esac
5. 定时任务
在oracle用户下创建定时备份任务,每天的 04:00
执行 rmanbackup.sh
这个备份脚本。
~]# su - oracle
~]$ crontab -e
00 04 * * * sh /dbData/OracleBack/rmanbak/rmanbackup.sh
另外也可参考下面的写法:
# Perform full backup on database Sunday at 3:05 am
5 3 * * 0 /$scrip_path/backup_db database_name full > /$scrip_path/logs/full_backup.log
# Perform incremental backups on database Mon-Sat at 3:05 am
5 3 * * 1-6 /$scrip_path/backup_db database_name inc > /$scrip_path/logs/inc_backup.log