沒必要用root權(quán)限執(zhí)行,oracle用戶同樣可以執(zhí)行crontab計劃任務(wù)
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:主機(jī)域名、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、三山網(wǎng)站維護(hù)、網(wǎng)站推廣。
1、su - oracle
crontab -e
0 4 * * 0 /home/oracle/exp.sh 21 /home/oracle/script_exp.log;
0~59 表示分
0~23 表示小時
1~31 表示日
1~12 表示月份
0~6 表示星期(其中0表示星期日)
2、cat /home/oracle/exp.sh
. ~/.bash_profile
bakdir=/opt/dbbak
logdir=/home/oracle/dbbak
rq=$(date +%Y%m%d)
exp system/***** owner=jysh file=${bakdir}/jysh_${rq}.dmp log=${logdir}/jysh_${rq}.log;
find ${bakdir} -type f -mtime +30 -exec rm -rf {} \;
用job,具體用法:
declare
n_job binary_integer;
begin
dbms_job.submit(n_job, '你要執(zhí)行的東西', sysdate, TRUNC(LAST_DAY(SYSDATE))+4+2/24);
end;
-- 每月4號執(zhí)行
dbms_job.submit(n_job_01,'你調(diào)的東西',sysdate,'TRUNC(LAST_DAY(SYSDATE))+4+2/24');
-- 每分鐘執(zhí)行一次
dbms_job.submit(n_job_01,'你調(diào)的東西',sysdate,'TRUNC(sysdate,’mi’) + 1 / (24*60)');
-- 凌晨兩點執(zhí)行
dbms_job.submit(n_job_01,'你調(diào)的東西',sysdate,'TRUNC(sysdate) + 1 + 2/24');
-- 每周一凌晨2點執(zhí)行 周一是每周的第二天next_day(sysdate,2)同理周二是第三天,next_day(sysdate,3)
dbms_job.submit(n_job_01,'你調(diào)的東西',sysdate,'TRUNC(next_day(sysdate,2))+2/24');
-- 每月1日凌晨兩點執(zhí)行
dbms_job.submit(n_job_01,'你調(diào)的東西',sysdate,'TRUNC(LAST_DAY(SYSDATE))+1+2/24');
-- 每季第一天凌晨兩點執(zhí)行
dbms_job.submit(n_job_01,'你調(diào)的東西',sysdate,'TRUNC(ADD_MONTHS(SYSDATE,3),'Q') + 2/24');
-- 每年7月1日和1月1日凌晨2點
dbms_job.submit(n_job_01,'你調(diào)的東西',sysdate,'ADD_MONTHS(trunc(sysdate,'yyyy'),6)+2/24');
-- 每年1月1日凌晨2點執(zhí)行
dbms_job.submit(n_job_01,'你調(diào)的東西',sysdate, 'Add_months(trunc(sysdate,'yyyy'), 12) +2/24');
在ORACLE數(shù)據(jù)庫中,RMAN備份的腳本非常多,下面介紹一例shell腳本如何通過RMAN備份,以及FTP上傳RMAN備份文件以及歸檔日志文件的腳本。
fullback.sh 里面調(diào)用RMAN命令做數(shù)據(jù)庫備份,它使用的cmdfile為/home/oracle/backup/bin/fullback.rcv,同時在/home/oracle/backup/logs目錄下生成日志文件。
1: [oracle@DB-Server bin]$ more fullback.sh
2:
3: #!/bin/bash
4:
5: export ORACLE_BASE=/u01/app/oracle
6:
7: export ORACLE_SID=gps
8:
9: ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1; export ORACLE_HOME
10:
11: TMP=/tmp; export TMP
12:
13: TMPDIR=$TMP; export TMPDIR
14:
15: PATH=/usr/sbin:$PATH; export PATH
16:
17: PATH=$ORACLE_HOME/bin:$PATH; export PATH
18:
19: LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib; export LD_LIBRARY_PATH
20:
21: CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib;
22:
23: export CLASSPATH
24:
25: TODAY=`date +%Y_%m_%d`
26:
27: rman nocatalog target / cmdfile /home/oracle/backup/bin/fullback.rcv log /home/oracle/backup/logs/fullbackup_$TODAY.log
28:
29: /home/oracle/backup/bin/ftpbackup.sh
30:
fullback.rcv文件非常簡單, 如下所示:
1: [oracle@DB-Server bin]$ more /home/oracle/backup/bin/fullback.rcv
2:
3: run{
4:
5: allocate channel c4 type disk;
6:
7: backup as compressed backupset
8:
9: skip inaccessible
10:
11: tag fullbackupwitharchivelog
12:
13: (database);
14:
15: backup current controlfile;
16:
17: backup spfile;
18:
19: sql "alter system archive log current";
20:
21: delete noprompt obsolete;
22:
23: release channel c4;
24:
25: }
26:
RMAN生成的備份文件,需要通過FTP上傳到FTP服務(wù)器,一則數(shù)據(jù)庫服務(wù)器沒有這么多空間存儲多天的備份,二則是出于容災(zāi)、數(shù)據(jù)安全需要。
下面腳本中FTP服務(wù)器,用戶名密碼均使用xxx替代,在實際環(huán)境中,使用具體的信息替代即可。
1: [oracle@DB-Server bin]$ more ftpbackup.sh
2:
3: #!/bin/sh、
4:
5: rm -f /home/oracle/.netrc
6:
7: date_yesterday=`date -d'yesterday' +%Y_%m_%d`
8:
9: date_today=`date +%Y_%m_%d`
10:
11: echo "default login xxxx password xxxxxx" /home/oracle/.netrc
12:
13: echo "macdef init" /home/oracle/.netrc
14:
15: echo "binary" /home/oracle/.netrc
16:
17: echo "cd archivelog" /home/oracle/.netrc
18:
19: echo "mkdir $date_yesterday" /home/oracle/.netrc
20:
21: echo "cd $date_yesterday" /home/oracle/.netrc
22:
23: echo "lcd /u04/flash_recovery_area/gps/archivelog/$date_yesterday" /home/oracle/.netrc
24:
25: echo "mput *" /home/oracle/.netrc
26:
27: echo "cd .." /home/oracle/.netrc
28:
29: echo "mkdir $date_today" /home/oracle/.netrc
30:
31: echo "cd $date_today" /home/oracle/.netrc
32:
33: echo "lcd /u04/flash_recovery_area/gps/archivelog/$date_today" /home/oracle/.netrc
34:
35: echo "mput * "/home/oracle/.netrc
36:
37: echo "cd .." /home/oracle/.netrc
38:
39: echo "cd ../backupset" /home/oracle/.netrc
40:
41: echo "mkdir $date_today" /home/oracle/.netrc
42:
43: echo "cd $date_today" /home/oracle/.netrc
44:
45: echo "lcd /u04/flash_recovery_area/gps/backupset/$date_today" /home/oracle/.netrc
46:
47: echo "mput *" /home/oracle/.netrc
48:
49: echo "cd .." /home/oracle/.netrc
50:
51: echo "cd ../autobackup" /home/oracle/.netrc
52:
53: echo "mkdir $date_today" /home/oracle/.netrc
54:
55: echo "cd $date_today" /home/oracle/.netrc
56:
57: echo "lcd /u04/flash_recovery_area/gps/autobackup/$date_today" /home/oracle/.netrc
58:
59: echo "mput *" /home/oracle/.netrc
60:
61: echo "quit" /home/oracle/.netrc
62:
63: echo "" /home/oracle/.netrc
64:
65: chmod 600 /home/oracle/.netrc
66:
67: ftp -i -v xxx.xxx.xxx.xxx 8021 /home/oracle/backup/logs/ftp$date_today.log 21
68:
另外,關(guān)于歸檔日志也需要每隔2小時上傳一次到FTP服務(wù)器,2小時上傳一次歸檔日志的shell腳本如下所示:
1: [oracle@DB-Server bin]$ more ftp2hours.sh
2:
3: #!/bin/sh
4:
5: rm -f /home/oracle/.netrc
6:
7: date_yesterday=`date -d'yesterday' +%Y_%m_%d`
8:
9: date_today=`date +%Y_%m_%d`
10:
11: echo "default login xxxx password xxxx" /home/oracle/.netrc
12:
13: echo "macdef init" /home/oracle/.netrc
14:
15: echo "binary" /home/oracle/.netrc
16:
17: echo "cd archivelog" /home/oracle/.netrc
18:
19: echo "mkdir $date_today" /home/oracle/.netrc
20:
21: echo "cd $date_today" /home/oracle/.netrc
22:
23: echo "lcd /u04/flash_recovery_area/gps/archivelog/$date_today" /home/oracle/.netrc
24:
25: echo "mput * "/home/oracle/.netrc
26:
27: echo "quit" /home/oracle/.netrc
28:
29: echo "" /home/oracle/.netrc
30:
31: chmod 600 /home/oracle/.netrc
32:
33: ftp -i -v xxx.xxx.xxx.xxx 8021 /home/oracle/backup/logs/ftp2hours.$date_today.log 21
34:
最后需要將RMAN備份生成的日志文件,以及FTP上傳備份文件以及歸檔日志的記錄通過郵件形式發(fā)送給DBA或系統(tǒng)管理員,
1: [oracle@DB-Server bin]$ more chkbackandmail.sh
2: #!/bin/bash
3: rm -f /home/oracle/backup/bin/sendmail.pl
4: date_today=`date +%Y_%m_%d`
5: subject="Oracle Backup Alert Service on $date_today"
6: content="Dear colleagues,
7:
8: Attached please find the logs of xxx(xxx.xxx.xxx.xxx) oracle database backup and transfer to FTP Server(xxx.xxx.xxx.xxx), please
9: review the file and check whether the backup succeeded or not,and double check all backups have been dumped to tape, many tha
10: nks
11:
12:
13:
14:
15: Best regards
16: Oracle Alert Services
17:
18: "
19: file="/home/oracle/backup/logs/fullbackup_$date_today.log,/home/oracle/backup/logs/ftp$date_today.log"
20: echo "#!/usr/bin/perl" /home/oracle/backup/bin/sendmail.pl
21: echo "use Mail::Sender;" /home/oracle/backup/bin/sendmail.pl
22: echo "\$sender = new Mail::Sender {smtp = 'xxx.xxx.xxx.xxx', from = 'xxxx@xxx.com'}; " /home/oracle/backup/bin/sendmai
23: l.pl
24: echo "\$sender-MailFile({to = 'xxx@esquel.com'," /home/oracle/backup/bin/sendmail.pl
25: echo "cc='xxx@xxx.com,xxx@xxx.com,xxx@xxx.com'," /home/oracle/backup/b
26: in/sendmail.pl
27: echo "subject = '$subject'," /home/oracle/backup/bin/sendmail.pl
28: echo "msg = '$content'," /home/oracle/backup/bin/sendmail.pl
29: echo "file = '$file'});" /home/oracle/backup/bin/sendmail.pl
30: perl /home/oracle/backup/bin/sendmail.pl
最后在Crontab 作業(yè)里面配置調(diào)用這些shell腳本。例如如下所示,在1:01分執(zhí)行fullback.sh ,每隔兩個小時(例如0:50、2:50...)執(zhí)行一次ftp2hours.sh, 在每天早上8:40執(zhí)行chkbackandmail.sh 發(fā)送fullback.sh 以及ftp2hour.sh的執(zhí)行日志記錄。