Backup and recover user roles oracle - oracle

Is it possible to backup user roles (not the whole DB) with a script or is there some backup function in oracle 11 ?
I want to backup the users and restore them later by script, this should be used as a backup against mis configurations.
DBA_USERS
DBA_ROLES
DBA_ROLE_PRIVS
ROLE_TAB_PRIVS
SESSION_PRIVS
SESSION_ROLES
TABLE_PRIVILEGES
Currently, i can query or create views of the tables that store most of the user settings. But creating roles and granting right on tables from that source involves a lot of text transformation.
view > store to file > concat convert them to create/grant > execute script on commandline
Is there another way to export AND import user rights and roles and the way they are assigned to tables ? Or can i simply backup and restore the tables mentioned in this question without braking the DB?

You can use datapump to backup them:
expdp cyrille/*******#//localhost:1521/orclpdb \
dumpfile=exp_USER_GRANTS.dmp \
logfile=exp_USER_GRANTS.log \
directory=MY_DIRECTORY \
INCLUDE=GRANT \
INCLUDE=OBJECT_GRANT \
INCLUDE=SYSTEM_GRANT \
INCLUDE=ROLE_GRANT \
INCLUDE=USER \
full=y
Export: Release 12.2.0.1.0 - Production on Wed Sep 27 11:35:35 2017
Copyright (c) 1982, 2017, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
Starting "*****"."SYS_EXPORT_FULL_01": cyrille/********#//localhost:1521/orclpdb dumpfile=exp_USER_GRANTS.dmp logfile=exp_USER_GRANTS.log directory=MY_DIRECTORY INCLUDE=GRANT INCLUDE=OBJECT_GRANT INCLUDE=SYSTEM_GRANT INCLUDE=ROLE_GRANT INCLUDE=USER full=y
Processing object type DATABASE_EXPORT/SYS_USER/USER
Processing object type DATABASE_EXPORT/SCHEMA/USER
Processing object type DATABASE_EXPORT/GRANT/SYSTEM_GRANT/PROC_SYSTEM_GRANT
Processing object type DATABASE_EXPORT/SCHEMA/GRANT/SYSTEM_GRANT
Processing object type DATABASE_EXPORT/SCHEMA/ROLE_GRANT
Processing object type DATABASE_EXPORT/SCHEMA/SEQUENCE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type DATABASE_EXPORT/DIRECTORY/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type DATABASE_EXPORT/SYSTEM_PROCOBJACT/GRANT/PROCOBJ_GRANT
Processing object type DATABASE_EXPORT/SCHEMA/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type DATABASE_EXPORT/SCHEMA/FUNCTION/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type DATABASE_EXPORT/SCHEMA/PROCEDURE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type DATABASE_EXPORT/SCHEMA/JAVA_CLASS/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type DATABASE_EXPORT/SCHEMA/JAVA_RESOURCE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type DATABASE_EXPORT/SCHEMA/OPERATOR/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type DATABASE_EXPORT/SCHEMA/INDEXTYPE/GRANT/OWNER_GRANT/OBJECT_GRANT
Master table "CYRILLE"."SYS_EXPORT_FULL_01" successfully loaded/unloaded
******************************************************************************
Dump file set for CYRILLE.SYS_EXPORT_FULL_01 is:
/u01/exp_USER_GRANTS.dmp
From that dump you can also generate a script using the sqlfile parameter:
impdp cyrille/******#//localhost:1521/orclpdb dumpfile=exp_USER_GRANTS.dmp logfile=imp_USER_GRANTS.log directory=MY_DIRECTORY sqlfile=my_script.sql
this will generate a sql file with the scripts.

Related

Reading the same Oracle external table multiple times with the different underlying file content raises the error KUP-05011: Size of file has changed

I try to read the same Oracle external table multiple times with the different underlying file content.
I use the java app with the code fragment like:
int sptepn = 1;
for (File file: files) {
log_step(stepn++);
copy_file_to_oracle_directory(file); // copy myfileN.txt to myfile.txt
call_oracle_sp_read_file();
}
In the pl/sql procedure I use code fragment like:
procedure read_file() is
cursor ext_cu is
select * from ext_table;
begin
for ext_rec in ext_cu loop
-- do something with ext_rec.*
end loop;
end read_file;
On the one group of db-servers this pl/sql procedure works properly. But on the other group of db-servers the call of this pl/sql procedure on the step 2 (of the java procedure) raises the error:
29913-ORA-29913: error in executing ODCIEXTTABLEFETCH callout: ORA-29400: data cartridge error: KUP-05011: Size of file myfile.txt in directory /mydir has changed from 12345 to 67890.
Where the myfile1.txt has the size 12345 and the myfile2.txt has the size 67890.
What is the problem?
The affected server has the version:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
PL/SQL Release 12.2.0.1.0 - Production
"CORE 12.2.0.1.0 Production"
TNS for IBM/AIX RISC System/6000: Version 12.2.0.1.0 - Production
NLSRTL Version 12.2.0.1.0 - Production
How about telling Oracle that you've changed the file? Instead of copy myfileN.txt to myfile.txt I'd try to keep the number in the filenames and alter the external table:
ALTER TABLE ext_table LOCATION ("MYDIR":'myfile1.txt');
read_file();
ALTER TABLE ext_table LOCATION ("MYDIR":'myfile2.txt');
read_file();
...

I can not import oracle database from another user

I exported oracle database from user c##cuongnguyen. After i exported, i had dump file , So i want to import this file to another user: mbbank
So i run this syntax:
**
imp mbbank/xxxxxx FROMUSER=c##cuongnguyen TOUSER=mbbank
file=C:\oracle\export\cuongnguyen-user.DMP;
**
I recieved the response, it's about character.
Import: Release 12.2.0.1.0 - Production on Wed Apr 22 19:30:22 2020
Copyright (c) 1982, 2017, Oracle and/or its affiliates. All rights
reserved.
Connected to: Oracle Database 12c Enterprise Edition Release
12.2.0.1.0 - 64bit Production
Export file created by EXPORT:V12.02.00 via conventional path
Warning: the objects were exported by C##CUONGNGUYEN, not by you
import done in WE8MSWIN1252 character set and AL16UTF16 NCHAR
character set import server uses AL32UTF8 character set (possible
charset conversion) IMP-00085: multiple input files specified for
unbounded export file IMP-00000: Import terminated unsuccessfully
If you have any ideas, please tell me know, Many thanks
Have you tried to run the command inside the directory where the dmp file is located? Or even put the directory path between ''?
cd C:\oracle\export
imp mbbank/xxxxxx FROMUSER=c##cuongnguyen TOUSER=mbbank file=cuongnguyen-user.DMP
or
imp mbbank/xxxxxx FROMUSER=c##cuongnguyen TOUSER=mbbank file='C:\oracle\export\cuongnguyen-user.DMP'
Just for your knowledge, oracle has a more improved database export and import tool: expdp and impdp. And you can do it in your own way:
Creating a directory of export inside database:
$ORACLE_HOME/bin/sqlplus '/ as sysdba'
SQL> create or replace directory export as 'C:\oracle\export';
Exporting the schema:
expdp mbbank/xxxxxx dumpfile=cuongnguyen-user.DMP directory=export schemas=c##cuongnguyen
Finally importing as a new user:
impdp mbbank/xxxxxx dumpfile=cuongnguyen-user.DMP directory=export schemas=c##cuongnguyen remap_schema=c##cuongnguyen:mbbank

oracle 11g impdp returns invalid operation and object was not found

I wanted to transfer an oracle table between two users using expdp and impdp commands.
First of all, I created a directory and granted proper privileges to source and destination users:
SQL> CREATE OR REPLACE DIRECTORY TEST_DIR AS '/u01/app/oracle/oradata/temp_dirs';
Directory created.
SQL> GRANT READ, WRITE ON DIRECTORY test_dir TO SOURCE_USER;
Grant succeeded.
SQL> GRANT READ, WRITE ON DIRECTORY test_dir TO DEST_USER;
Grant succeeded.
SQL> commit;
This is my expdp command and it's output:
oracle#lab-57:~$ expdp SOURCE_USER/SOURCE_USER tables=tbl_user directory=TEST_DIR dumpfile=users.dmp logfile=exp_users.log
Export: Release 11.2.0.1.0 - Production on Tue Jul 24 16:18:27 2018
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Starting "SOURCE_USER"."SYS_EXPORT_TABLE_01": SOURCE_USER/******** tables=tbl_user directory=TEST_DIR dumpfile=users.dmp logfile=exp_users.log
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 64 KB
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
. . exported "SOURCE_USER"."TBL_USER" 8.085 KB 2 rows
Master table "SOURCE_USER"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SOURCE_USER.SYS_EXPORT_TABLE_01 is:
/u01/app/oracle/oradata/temp_dirs/users.dmp
Job "SOURCE_USER"."SYS_EXPORT_TABLE_01" successfully completed at 16:18:38
This implies the export operation was done successfully.
Then I tried to import it using impdp as following:
oracle#lab-57:~$ impdp DEST_USER/DEST_USER tables=TBL_USER directory=TEST_DIR dumpfile=users.dmp logfile=imp_users.log
Import: Release 11.2.0.1.0 - Production on Tue Jul 24 17:26:58 2018
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
ORA-39002: invalid operation
ORA-39166: Object DEST_USER.TBL_USER was not found.
I also tried tables=DEST_USER.TBL_USER and content=data_only; both returned same error.
As you're exporting as SOURCE_USER and importing as DEST_USER, the REMAP_SCHEMA parameter might help. (I'll split the command into several rows for easier reading)
impdp DEST_USER/DEST_USER tables=TBL_USER directory=TEST_DIR
dumpfile=users.dmp logfile=imp_users.log
remap_schema=source_user:dest_user --> this
If you're on 11.2, you might hit a bug which won't allow you to do that (see My Oracle Support for more info). In that case, you can still perform import, but you'll have to omit the TABLES parameter, i.e.
TABLES was in this gap
|
impdp DEST_USER/DEST_USER v directory=TEST_DIR
dumpfile=users.dmp logfile=imp_users.log
remap_schema=source_user:dest_user
Simple answer :
Create import : keep the dump ready.
Two option to restore the dump , consider complete dump or specific table.
For table specific :- Table name = MONTHS_DAY
impdp DBA_USER/DBA_Password tables = SOURCE_SCHEMA.MONTHS_DAY directory=AKSHAY_SPM_EDIR dumpfile=MONTHS_DAY.dmp logfile=impdpMONTHS_DAY.log remap_schema = SOURCE_SCHEMA:DESTINATION_SCHEMA
For complete import :
impdp DBA_USER/DBA_Password directory = AKSHAY_SPM_EDIR dumpfile=MONTHS_DAY.dmp logfile=impdpMONTHS_DAY.log remap_schema = SOURCE_SCHEMA:DESTINATION_SCHEMA
Make sure, directory is accessible as dump is present there

VARCHAR2 field and NVARCHAR2 exsit in the same table,the former is correct,the latter was garbled

the database A field Type is:
select column_name,data_type From all_tab_columns ;
COLUMN_NAME DATA_TYPE
------------------------------------
NAME VARCHAR2
ID_ISSUE_PLACE VARCHAR2
NATIONALITY NVARCHAR2
I want to export a table with expdp:
expdp abc/123 tables=A.CUST:PARTNUM_0 exclude=grant,index,contraint,statistics,trigger DIRECTORY=DATATMP filesize=100M dumpfile=expdp_%U.dmp parallel=4 cluster=N COMPRESSION=DATA_ONLY
Then,I import the dmp file to my local oracle,the field 'NATIONALITY' is correct , but the other field is garbled.
impdp abc/123 DIRECTORY=DATATMP DUMPFILE=expdp_01.dmp
[oracle#db01 tmp]$ impdp bi71/bi71 DIRECTORY=datatmp
DUMPFILE=expdp_01.dmp
Import: Release 11.2.0.1.0 - Production on Tue Sep 6 11:18:05 2016
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights
reserved.
Connected to: Oracle Database 11g Enterprise Edition Release
11.2.0.1.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options Master table
"A"."SYS_IMPORT_FULL_01" successfully loaded/unloaded Starting
"A"."SYS_IMPORT_FULL_01": abc/******** DIRECTORY=datatmp
DUMPFILE=expdp_01.dmp Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA . . imported
"A"."CUST":"PARTNUM_0" 5.990 MB 78037 rows
Statement:Not a windows client problems,because the vietnamese charecter has been displayed properly.Here with reason screenshot windows client, is to make the results better clarity.
screenshot
I found a rule,once vietnamese nationality in this line appears,the field NAME,ID_ISSUE_PLACE are garbled.
How to resolve? thanks(^_^).

Cannot import Oracle dump: IMP-00033: Warning: Table not found in export file

I have an Oracle dump (.dmp) that I want to import into my local Oracle instance. But when I do a full import, it fails with
IMP-00033: Warning: Table not found in export file.
Some facts:
using imp system/pass ignore=yes tables=(t1,t2,t3,..)
export is successful (according to log)
export is done in Oracle Database 10g Release 10.1.0.4.0, import is done in Oracle Database 11g Express Edition Release 11.2.0.2.0
when I do show=y DDL is shown for all the tables on the list
before printing the 00033 warning, there is IMP-00009: abnormal end of export file
when I do a full import (without specifying table names), there are all sorts of errors, including
"IMP-00003: ORACLE error 1435 encountered",
"ORA-01435: user does not exist",
"ORA-01031: insufficient privileges".
These errors do not appear when I specify the table names.
How to do the import?
If you got IMP-00009: abnormal end of export file this probably means that the import file do not have the expected format, or worse that it is incomplete! (but in that last case you would not be able to import specifying table names).
Since the database versions are so different, you should make sure that your import script specifies the format of incoming data with version=10.1.0 in your imp command

Resources