达梦数据库逻辑备份工具可以对本地或者远程数据库进行数据库级、模式级和表级的逻辑备份。备份的内容非常灵活,可以选择是否备份索引、数据行和权限,是否忽略各种约束(外键约束、非空约束、唯一约束等),在备份前可以选择生成日志文件,记录备份的过程以供查看。本文内容就是提供相关测试脚本。
备注:
1.数据库安装路径/home/dmdba;
2.实例路径为/home/dmdba/dmdbms/data;
3.在需要新建文件夹为dmpFiles,统一放dmp文件和日志文件,具体路径为/home/dmdba/dmdbms/data/DAMENG/dmpFiles;
一.库级导出导入
建表、建用户;
执行数据库级导出;
删除表;
执行全数据库导入;
查询表数据
spool /home/dmdba/dmdbms/test.sql
set autocommit on;
set lineshow off;
set linesize 800;
CONN SYSDBA/SYSDBA;
CREATE USER U1 IDENTIFIED BY "Liaotao941103";
create table U1.t1 as select * from dba_objects;
host cd /home/dmdba/dmdbms/bin/
host ./dexp USERID=SYSDBA/SYSDBA FILE=db_full.dmp LOG=db_full.log FULL=Y DIRECTORY=/home/dmdba/dmdbms/data/DAMENG/dmpFiles
drop table U1.t1 purge;
host cd /home/dmdba/dmdbms/bin/
host ./dimp USERID=SYSDBA/SYSDBA FILE=db_full.dmp LOG=db_full_imp.log FULL=Y DIRECTORY=/home/dmdba/dmdbms/data/DAMENG/dmpFiles TABLE_EXISTS_ACTION=REPLACE
select count(1) from U1.t1 ;
spool off;
二.模式级导出导入
建表、建用户;
执行模式级导出;
删除表;
执行模式级导入;
查询表数据
spool /home/dmdba/dmdbms/test.sql
set autocommit on;
set lineshow off;
set linesize 800;
CONN SYSDBA/SYSDBA;
CREATE USER U1 IDENTIFIED BY "Liaotao941103";
create table U1.t1 as select * from dba_objects;
host cd /home/dmdba/dmdbms/bin/
host ./dexp USERID=SYSDBA/SYSDBA FILE=schema_u1.dmp LOG=schema_u1_exp.log schemas=u1 DIRECTORY=/home/dmdba/dmdbms/data/DAMENG/dmpFiles
drop table U1.t1 purge;
host cd /home/dmdba/dmdbms/bin/
host ./dimp USERID=SYSDBA/SYSDBA FILE=schema_u1.dmp LOG=schema_u1_imp.log schemas=u1 DIRECTORY=/home/dmdba/dmdbms/data/DAMENG/dmpFiles TABLE_EXISTS_ACTION=REPLACE
select count(1) from U1.t1 ;
spool off;
三.用户级导出导入
建表、建用户;
执行用户级导出;
删除用户及其表对象;
创建该用户,再执行用户级导入;
查询用户模式下的表数据
spool /home/dmdba/dmdbms/test.sql
set autocommit on;
set lineshow off;
set linesize 800;
CONN SYSDBA/SYSDBA;
CREATE USER U1 IDENTIFIED BY "Liaotao941103";
create table U1.t1 as select * from dba_objects;
host cd /home/dmdba/dmdbms/bin/
host ./dexp USERID=SYSDBA/SYSDBA FILE=owner_u1.dmp LOG=owner_u1_exp.log owner=u1 DIRECTORY=/home/dmdba/dmdbms/data/DAMENG/dmpFiles
drop table U1.t1 purge;
host cd /home/dmdba/dmdbms/bin/
host ./dimp USERID=SYSDBA/SYSDBA FILE=owner_u1.dmp LOG=owner_u1_imp.log owner=u1 DIRECTORY=/home/dmdba/dmdbms/data/DAMENG/dmpFiles TABLE_EXISTS_ACTION=REPLACE
select count(1) from U1.t1 ;
spool off;
四.表级导出导入
建表并插入数据;
执行表级导出;
删除表;
执行表级导入
查询表数据
spool /home/dmdba/dmdbms/test.sql
set autocommit on;
set lineshow off;
set linesize 800;
CONN SYSDBA/SYSDBA;
CREATE USER U1 IDENTIFIED BY "Liaotao941103";
create table U1.t1 as select * from dba_objects;
host cd /home/dmdba/dmdbms/bin/
host ./dexp USERID=SYSDBA/SYSDBA FILE=tables_t1.dmp LOG=tables_t1_exp.log TABLES=u1.t1 DIRECTORY=/home/dmdba/dmdbms/data/DAMENG/dmpFiles
drop table U1.t1 purge;
host cd /home/dmdba/dmdbms/bin/
host ./dimp USERID=SYSDBA/SYSDBA FILE=tables_t1.dmp LOG=tables_t1_imp.log TABLES=u1.t1 DIRECTORY=/home/dmdba/dmdbms/data/DAMENG/dmpFiles TABLE_EXISTS_ACTION=REPLACE
select count(1) from U1.t1 ;
spool off;
五.表的子集导出导入
建表并插入数据;
执行表级导出,导出满足某个查询条件的结果集;
删除表;
执行表级导入
查询表数据
spool /home/dmdba/dmdbms/test.sql
set autocommit on;
set lineshow off;
set linesize 800;
CONN SYSDBA/SYSDBA;
CREATE USER U1 IDENTIFIED BY "Liaotao941103";
create table U1.t1 as select * from dba_objects;
host cd /home/dmdba/dmdbms/bin/
host ./dexp USERID=SYSDBA/SYSDBA FILE=tables_t1_where.dmp LOG=tables_t1_where_exp.log TABLES=u1.t1 DIRECTORY=/home/dmdba/dmdbms/data/DAMENG/dmpFiles QUERY="WHERE OWNER='SYSDBA'"
drop table U1.t1 purge;
host cd /home/dmdba/dmdbms/bin/
host ./dimp USERID=SYSDBA/SYSDBA FILE=tables_t1_where.dmp LOG=tables_t1_where_imp.log TABLES=u1.t1 DIRECTORY=/home/dmdba/dmdbms/data/DAMENG/dmpFiles TABLE_EXISTS_ACTION=REPLACE
select count(1) from U1.t1 a where a.OWNER='SYSDBA';
spool off;
六.约束导出导入
建带约束的表并插入数据;
执行表级导出,导出表的约束;
删除表;
执行表级导入,导入表的约束
查询表定义
spool /home/dmdba/dmdbms/test.sql
CONN SYSDBA/SYSDBA;
set autocommit on;
set lineshow off;
set linesize 800;
create table tab5 (id int, name char(10) unique);
alter table tab5 add constraint con_id primary key(id);
insert into tab5 values (1,'TEST1'),(2,'TEST2');
select a.TABLE_NAME,a.CONSTRAINT_TYPE,a.CONSTRAINT_NAME from USER_CONSTRAINTS a where a.TABLE_NAME='TAB5';
host cd /home/dmdba/dmdbms/bin/
host ./dexp USERID=SYSDBA/SYSDBA FILE=tab5.dmp LOG=tab5_exp.log TABLES=tab5 DIRECTORY=/home/dmdba/dmdbms/data/DAMENG/dmpFiles
drop table tab5 purge;
host cd /home/dmdba/dmdbms/bin/
host ./dimp USERID=SYSDBA/SYSDBA FILE=tab5.dmp LOG=tab5_imp.log TABLES=tab5 DIRECTORY=/home/dmdba/dmdbms/data/DAMENG/dmpFiles TABLE_EXISTS_ACTION=REPLACE
select count(1) from tab5 a;
select a.TABLE_NAME,a.CONSTRAINT_TYPE,a.CONSTRAINT_NAME from USER_CONSTRAINTS a where a.TABLE_NAME='TAB5';
spool off;
七.索引导出导入
建表插入数据,并建索引;
执行表级导出,导出表的索引;
删除表;
执行表级导入,导入表的索引
通过查询系统表查询表上索引的ID和定义
spool /home/dmdba/dmdbms/test.sql
CONN SYSDBA/SYSDBA;
set autocommit on;
set lineshow off;
set linesize 800;
create table tab5 (id int, name char(10));
create index inx_name on tab5(name);
select a.INDEX_NAME from USER_INDEXES a where a.TABLE_NAME='TAB5' and a.INDEX_NAME='INX_NAME';
host cd /home/dmdba/dmdbms/bin/
host ./dexp USERID=SYSDBA/SYSDBA FILE=tab5.dmp LOG=tab5_exp.log TABLES=tab5 DIRECTORY=/home/dmdba/dmdbms/data/DAMENG/dmpFiles
drop table tab5 purge;
host cd /home/dmdba/dmdbms/bin/
host ./dimp USERID=SYSDBA/SYSDBA FILE=tab5.dmp LOG=tab5_imp.log TABLES=tab5 DIRECTORY=/home/dmdba/dmdbms/data/DAMENG/dmpFiles TABLE_EXISTS_ACTION=REPLACE
select a.INDEX_NAME from USER_INDEXES a where a.TABLE_NAME='TAB5' and a.INDEX_NAME='INX_NAME';
spool off;
八.权限导出导入
spool /home/dmdba/dmdbms/test.sql
CONN SYSDBA/SYSDBA;
set autocommit on;
set lineshow off;
set linesize 800;
create table tab5 (id int, name char(10));
create user liaotao IDENTIFIED by "Liaotao941103";
grant select,UPDATE ON tab5 TO liaotao;
SELECT GRANTEE,GRANTOR,TABLE_NAME,PRIVILEGE FROM DBA_TAB_PRIVS WHERE GRANTEE = 'LIAOTAO';
host cd /home/dmdba/dmdbms/bin/
host ./dexp USERID=SYSDBA/SYSDBA FILE=TEST01.dmp LOG=TEST01_exp.log FULL=Y DIRECTORY=/home/dmdba/dmdbms/data/DAMENG/dmpFiles
drop table tab5 purge;
host cd /home/dmdba/dmdbms/bin/
host ./dimp USERID=SYSDBA/SYSDBA FILE=TEST01.dmp LOG=TEST01_imp.log FULL=Y DIRECTORY=/home/dmdba/dmdbms/data/DAMENG/dmpFiles TABLE_EXISTS_ACTION=REPLACE
SELECT GRANTEE,GRANTOR,TABLE_NAME,PRIVILEGE FROM DBA_TAB_PRIVS WHERE GRANTEE = 'LIAOTAO';
spool off;
九.触发器导出导入
建表并建表级触发器;
建表并建事件触发器;
执行数据库级导出,导出触发器;
删除触发器和表;
执行数据库级导入,导入触发器;
验证表级触发器和事件触发器是否导入成功
spool /home/dmdba/dmdbms/test.sql
CONN SYSDBA/SYSDBA;
set autocommit on;
set lineshow off;
set linesize 800;
--建表并建表级触发器;
create table employees
(
employee_id dec(6) not null,
employee_name varchar(20) not null,
salary dec(8,2),
primary key(employee_id)
);
create table change_table
(
old_salary dec(8,2),
new_salary dec(8,2)
);
create or replace trigger trigger1
before update of salary on employees
for each row
begin
insert into change_table values(old.salary,:new.salary);
END;
/
--建表并建事件触发器;
create table tab1( name varchar(20), time1 date);
create or replace trigger trigger2
after drop on database
begin
insert into tab1 values(user,sysdate);
end;
/
--执行数据库级导出,导出触发器;
host cd /home/dmdba/dmdbms/bin/
host ./dexp USERID=SYSDBA/SYSDBA FILE=db_full_0722.dmp LOG=db_full_0722_exp.log FULL=Y DIRECTORY=/home/dmdba/dmdbms/data/DAMENG/dmpFiles
--删除触发器和表
drop trigger trigger1;
drop trigger trigger2;
drop table employees purge;
drop table change_table purge;
drop table tab1 purge;
--执行数据库级导入,导入触发器;
host cd /home/dmdba/dmdbms/bin/
host ./dimp USERID=SYSDBA/SYSDBA FILE=db_full_0722.dmp LOG=db_full_0722_imp.log FULL=Y DIRECTORY=/home/dmdba/dmdbms/data/DAMENG/dmpFiles
--验证表级触发器和事件触发器是否导入成功
insert into employees values(1, '关羽羽', 1000),(2, '关羽', 2000);
commit;
select * from change_table;
update employees set salary=888;
commit;
select * from change_table;
create table t (id int);
drop table t purge;
select * from tab1;
spool off;
十.导出数据压缩
建表并插入10万条数据;
全库逻辑导出,不带压缩数据选项;
全库逻辑导出,带压缩数据选项;
比较不压缩生成的文件和带压缩生成的文件的大小;
删除表;
用压缩生成的文件执行导入;
验证表及数据是否导入成功
spool /home/dmdba/dmdbms/test.sql
CONN SYSDBA/SYSDBA;
set autocommit on;
set lineshow off;
set linesize 800;
create table test_tab as
select
sys_guid() bh ,
substr(sys_guid(), 1, 20) sfzhm ,
sys_guid() xm ,
case when mod(to_number(trunc(dbms_random.value(0, 2))), 2)=0 then 'M' else 'F' end xb,
SYSDATE shengri ,
dbms_random.value(10000, 9999999) dhhm ,
dbms_random.value(1, 999) xz ,
trunc(dbms_random.value(10000, 10000222))
||'@qq.com' email ,
null zhaopian ,
'what' jianli ,
dbms_random.value(1, 999) c1,
dbms_random.value(1, 999) c2,
sysdate c3 ,
sysdate c4
from
dual connect by rownum<=100000;
host cd /home/dmdba/dmdbms/bin/
host ./dexp USERID=SYSDBA/SYSDBA FILE=db_full_nocompress.dmp LOG=db_full_nocompress_exp.log FULL=Y DIRECTORY=/home/dmdba/dmdbms/data/DAMENG/dmpFiles COMPRESS=N
host cd /home/dmdba/dmdbms/bin/
host ./dexp USERID=SYSDBA/SYSDBA FILE=db_full_compress.dmp LOG=db_full_compress_exp.log FULL=Y DIRECTORY=/home/dmdba/dmdbms/data/DAMENG/dmpFiles COMPRESS=Y
drop table test_tab purge;
host cd /home/dmdba/dmdbms/bin/
host ./dimp USERID=SYSDBA/SYSDBA FILE=db_full_compress.dmp LOG=db_full_compress_imp.log FULL=Y DIRECTORY=/home/dmdba/dmdbms/data/DAMENG/dmpFiles table_exists_action=replace
select count(1) from test_tab;
spool off;