如果需要从oracle中每天定时导出数据用什么方法?

如果需要从oracle中每天定时导出数据用什么方法?,第1张

1、创建数据库日志表导出为文本文件脚本tabout.ctl或tabout.sql

注意:该日志导出后在c:\HttpLog目录下

tabout.ctl脚本内容如下:

SET NEWPAGE NONE

SET HEADING OFF

SET TERM OFF

SET SPACE 0

SET PAGESIZE 0

SET TRIMOUT ON

SET TRIMSPOOL ON

SET LINESIZE 2500

set feedback off

set echo off

SET VERIFY OFF

column v_date new_value filename

select to_char(sysdate,'yyyymmdd') || '.log' v_date from dual

spool ..\HttpLog\&&filename            

select id||','||name from orcluser.test

spool off

exit

2、创建调用该脚本的logout.bat文件

logout.bat内容如下:

mkdir ..\HttpLog\

C:\oracle\ora92\bin\sqlplus orcluser/orcl @C:Oracleout.ctl

说明:该bat文件直接在当前目录下创建名为HttpLog的文件夹,以便tabout.ctl脚本输出的文件存放到该目录下。其通过调用Oracle中sqlplus可执行文件来执行tabout.ctl脚本,所以该文件直接双击即可运行,无需其他 *** 作。

3、通过windows定时任务实现自动定期执行

“在控制面板——任务计划——添加任务计划”中建立定期将数据库中日志表数据导出为文本格式(如:你可以根据需求设置为每天对日志表数据进行导出)。根据你当前对日志文件路径的存储需求,修改上面两个脚本中的存储路径,在windows定时任务直接调用logout.bat文件执行即可。

DB:ORACLE10G

任务简要描述:每天23点25分定时执行一段脚本, 导出表bbs_info中的数据。

〖Action(方法) 〗

Step01:以ORACLE的身份登陆 *** 作系统。

$ su - oracle

Password:

Sun Microsystems Inc. SunOS 5.9 Generic January 2003

$

Step02:创建参数文件,这个文件将控制导出的行为。

$ vi ePFILE

ePFILE的内容如下:

ROWS=y

TABLES = (TEST.bbs_info)

Compress = y

Feedback =5000

Buffer = 5000

log=/export/home/oracle/exportlog.txt

这个脚本的任务是:导出表bbs_info中的数据。

Step03:创建SHELL脚本文件expbbs_info。

$vi expbbs_info

加入如下内容:

echo -e '\n\n\n\n\n\n\n###############################################'

echo 'export data of bbs_info'

date

fmt=` date "+%Y-%m-%d[%HH%MM%SS]" `

ORACLE_SID=nbo

export ORACLE_SID

ORACLE_BASE=/oracle/app/oracle

export ORACLE_BASE

ORACLE_HOME=/oracle/app/oracle/product/10.1.0/Db_1

export ORACLE_HOME

PATH=/usr/bin:$ORACLE_HOME/bin:/bin

export PATH

HOME=/export/home/oracle

export HOME

exp system/wy file=/export/home/oracle/bbs_info"${fmt}".dmp PARFILE='/export/home/oracle/ePFILE'

echo 'export data of bbs_info complete!'

脚本说明:

?echo -e '\n\n\n\n\n\n\n###############################################为了显示美观。

?echo 'export data of bbs_info'是对这段SHELL脚本的任务进行说明

date显示执行这段SHELL脚本的时间。

?fmt=` date "+%Y-%m-%d[%HH%MM%SS]" `把系统当前日期存放到一个变量中,这个日期将追加到dump文件名中,这样我们就能很容易标识一个dump文件是什么时候导出的。date "+%Y-%m-%d[%HH%MM%SS]"会产生类似于下面的字符串:

2006-08-19[20H49M30S]

上面的字符串表示,系统当前时间是2006年8月19日,20点49分30秒。

?ORACLE_SID,ORACLE_BASE,ORACLE_HOME,PATH都是设置Oracle的环境变量。

?file=/export/home/oracle/bbs_info"${fmt}".dmp设置dump文件名的格式,其中用到了变量fmt。dump文件的名字应该类似于bbs_info2006-08-19[20H38M59S].dmp。

?exp这一行执行数据的导出,system/wy分别是执行数据导出的用户名和密码

?PARFILE用于指定参数文件

Step04:使shell脚本expbbs_info可执行。

$ chmod 775 expbbs_info

$ ls -l

total 16

-rw-r--r-- 1 oracle oinstall 2048 Aug 15 23:59 d1.dmp

-rw-r--r-- 1 oracle oinstall720 Aug 19 20:20 ePFILE

-rw-r--r-- 1 oracle oinstall660 Aug 15 23:58 ePFILE_pipe

-rwxrwxr-x 1 oracle oinstall472 Aug 19 20:21 expbbs_info

-rw-r--r-- 1 oracle oinstall540 Aug 15 23:59 exportlog.txt

-rw-r--r-- 1 oracle oinstall 2048 Aug 15 23:22 zip_pipe.dmp

Step05:手工执行这个SHELL脚本,看看能否成功导出数据。这一步是非常必要的,它可以检验这个SHELL脚本的正确性。

$ ./expbbs_info

###############################################

export data of bbs_info

Sat Aug 19 20:54:31 CET 2006

……

Step06:定时SHELL脚本expbbs_info。

$ crontab –e

加入如下的内容

25 23 * * * /export/home/oracle/expbbs_info >>/export/home/oracle/expbbs_log 2>&1

在每天的23点25分,将执行数据库的导出,导出的日志放到expbbs_log中。2>&1表示错误和正确信息都会被写到日志文件expbbs_log。

Step07:查看定时的情况。

$ crontab -l

25 23 * * * /export/home/oracle/expbbs_info >>/export/home/oracle/expbbs_log 2>&1

$

SHELL脚本定时以后,我们每天只需检查日志文件expbbs_log,就可以知道每天数据的导出情况。

///////////////////////////////////////////////////////////////////////

file1: export.bat

内容:

sqlplus user/pass@dbname @d:\export.sql

file2:export.sql

内容:

set term off verify off

set feedback off pagesize 999

spool d:\data.xls

set head off

select '<meta http-equiv=Content-Type content="text/htmlcharset=GB2312">' from dual

set head on

set linesize 200 markup html on entmap ON spool on preformat off

@d:\get_data.sql

spool off

exit

file3: get_data.sql

内容:

select sysdate from dual

1 如果数据量特别小可以尝试用spool命令

SQL>spool /home/oracle/cc.txt

SQL>select * from tables where time between 20150101 and 20150102

SQL>spool off

2 如果数据库版本高于9i,并且查询数据库小,时间段是最近的可以尝试闪回查询

SQL>select * from tables as of timestamp (systimestamp - interval '10' minute)

3 可以通过闪回数据库,闪回到你要的时间点,然后在备份

4 可以通过exp或expdp的query参数


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9975303.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-03
下一篇2023-05-03

发表评论

登录后才能评论

评论列表(0条)

    保存