You are on page 1of 2

Copy Automatic Storage Management ASM files with

DBMS_FILE_TRANSFER
Oracle’s Automatic Storage Management (ASM), introduced in Oracle 10g, provides an alternative to raw
disk devices for storing Oracle-related files. Like raw disks, ASM volumes (called diskgroups) have no
filesystem and cannot be browsed directly at the operating system level. This makes maintenance a
challenge because you cannot use the normal commands to copy and delete files (cp and rm in UNIX, copy
and del in Windows).You can use RMAN to back up and restore ASM files and, in 10gR2, you can use
ASMCMD to view and manipulate the directory structure. The DBMS_FILE_TRANSFER package in
Oracle 10g is yet another way to work with ASM.

DBMS_FILE_TRANSFER copies files on the same Oracle server or between two Oracle servers. It uses
directory objects to specify the source and destination directories and, because directory objects support
ASM pathnames, so does DBMS_FILE_TRANSFER. This makes it an easy way to move files to and from
ASM storage from a regular filesystem.

DBMS_FILE_TRANSFER can copy any kind of file to and from regular filesystem storage, but it can only
transfer Oracle files to and from ASM diskgroups. Datafiles, logfiles (including archivelogs), and
controlfiles can be copied, but you can’t put a copy of your init.ora there (for example).

Suppose you use an ASM diskgroup to store archivelogs, and you need to extract some of the archivelogs to
send to a Data Guard standby server. Listing A shows two CREATE DIRECTORY commands that define
the archive directory on the ASM diskgroup (+DG1) on my server and a temporary directory (C:\Temp) in a
filesystem. Listing B shows the use of DBMS_FILE_TRANSFER’s COPY_FILE procedure to copy the
logs.

A cursor loops through the log names, selected from the V$ARCHIVED_LOG dynamic performance view.
The filename portion and sequence number are extracted via SUBSTR, and then a new filename is built.
Finally, DBMS_FILE_TRANSFER is called to perform the copy.

You can also transfer to and from other servers using the GET_FILE and PUT_FILE procedures in the
package.

Listing A
SQL> col name format a50
SQL> SELECT sequence#, name
2 FROM v$archived_log
3 ORDER BY sequence#;

SEQUENCE# NAME
---------- --------------------------------------------------
12 +DG1/orcl/arch/arc00012_0578762891.001
13 +DG1/orcl/arch/arc00013_0578762891.001
14 +DG1/orcl/arch/arc00014_0578762891.001

SQL> CREATE DIRECTORY archdir AS '+DG1/orcl/arch';

Directory created.

SQL> CREATE DIRECTORY tempdir AS 'C:\Temp';

Directory created.
SQL>
Listing B
set serverout on

DECLARE

v_archivedir VARCHAR2(30) := 'ARCHDIR';


v_tempdir VARCHAR2(30) := 'TEMPDIR';

v_asm_logname VARCHAR2(100);
v_win_logname VARCHAR2(100);

v_first_log_seq NUMBER := 12;


v_last_log_seq NUMBER := 14;
v_log_seq VARCHAR2(5);

CURSOR c_logs IS
SELECT name
FROM v$archived_log
WHERE sequence# BETWEEN v_first_log_seq AND v_last_log_seq
ORDER BY sequence#;

BEGIN
FOR i IN c_logs LOOP
v_asm_logname := SUBSTR(i.name, 16);
v_log_seq := SUBSTR(v_asm_logname,4,5);
v_win_logname := 'orcl_arc'||v_log_seq||'.log';

DBMS_FILE_TRANSFER.COPY_FILE(v_archivedir,
v_asm_logname,
v_tempdir,
v_win_logname);

DBMS_OUTPUT.PUT_LINE(v_asm_logname||' copied to '||


v_win_logname||'.');

END LOOP;
END;
/

SQL> @copyasm_b
arc00012_0578762891.001 copied to orcl_arc00012.log.
arc00013_0578762891.001 copied to orcl_arc00013.log.
arc00014_0578762891.001 copied to orcl_arc00014.log.

PL/SQL procedure successfully completed.

SQL>

You might also like