怎么写oracle脚本 oracle 脚本

oracle rman备份脚本怎么写在ORACLE数据库中,RMAN备份怎么写oracle脚本的脚本非常多,下面介绍一例shell脚本如何通过RMAN备份,以及FTP上传RMAN备份文件以及归档日志文件怎么写oracle脚本的脚本 。
fullback.sh 里面调用RMAN命令做数据库备份,它使用怎么写oracle脚本的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文件非常简单,如下所示怎么写oracle脚本:
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服务器,一则数据库服务器没有这么多空间存储多天的备份,二则是出于容灾、数据安全需要 。
下面脚本中FTP服务器,用户名密码均使用xxx替代,在实际环境中,使用具体的信息替代即可 。
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:
另外,关于归档日志也需要每隔2小时上传一次到FTP服务器,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上传备份文件以及归档日志的记录通过邮件形式发送给DBA或系统管理员,
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 作业里面配置调用这些shell脚本 。例如如下所示,在1:01分执行fullback.sh,每隔两个小时(例如0:50、2:50...)执行一次ftp2hours.sh, 在每天早上8:40执行chkbackandmail.sh 发送fullback.sh 以及ftp2hour.sh的执行日志记录 。
谁来写一个linux下开机启动oracle的脚本以root身份建立开机启动oracle服务的脚本:vi /etc/init.d/oracle,添加如下脚本:
#!/bin/sh
#chkconfig: 2345 20 80
#description: Oracle dbstart / dbshut
#以上两行为chkconfig所需
ORA_HOME=/opt/oracle/11g
ORA_OWNER=oracle
LOGFILE=/var/log/oracle.log
echo "#################################"${LOGFILE}
date"### %T %a %D: Run Oracle"${LOGFILE}
if [ ! -f ${ORA_HOME}/bin/dbstart ] || [ ! -f ${ORA_HOME}/bin/dbshut ]; then
echo "Error: Missing the script file ${ORA_HOME}/bin/dbstart or ${ORA_HOME}/bin/dbshut!"${LOGFILE}
echo "#################################"${LOGFILE}
exit
fi
start(){
echo "###Startup Database..."
su - ${ORA_OWNER} -c "${ORA_HOME}/bin/dbstart ${ORA_HOME}"
echo "###Done."
echo "###Run database control..."
su - ${ORA_OWNER} -c "${ORA_HOME}/bin/emctl start dbconsole"
echo "###Done."
}
stop(){
echo "###Stop database control..."
su - ${ORA_OWNER} -c "${ORA_HOME}/bin/emctl stop dbconsole"
echo "###Done."
echo "###Shutdown Database..."
su - ${ORA_OWNER} -c "${ORA_HOME}/bin/dbshut ${ORA_HOME}"
echo "###Done."
}
case "$1" in
'start')
start${LOGFILE}
'stop')
stop${LOGFILE}
'restart')
stop${LOGFILE}
start${LOGFILE}
esac
date"### %T %a %D: Finished."${LOGFILE}
echo "#################################"${LOGFILE}
echo ""
使用如下命令将 /etc/init.d/oracle 置为可执行文件:
chmod a x /etc/init.d/oracle
至此 , 可使用如下命令对oracle进行启动和关闭
/etc/init.d/oracle start #启动oracle(包括数据库实例、监听器、EM)
/etc/init.d/oracle stop #关闭oracle
/etc/init.d/oracle restart #重启oracle
将 oracle 添加到 chkconfig中:
chkconfig --add oracle
可使用如下命令查看和设置oracle服务的开机启动级别:
chkconfig | grep oracle #查看oracle服务的开机启动级别
chkconfig --level 24 oracle off #修改oracle服务的开机启动级别
chkconfig --level 35 oracle on
至此可使用如下命令对oracle的启动或关闭进行管理
service oracle start #启动
service oracle stop #关闭
service oracle restart #重启
建立连接:
ln -s /etc/init.d/oracle /etc/rc0.d/K01oracle#关机执行
ln -s /etc/init.d/oracle /etc/rc6.d/K01oracle#重启执行
Oracle数据库备份脚本怎么写?没必要用root权限执行怎么写oracle脚本 , oracle用户同样可以执行crontab计划任务
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 -mtime30 -exec rm -rf {} \;
怎么在oracle下写一个脚本 , 然后导出表数据为EXCEL格式文件?无法直接保存到excel中怎么写oracle脚本,但是可以保存到csv文件,同样是excel的格式 , 方法如下怎么写oracle脚本:
譬如要把文件生成在d盘下的test目录下怎么写oracle脚本:
1,在d盘根目录下新建test目录
2,sqlplus以system用sysdba登录
3,create or replace directory TMP as ’d:\test’;
4,grant read on directory TMP to user;
5,alter system set utl_file_dir='d:\test' scope=spfile;
然后建立存储过程 , 表是随便建了一个,最后的文件名按要求需要按照当前日期的前一天生成,所以后边文件名的地方write_file_name处就按照要求来了
create or replace PROCEDURE SP_OUTPUT
(
on_flagOUTNUMBER,
out_reasonOUTVARCHAR2)
is
v_codenumber;
v_textvarchar2(255);
file_handle utl_file.file_type;
Write_content VARCHAR2(1024);
Write_file_name VARCHAR2(50);
v_aaa varchar2(5);
v_bbb varchar2(5);
cursor cur_sp_out
is
select aaa,bbb
from aaa;
begin
open cur_sp_out;
loop
fetch cur_sp_out intov_aaa,v_bbb;
exit when cur_sp_out%notfound;
write_file_name := to_char(SYSDATE,'YYYYMMDD')||'.xls';
file_handle := utl_file.fopen('TMP',write_file_name,'a');
write_content := v_aaa||' '||v_bbb;
--write file
IF utl_file.is_open(file_handle) THEN
utl_file.put_line(file_handle,write_content);
END IF;
--close file
utl_file.fclose(file_handle);
end loop;
close cur_sp_out;
v_code := 0;
v_text := '完成';
exception
when others then
on_flag := SQLCODE;
out_reason := SQLERRM;
begin
IF utl_file.is_open(file_handle) THEN
utl_file.fclose(file_handle);
end IF;
end;
rollback;
end;
这样基本就完成了,但是建立完毕后需要重启数据库,否则文件写不进指定的目录下
需要注意的是: write_content := v_aaa||' '||v_bbb;这个位置
v_aaa|| 和v_bbb中间引号的地方是TAB符 , 否则输出来的XLS文件中,会把这些字符串合在一个单元格中 。
【怎么写oracle脚本 oracle 脚本】关于怎么写oracle脚本和oracle 脚本的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读