Copy OS files from Oracle
Posted by Kamran Agayev A. on 9th December 2008
Sometimes you need to copy some OS file from one location to another. If you’re using only Sql*Plus and you can’t connect directly to OS, you can user Oracle’s UTL_FILE.FCOPY function to achieve this goal.
Firstly, we’ll create two directories and one file. Then we’ll create a user and copy this file from one folder to another using UTL_FILE package
[oracle@localhost /]$ cd /tmp
[oracle@localhost tmp]$ mkdir copy_dir
[oracle@localhost tmp]$ mkdir backup_dir
[oracle@localhost tmp]$ ls backup_dir/
[oracle@localhost tmp]$
[oracle@localhost tmp]$ cd copy_dir
[oracle@localhost copy_dir]$ vi test.dat (We are adding a line with word MY_TEST to file test.dat)
[oracle@localhost copy_dir]$ ls -lh
total 4.0K
-rw-r–r– 1 oracle oinstall 8 Dec 7 04:11 test.dat
[oracle@localhost copy_dir]$ cat test.dat
MY_TEST
[oracle@localhost copy_dir]$sqlplus “/ as sysdba”
SQL> CREATE USER kamran IDENTIFIED BY kamran;
User created.
SQL> grant connect, resource to kamran;
Grant succeeded.
SQL> grant create any directory to kamran;
Grant succeeded.
SQL> grant execute on utl_file to kamran;
Grant succeeded.
SQL> conn kamran/kamran
Connected.
SQL> create directory file_source as ‘/tmp/copy_dir’;
Directory created.
SQL> create directory backup_source as ‘/tmp/backup_dir’;
Directory created.
SQL> exec utl_file.fcopy(‘FILE_SOURCE’,’test.dat’,’BACKUP_SOURCE’,’copied_test.dat’);
PL/SQL procedure successfully completed.
SQL>exit
[oracle@localhost tmp]$ cd backup_dir/
[oracle@localhost backup_dir]$ ls -lh
total 4.0K
-rw-r–r– 1 oracle oinstall 8 Dec 7 04:32 copied_test.dat
[oracle@localhost backup_dir]$
Posted in DBA scripts | 2 Comments »