Oracle備份、還原數據庫

備份數據庫

建立備份目錄(用sys帳號),若已建立備份目錄,此步可忽略

create directory db_bak as 'D:\ ECIMS_DB'
--查看建立的目錄
select * from dba_directories

--刪除已建立的目錄
drop directory DB_BAK

格式:
drop directory 目錄名

備份(導出)數據庫(cmd狀態下)

expdp XXX/XXX@XXX schemas=XXX dumpfile=XXX_20181130.dump logfile=XXX_20181130.LOG DIRECTORY=DB_BAK

語法:
expdp 用戶名/密碼@實例名 schemas=用戶名 dumpfile=導出dump文件名.dump logfile=導出日誌文件名.LOG DIRECTORY=DB_BAK

導入數據庫

步驟1、導入前,先刪除帳號(plsql狀態下)

drop user XXX cascade;

格式:
drop user 用戶名 cascade;

注:若刪除不掉,需先刪除全部會話!!!sql

select username, sid, serial# from v$session where username='XXX' --找到用戶SESSION

格式:
select username, sid, serial# from v$session where username='用戶名' --找到用戶SESSION

注:如有多條會員,需批量刪除

alter system kill session '249,57377' --殺掉用戶SESSION 'sid,serial#'
alter system kill session '250,57376' --殺掉用戶SESSION 'sid,serial#'
alter system kill session '251,57375' --殺掉用戶SESSION 'sid,serial#'

格式:
alter system kill session 'sid,serial' 

步驟2、建立帳號,賦予權限(plsql狀態下)

create user XXX identified by XXX default tablespace USERS
temporary tablespace TEMP
profile DEFAULT;

-- Grant/Revoke role privileges 
grant connect to XXX;
grant dba to XXX;
grant resource to XXX;

-- Grant/Revoke system privileges 
grant alter any sequence to XXX;
grant alter any table to XXX;
grant alter any trigger to XXX;
grant change notification to XXX;
grant create any procedure to XXX;
grant create any sequence to XXX;
grant create any table to XXX;
grant create any type to XXX;
grant create any view to XXX;
grant unlimited tablespace to XXX;

--------------------------------------------------------------
格式:
create user 用戶名 identified by 密碼 default tablespace USERS
temporary tablespace TEMP
profile DEFAULT;

-- Grant/Revoke role privileges 
grant connect to 用戶名;
grant dba to 用戶名;
grant resource to 用戶名;

-- Grant/Revoke system privileges 
grant alter any sequence to 用戶名;
grant alter any table to 用戶名;
grant alter any trigger to 用戶名;
grant change notification to 用戶名;
grant create any procedure to 用戶名;
grant create any sequence to 用戶名;
grant create any table to 用戶名;
grant create any type to 用戶名;
grant create any view to 用戶名;
grant unlimited tablespace to 用戶名;

步驟3、導入數據(cmd狀態下)

impdp XXX/XXX DIRECTORY=db_bak DUMPFILE=XXX.dump logfile=XXX.log REMAP_SCHEMA=XXX:XXX remap_tablespace=XXX:XXX

格式
impdp 用戶名/密碼 DIRECTORY=db_bak DUMPFILE=備份文件名.dump logfile=備份日誌文件名.log REMAP_SCHEMA=導出用戶名:導入用戶名 remap_tablespace=導出表空間:導入表空間
相關文章
相關標籤/搜索