之前写了脚本,手动执行可以,使用crontab总是无法运行成功,今天下午花了两个小时实验,完成如下:
注意事项:脚本完成首先手动执行,确定可以正常执行。
在crontab中使用,要注意以下几点:
1.要定义环境变量,即使是使用的ORACLE用户crontab -e编辑的。source /home/oracle/.bash_profile或者直接将/home/oracle/.bash_profile内容放在备份脚本最前面。
2.然后注意脚本中调用RMAN时要用绝对路径,不然使用crontab无法运行成功。/u01/app/oracle/product/11.2.0/dbhome_1/bin/rman
下面是备份脚本及LINUX中设置crontab
一、备份脚本:只用了一个备份归档日志的脚本,比全备速度快方便实验。
[oracle@oel-01 ~]$ cat archback.sh
#!/bin/sh
#su - oracle
source /home/oracle/.bash_profile
#########back arch test 0704
##########
/u01/app/oracle/product/11.2.0/dbhome_1/bin/rman log /home/oracle/rman-arch`date +%Y%m%d-%H%M`.log <
connect target /;
run{
backup archivelog all delete input
format '/backup/archlog/arch_%d_%T_%s';
}
exit
二、在LINUX中设置crontab定时任务。在7月4号16点53分执行。
[oracle@oel-01 ~]$ crontab -e
53 16 4 7 * /home/oracle/archback.sh
按SHIFT+ZZ保存和在VI里一样哈哈。
[oracle@oel-01 ~]$ crontab -l
53 16 4 7 * /home/oracle/archback.sh
[oracle@oel-01 ~]$
这样就可以了。
三、使用archback.sh 脚本生成的日志:
[oracle@oel-01 ~]$ ls -al rman-arch20130704-1653.log
-rw-r--r-- 1 oracle oinstall 1246 Jul 4 16:53 rman-arch20130704-1653.log
[oracle@oel-01 ~]$ cat rman-arch20130704-1653.log
Recovery Manager: Release 11.2.0.1.0 - Production on Thu Jul 4 16:53:01 2013
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
RMAN>
connected to target database: BYS001 (DBID=2041679290)
RMAN> 2> 3> 4>
Starting backup at 04-JUL-13
current log archived
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=41 device type=DISK
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=19 RECID=111 STAMP=819910382
channel ORA_DISK_1: starting piece 1 at 04-JUL-13
channel ORA_DISK_1: finished piece 1 at 04-JUL-13
piece handle=/backup/archlog/arch_BYS001_20130704_119 tag=TAG20130704T165302 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:03
channel ORA_DISK_1: deleting archived log(s)
archived log file name=/u01/app/oracle/archbys001/arch_19_1_818895125.arc RECID=111 STAMP=819910382
Finished backup at 04-JUL-13
Starting Control File and SPFILE Autobackup at 04-JUL-13
piece handle=/backup/ctl_c-2041679290-20130704-04 comment=NONE
Finished Control File and SPFILE Autobackup at 04-JUL-13
RMAN>
Recovery Manager complete.
四、有的文档上说需要设置完crontab再进入 root用户,重启CROND服务。我的实验中未重启因为CROND就在运行着。
我这里CROND就在打开着,不需要重启就行,也可以去重启下 。
重启的示例如下 :
[oracle@oel-01 ~]$ su - root
Password:
[root@oel-01 ~]# service crond status
crond (pid 27777) is running...
[root@oel-01 ~]# service crond restart
Stopping crond: [ OK ]
Starting crond: [ OK ]
查看cron的日志:
[root@oel-01 ~]# cd /var/log
[root@oel-01 log]# tail cron
Jul 4 17:01:01 oel-01 crond[28305]: (root) CMD (run-parts /etc/cron.hourly)
Jul 4 17:02:50 oel-01 crontab[28313]: (oracle) BEGIN EDIT (oracle)
Jul 4 17:02:54 oel-01 crontab[28315]: (oracle) BEGIN EDIT (oracle)
Jul 4 17:02:58 oel-01 crontab[28317]: (oracle) BEGIN EDIT (oracle)
Jul 4 17:03:11 oel-01 crontab[28317]: (oracle) REPLACE (oracle)
Jul 4 17:03:11 oel-01 crontab[28317]: (oracle) END EDIT (oracle)
Jul 4 17:03:27 oel-01 crontab[28321]: (oracle) LIST (oracle)
Jul 4 17:04:01 oel-01 crond[27777]: (oracle) RELOAD (cron/oracle)
Jul 4 17:05:21 oel-01 crond[28396]: (CRON) STARTUP (V5.0)
Jul 4 17:05:21 oel-01 crond[28396]: (CRON) @reboot jobs will be run at computer's startup. ()
阅读(2527) | 评论(0) | 转发(0) |