windows下oracle数据文件的迁移和规范(一)

2014-11-24 09:43:37 · 作者: · 浏览: 0

研发中心一台windows2003上跑着oracle和sqlserver的数据库,是一台老机器,已经加挂了3块硬盘,但是最近接到同事请求处理说oracle数据库数据文件所在磁盘快满掉了,问改怎么处理,之前该同事已经咨询过另外一个本部门的同事,本部门同事竟然给答复说没办法处理,处理之后会导致数据库无法使用,我 !无奈研发同事转问我,但是给她答复说可以处理,于是跑过去看了下机器磁盘信息,发现每块盘都用的差不多快完了,必须购买新硬盘才能处理,无奈之下,研发同事只好选择删除掉1个历史大文档文件,然后先把其中的一个数据文件迁移过去,同时要求申购新硬盘。

今天新硬盘到货,上硬盘并分区,然后准备变更文件存放位置,了解了下该库有3个应用用户,而且数据文件存放的比较混乱,命名也不规范,针对这2点做了统一处理,将数据文件存放到新增加硬盘所在分区,安装用户名称规范存放,文件后增加序号标识。

一下是处理过程,记录在此!

执行如下脚本:

spool f:\mv_datafiles.txt

--Set linesize 200;

Set pagesize 100;

Column username format a8;

Column dtbspace format a8;

Column dtpspace format a8;

Column command format a75;

Select * From v$version;

select a.username,a.default_tablespace dtbspace,a.temporary_tablespace dtpspace from dba_users a where a.username in ('user1','user2','user3') order by a.username;

break on username skip 1;

break on offtbspace skip 1;

With Tbs As

(Select a.Username,

a.Default_Tablespace,

b.File_Name,

b.File_Id,

b.Bytes / 1024 / 1024 "SIZE-Mb"

From Dba_Users a, Dba_Data_Files b

Where a.Username In ('user1', 'user2', 'user3')

And a.Default_Tablespace = b.Tablespace_Name)

Select 'Datafile total size:' || To_Char(Sum("SIZE-Mb")) || 'Mb' command

From Tbs

Union All

Select '****Run follow script use sqlplus****'

From Dual

Union All

Select 'ALTER TABLESPACE ' || Default_Tablespace || ' OFFLINE;'

From Tbs

Group By Default_Tablespace

Union All

Select '****Run follow script use MS-DOS****'

From Dual

Union All

Select 'echo %date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,2%:%time:~3,2%:%time:~6,2%'

From Dual

Union All

Select 'ocopy ' || File_Name || ' f:\oradata\' || Username || '\' ||

Username || Row_Number() Over(Partition By Username Order By File_Id) || '.dbf' cptbdatafile

From Tbs

Union All

Select '****Run follow script use sqlplus****'

From Dual

union all

Select 'alter tablespace '||Default_Tablespace || ' rename datafile '''|| File_Name || ''' to ''f:\oradata\' || Username || '\' ||

Username || Row_Number() Over(Partition By Username Order By File_Id) || '.dbf'';' renametbdatafile

From Tbs

Union All

Select 'echo %date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,2%:%time:~3,2%:%time:~6,2%'

From Dual

Union All

Select '****Run follow script use sqlplus****'

From Dual

Union All

Select 'ALTER TABLESPACE ' || Default_Tablespace || ' ONLINE;'

From Tbs

Group By Default_Tablespace

Union All

Select '****Run follow script use MS-DOS****'

From Dual

Union All

Select 'before you delete datafiles job recommend you check the tbs and datafiles is work' From dual

Union All

Select 'del ' || File_Name Deldatafile From Tbs;

spool Off;

查看脱机文件,安装顺序执行脚本,注意在最后os delete的之前最好查看下表空间和数据文件的状态相关信息:

col defaultt_tablespace For a20;

col tablespacestatus For a17;

col defaultt_tablespace For a20;

col file_name For a50;

col datafilestatus For a15;

break On username Skip 1;

break On Default_Tablespace Skip 1;

Select a.Username,

a.Default_Tablespace,c.status tablespacestatus,b.file_name,b.status datafilestatus

From Dba_Users a, Dba_Data_Files b,Dba_Tablespaces c

Where a.Username In ('user1', 'user2', 'user3')

And a.Default_Tablespace = b.Tablespace_Name And b.tablespace_name=c.tablespace_name Order By usern