1. backup.bat
@ECHO OFF

SET YYYY=%date:~0,4%
SET MM=%date:~5,2%
SET DD=%date:~8,2%
SET HH=%time:~0,2%
SET MI=%time:~3,2%
SET SS=%time:~6,2%

REM fill zero
SET TT=0
FOR %%C IN (%HH%) DO (
    SET TT=%TT%%%C
)
SET HH=%TT:~-2%

:: 파일명 만들기
SET FILENAME=DUMP_%YYYY%%MM%%DD%%HH%%MI%%SS%.dmp

:: 파일명을 backup_file.txt에 저장
ECHO %FILENAME% > backup_file.txt

:: Export
:: EXP old_user/password@old_db OWNER=old_user ROWS=Y STATISTICS=NONE CONSTRAINTS=Y LOG=%FILENAME%.log FILE=%FILENAME%

:: Export Data Pump
EXPDP system/password@old_db DIRECTORY=DATA_PUMP_DIR DUMPFILE=%FILENAME% LOGFILE=%FILENAME%.log SCHEMAS=OLD_USER

ECHO.

PAUSE

 

2. restore.bat
@ECHO OFF

:: 사용자 세션종료 & DROP & CREATE
SQLPLUS system/password@new_db @script.sql

ECHO.
ECHO.

ECHO Start Import

:: 백업한 파일명 알아오기
SET /p FILENAME=< backup_file.txt

:: Import
:: IMP new_user/password@new_db FROMUSER=old_user TOUSER=new_user ROWS=Y IGNORE=Y LOG=%FILENAME%.log FILE=%FILENAME%

:: Import Data Pump
IMPDP system/password@new_db DIRECTORY=DATA_PUMP_DIR DUMPFILE=%FILENAME% LOGFILE=%FILENAME%.log REMAP_TABLESPACE=OLD_TABLESPACE:NEW_TABLESPACE REMAP_SCHEMA=OLD_USER:NEW_USER

ECHO.

PAUSE

 

3. script.sql 
-- 현재세션 강제종료 시키고, 사용자 삭제하고 다시 생성하여 종속된 objects 제거한다.
DECLARE

BEGIN
  FOR V_ROW IN (SELECT SID, SERIAL# FROM V$SESSION WHERE USERNAME = 'NEW_USER')
  LOOP
    
    EXECUTE IMMEDIATE 'ALTER SYSTEM KILL SESSION ' || CHR(39) || V_ROW.SID || ', ' || V_ROW.SERIAL# || CHR(39);
    
  END LOOP;
END;
/

DROP USER new_user CASCADE;

CREATE USER new_user IDENTIFIED BY password
    DEFAULT TABLESPACE NEW_TABLESPACE
    TEMPORARY TABLESPACE TEMP;

GRANT CONNECT, RESOURCE, DBA, UNLIMITED TABLESPACE TO new_user;

QUIT;​