为确保oracle数据库数据的安全和一致性,一般我们都需要利用备份手段进行数据库的备份。在oracle数据库中,rman因其强大的功能和完善的手段,成为数据库备份的首选。Linux服务器中,要实现自动脚本备份,一般都是通过root用户设置crond定时任务执行脚本完成(oracle用户执行调取的涉及到环境变量等配置问题,弄起来更麻烦),故以下通过两种方式配置脚本,实现rman自动备份。
此处我分配4个本地通道进行备份,通道指定备份路径(自己挂载的备份盘)。具体备份语句如下:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
run{ allocate channel c1 type disk format '/backup/hpdb_%d_DB_%u_%T'; allocate channel c2 type disk format '/backup/hpdb_%d_DB_%u_%T'; allocate channel c3 type disk format '/backup/hpdb_%d_DB_%u_%T'; allocate channel c4 type disk format '/backup/hpdb_%d_DB_%u_%T'; sql 'alter system archive log current'; backup full (database include current controlfile); release channel c1; release channel c2; release channel c3; release channel c4; } |
归档日志备份时,为避免有时候人工删除文件导致备份失败,我们每次备份进行crosscheck,确保备份成功,且对备份成功的归档日志文件进行删除,确保目录空间不被撑满,具体如下:
|
1 2 3 4 5 6 7 8 9 10 11 |
crosscheck archivelog all; run{ allocate channel c1 type disk format '/backup/arch_%d_DB_%u_%T'; allocate channel c2 type disk format '/backup/arch_%d_DB_%u_%T'; allocate channel c3 type disk format '/backup/arch_%d_DB_%u_%T'; sql 'alter system archive log current'; backup (archivelog all delete input); release channel c1; release channel c2; release channel c3; } |
任务脚本我们可以有两种方式进行编写,具体如下:
|
1 2 |
find /backup -type f -mtime +5 | xargs rm -f su - oracle -c "rman target / cmdfile=/home/oracle/backup/backup.sh msglog=/home/oracle/backup/rman.log" |
第二种方式我们可以通过全脚本的形式写入到内部进行
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
su - oracle -c "rman target / << !EOF run{ allocate channel c1 type disk format '/backup/hpdb_%d_DB_%u_%T'; allocate channel c2 type disk format '/backup/hpdb_%d_DB_%u_%T'; allocate channel c3 type disk format '/backup/hpdb_%d_DB_%u_%T'; allocate channel c4 type disk format '/backup/hpdb_%d_DB_%u_%T'; sql 'alter system archive log current'; backup full (database include current controlfile); release channel c1; release channel c2; release channel c3; release channel c4; } quit !EOF " |
我们配置每天晚上10点进行相应的备份任务,进行crond配置如下:
|
1 2 |
crontab -e 00 22 * * * sh /home/oracle/backupcrond.sh |