达梦数据库备份和还原
Posted 幸福丶如此
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了达梦数据库备份和还原相关的知识,希望对你有一定的参考价值。
一、热备和热还原
1、dexp 备份(导出)
### 备份SCHEMAS
格式:dexp username/password@host:port directory=backupPath file=backFileName schemas="\\"schemaName\\"" tablespace=N drop=N log=logFile log_write=N
示例:
dexp dev/dameng123@localhost:5236 directory=./ file=currency_exp.dmp schemas="\\"currency_company\\"" tablespace=N drop=N log=log_exp_currency.log log_write=N
## 备份TABLES
格式:dexp username/password@host:port directory=backupPath file=backFileName tables="\\"schemaName\\""."\\"tableName\\"" tablespace=N drop=N log=logFile log_write=N
示例:
dexp dev/dameng123@localhost:5236 directory=./ file=currency_exp.dmp tables="\\"currency_company\\"""\\"COMMERCIAL_TENANT\\"" tablespace=N drop=N log=log_exp_currency.log log_write=N
2、 dimp 还原(导入)
### 还原SCHEMAS
格式:dimp username/password@host:port directory=backupPath file=backFileName schemas="\\"schemaName\\"" IGNORE=N COMPILE=Y INDEXFIRST=N TABLE_FIRST=N COMMIT_ROWS=10000 FAST_LOAD=N log=logFile log_write=N
示例:
dimp dev/dameng123@localhost:5236 directory=./ file=currency_exp.dmp schemas="\\"currency_company\\"" GNORE=N COMPILE=Y INDEXFIRST=N TABLE_FIRST=N COMMIT_ROWS=10000 FAST_LOAD=N log=log_exp_currency.log log_write=N
## 还原TABLES
格式:dimp username/password@host:port directory=backupPath file=backFileName tables="\\"schemaName\\""."\\"tableName\\"" GNORE=N COMPILE=Y INDEXFIRST=N TABLE_FIRST=N COMMIT_ROWS=10000 FAST_LOAD=N log=logFile log_write=N
示例:
dimp dev/dameng123@localhost:5236 directory=./ file=currency_exp.dmp tables="\\"currency_company\\"""\\"COMMERCIAL_TENANT\\"" GNORE=N COMPILE=Y INDEXFIRST=N TABLE_FIRST=N COMMIT_ROWS=10000 FAST_LOAD=N log=log_exp_currency.log log_write=N
扩展(将一个SCHEMA中的table还原到另一个SCHEMA中):
创建新用户TEST2,并给用户赋管理员权限
create user TEST2 identified by TEST123456 default tablespace tbs1;
grant dba to TEST2;
新用户创建好了,我们开始执行导入操作,TEST1导入到TEST2
dimp TEST1/TEST123456 file=db_bak.dmp ignore=y remap_schema=TEST1:TEST2
二、物理备份和还原
1、backup 备份
① 首先在配置文件 dm.ini 中修改备份的路径参数(BAK_PATH)
② 登录数据库 disql username/password
③ 备份命令
##全库备份
backup database full to full202106242200 compressed ; 或者
backup database full to "DB_dm1_FULL_2021_06_24_22_00_00" backupset 'DB_dm1_FULL_2021_06_24_22_00_00' backupinfo '/dm/dmbak/';
##全库增量备份
backup database increment to "DB_dm1_INCRE_2021_06_24_23_00_00" backupset 'DB_dm1_INCRE_2021_06_24_23_00_00' backupinfo '/dm/dmbak/';
##备份表
backup table "TEST"."TEST" to "TAB_TEST_TEST_2021_06_24_22_10_00" backupset 'TAB_TEST_TEST_2021_06_24_22_10_00' backupinfo '/dm/dmbak/';
##备份表空间
backup tablespace "MAIN" full to "TS_MAIN_FULL_2021_06_24_22_30_00" backupset 'TS_MAIN_FULL_2021_06_24_22_30_00' backupinfo '/dm/dmbak/';
##备份所有归档日志
backup archivelog all to "ARCH_2021_06_24_23_59_50" backupset 'ARCH_2021_06_24_23_59_50' backupinfo '/dm/dmbak/';
##备份归档日志以LSN between
backup archivelog lsn between 12 and 23 to "ARCH_2021_06_24_23_59_50" backupset 'ARCH_2021_06_24_23_59_50' backupinfo '/dm/dmbak/';
##备份归档日志以 from lsn
backup archivelog from lsn 12 to "ARCH_2021_06_24_23_59_50" backupset 'ARCH_2021_06_24_23_59_50' backupinfo '/dm/dmbak/';
##备份归档日志以 from time
backup archivelog from time '2021-05-24 00:00:00' to "ARCH_2021_06_24_23_59_50" backupset 'ARCH_2021_06_24_23_59_50' backupinfo '/dm/dmbak/';
##备份归档日志以 time between
backup archivelog time between '2021-05-24 00:00:00' and '2021-06-24 23:59:50' to "ARCH_2021_06_24_23_59_50" backupset 'ARCH_2021_06_24_23_59_50' backupinfo '/dm/dmbak/';
扩展:
如果在执行backup备份的时候,报错,可能是归档参数没有开启,按照以下步骤即可开启归档参数
#查看归档信息
SQL> select arch_mode from v$database ; #必须是Y
如果是N(归档的配置主要涉及两个文件,一个是dm_ini和dmarch_ini,只能从目录下去找这两个文件)
① 修改 dm.ini 参数中的ARCH_INI=1表是归档,0是非归档 #修改完成后重启达梦数据库 systemctl restart DmServicedm1.service
② chown -R dmdba:dinstall dmarch.ini
SQL> alter database mount;
SQL> alter database add archivelog 'type=local,dest=/data/services/dm8/data/DAMENG/arch,file_size=64,space_limit=0';
SQL> alter database archivelog;
SQL> alter database open;
SQL> 重新执行backup命令即可
2、restore 还原(恢复)
① 停止数据库
systemctl stop DmServicedm1.service
② 打开console
tool/console
③ 恢复脚本
RESTORE DATABASE TO '/dm/dmdbms/data/dm1' OVERWRITE FROM BACKUPSET '/dm/dmdbms/data/dm1/bak/DB_dm1_FULL_2021_06_24_22_00_00' DEVICE TYPE disk;
RECOVER DATABASE '/dm/dmdbms/data/dm1/dm.ini' FROM BACKUPSET '/dm/dmdbms/data/dm1/bak/DB_dm1_FULL_2021_06_24_22_00_00' DEVICE TYPE disk;
RECOVER DATABASE '/dm/dmdbms/data/dm1/dm.ini' UPDATE DB_MAGIC;
④ 启动数据库
systemctl start DmServicedm1.service
扩展:
① 命令行执行恢复操作(USE_AP的参数2表示还原)
dmrman USE_AP=2 CTLSTMT="RESTORE DATABASE TO '/dm/dmdbms/data/dm1' OVERWRITE FROM BACKUPSET '/dm/dmdbms/data/dm1/bak/DB_dm1_FULL_2021_06_24_22_00_00' DEVICE TYPE disk;"
dmrman USE_AP=2 CTLSTMT="RECOVER DATABASE '/dm/dmdbms/data/dm1/dm.ini' FROM BACKUPSET '/dm/dmdbms/data/dm1/bak/DB_dm1_FULL_2021_06_24_22_00_00' DEVICE TYPE disk;"
dmrman USE_AP=2 CTLSTMT="RECOVER DATABASE '/dm/dmdbms/data/dm1/dm.ini' UPDATE DB_MAGIC;"
② 网上提到用 dmrestore ini_path=/dm/dmdbms/data/dm1/dm.ini file=xxxx.bak 这种方式还原,没有试过
以上是关于达梦数据库备份和还原的主要内容,如果未能解决你的问题,请参考以下文章