达梦数据库物理备份与还原
1 备份恢复工具
注:本文适用于达梦8版本。
备份工具 | 备份方式 | 恢复工具 | 备注 |
dexp | 逻辑备份 | dimp | 用于数据迁移或异机还原 |
disql | 物理备份 | disql | 热备 1.需要开启DmAPService服务 2.需要开启归档 3.一般用来备份,恢复只能恢复到表级别 |
dmrman | 物理备份 | dmrman | 冷备 1.需要开启DmAPService服务 2.需要开启归档 3.需要数据库处于脱机状态,一般用来还原恢复 |
2 配置归档
物理备份须要数据库开启归档模式
关闭数据库
注:SYSDBA用户密码以现场实际密码为准
$disql sysdba/SYSDBA SQL> shutdown immediate;
主配置文件打开归档(具体路径以现场实际路径为准)
$cat /kingdee/dm8/data/cosmic/dm.ini|grep ARCH_INI ARCH_INI = 1 #dmarch.ini
配置备份路径
$cat /kingdee/dm8/data/cosmic/dm.ini|grep BAK_PATH -w BAK_PATH = /kingdee/dm8/data/backup
配置归档参数
$cat /kingdee/dm8/data/cosmic/dmarch.ini [ARCHIVE_LOCAL1] ARCH_TYPE=LOCAL ARCH_DEST=/kingdee/dm8/data/cosmic/archive_log ARCH_FILE_SIZE=2048 ARCH_SPACE_LIMIT = 102400
启动数据库
#systemctl start DmServicecosmic
检查配置
SQL>select * from V$ARCH_STATUS;
3 备份
备注:由于dmrman工具只能实现冷备,即数据库需要处在脱机状态,在实际情况中,一般使用联机备份的方式,即通过登录disql工具使用sql语句进行。
3.1 数据库备份
语法
BACKUP DATABASE[<备份类型>][TO <备份名>] [BACKUPSET '<备份集路径>'] [DEVICE TYPE <介质类型> [PARMS '<介质参数>']]
[BACKUPINFO '<备份描述>'] [MAXPIECESIZE <备份片限制大小>] [COMPRESSED [LEVEL <压缩级别>]]
其中备份类型分为全备和增量备份,备份前设置归档模式
全备示例
$disql sysdba/SYSDBA SQL> backup database full to "DB_COSMIC_FULL_2023-08-23" backupset 'all_cosmic' BACKUPINFO '完全备份' MAXPIECESIZE 128 compressed level 1 PARALLEL 2;
增备示例
注:COSMIC_FI用户密码以现场实际密码为准
SQL> conn COSMIC_FI/COSMIC_FI SQL> create table t2 as select * from t1; SQL>backup database increment to "DB_COSMIC_INCRE_2023-08 23" backupset 'incr_cosmic_1' BACKUPINFO '增量备份2' MAXPIECESIZE 128 compressed level 1 PARALLEL 2;
3.2 备份归档日志
语法
BACKUP <ARCHIVE LOG |ARCHIVELOG> [ALL | [FROM LSN <lsn>]| [UNTIL LSN <lsn>]|[LSN BETWEEN <lsn> AND <lsn>] | [FROM TIME '<time>']|[UNTIL TIME '<time>']|[TIME BETWEEN'<time>'> AND '<time>']][<notBackedUpSpec>][DELETE INPUT][TO <备份名>][<备份集子句>];
备份示例
SQL> BACKUP ARCHIVE LOG ALL to "DB_COSMIC_ARCH_2023-08 23" BACKUPSET 'arch_cosmic_01' BACKUPINFO '归档备份' MAXPIECESIZE 128 compressed level 1 PARALLEL 2;
3.3 备份表空间
语法
BACKUP TABLESPACE <表空间名> [FULL | INCREMENT <increment_statement>] [TO <备份名>] [BACKUPSET '<备份集路径>'][DEVICE TYPE <介质类型> [PARMS '<介质参数>']] [BACKUPINFO '<备份描述>'] [MAXPIECESIZE <备份片限制大小>]
全备示例
SQL> BACKUP TABLESPACE COSMIC_MC_D FULL to "TBS_COSMIC_MC_2023-08-23" BACKUPSET 'tbs_cosmic_mc_full' BACKUPINFO 'biz表空间全备' MAXPIECESIZE 128 compressed level 1 PARALLEL 2;
增备示例
SQL> BACKUP TABLESPACE COSMIC_MC_D INCREMENT WITH BACKUPDIR '/kingdee/dm8/data/backup' to "TBS_COSMIC_MC_incr_2023-08-23" BACKUPSET 'tbs_cosmic_mc_incr' BACKUPINFO 'biz表空间增备' MAXPIECESIZE 128 compressed level 1 PARALLEL 2;
3.4 备份表
语法
BACKUP TABLE <表名> [TO <备份名>] [BACKUPSET '<备份集路径>'] [DEVICE TYPE <介质类型> [PARMS '<介质参数>']] [BACKUPINFO '<备份描述>'] [MAXPIECESIZE <备份片限制大小>]
备份示例
SQL> BACKUP TABLE COSMIC_FI.T1 to "TB_COSMIC_FI_2023" BACKUPSET 'tb_cosmic_fi_t1' BACKUPINFO 't1表备份' MAXPIECESIZE 128 compressed level 1;
4 恢复
4.1 数据准备
建表并插入数据
SQL> conn cosmic_fi/COSMIC_FI SQL> create table t1(id int,name varchar2(20)); SQL> insert into t1 values(1,'zhangshan'); SQL> commit;
进行一次数据库全备
SQL> backup database full to "DB_COSMIC_FULL_2023-08-23" backupset 'all_cosmic' BACKUPINFO '完全备份' MAXPIECESIZE 128 compressed level 1 PARALLEL 2;
增量备份
SQL> set time on SQL> create table t2 as select * from t1; SQL> backup database increment to "DB_COSMIC_INCRE_2023-08-23" backupset 'incr_cosmic_1' BACKUPINFO '增量备份1' MAXPIECESIZE 128 compressed level 1 PARALLEL 2;
再次新增表
#第二次新增表 SQL> conn cosmic_fi/COSMIC_FI SQL> create table t3 as select * from t1; #触发完全检查点(如正常停数据库,会自动触发) SQL> checkpoint(100);
归档备份
SQL> BACKUP ARCHIVE LOG ALL to "DB_COSMIC_ARCH_2023-08-23" BACKUPSET 'arch_cosmic_01' BACKUPINFO '归档备份' MAXPIECESIZE 128 compressed level 1 PARALLEL 2;
4.2 恢复到最新状态
停止数据库
SQL> shutdown immediate;
用最新数据库备份进行恢复
以下所有rman命令,均在dmdba用户下,以dmram命令进入:
$dmrman
a) 检验备份集是否正确
RMAN>CHECK BACKUPSET '/kingdee/dm8/data/backup/incr_cosmic_1'; b) 还原数据库 RMAN> RESTORE DATABASE '/kingdee/dm8/data/cosmic/dm.ini' FROM BACKUPSET '/kingdee/dm8/data/backup/incr_cosmic_1';
还原归档日志
a) 指定还原的目标归档日志目录(为演示,本次在归档目录下创建了一个子目录arch_cosmic_01和一个备份目录bak,将当前归档移到备份,子目录用来做还原用): $mkdir -p /kingdee/dm8/data/cosmic/archive_log/{arch_cosmic_01,bak} $cd /kingdee/dm8/data/cosmic/archive_log $mv ARCHIVE*log bak RMAN> CHECK BACKUPSET '/kingdee/dm8/data/backup/arch_cosmic_01'; b) 还原归档至某个目录(步骤b和c任选一个) RMAN> RESTORE ARCHIVE LOG FROM BACKUPSET '/kingdee/dm8/data/backup/arch_cosmic_01' TO ARCHIVEDIR '/kingdee/dm8/data/cosmic/archive_log/arch_cosmic_01' OVERWRITE 2; c) 还原到目标库的 dm.ini 指定的归档路径(OVERWRITE 2表示存在同名归档立即报错返回,终止还原) RMAN> RESTORE ARCHIVE LOG FROM BACKUPSET '/kingdee/dm8/data/backup/arch_cosmic_01' TO DATABASE '/kingdee/dm8/data/cosmic/dm.ini' OVERWRITE 2; d) 归档修复 RMAN> REPAIR ARCHIVELOG DATABASE '/kingdee/dm8/data/cosmic/dm.ini';
恢复至最新状态
RMAN> RECOVER DATABASE '/kingdee/dm8/data/cosmic/dm.ini' WITH ARCHIVEDIR '/kingdee/dm8/data/cosmic/archive_log'; RMAN> RECOVER DATABASE '/kingdee/dm8/data/cosmic/dm.ini' UPDATE DB_MAGIC; $systemctl start DmServicecosmic.service
启动数据库
$systemctl start DmServicecosmic.service
4.3 恢复到指定时间点
其他步骤和4.2节一致,只在恢复部分修改如下:
RMAN> RECOVER DATABASE '/kingdee/dm8/data/cosmic/dm.ini' WITH ARCHIVEDIR '/kingdee/dm8/data/cosmic/archive_log' UNTIL TIME '2023-08-23 17:40:14'; RMAN> RECOVER DATABASE '/kingdee/dm8/data/cosmic/dm.ini' UPDATE DB_MAGIC;
5 备份策略设定执行
每周天凌晨3点一次全备,其余每天凌晨3点进行增备,保留一个月的备份集。
注:项目上应以自身实际需要进行调整备份策略,如条件满足情况下也可每天进行全库备份。
准备备份脚本
####dmrman_backup_db.sh脚本起始### #!/bin/bash export target_db="cosmic" export backup_dest_dir="/kingdee/dm8/data/backup" export LOGFILE=/kingdee/dm8/data/backup/logs/dmrmanbak_`date +%Y%m%d`.log export Begintime=`date +"%Y-%m-%d %H:%M:%S"` export Endtime=`date +"%Y-%m-%d %H:%M:%S"` export backup_time=`date +"%Y-%m-%d"` export user="sysdba" export passwd="SYSDBA" export v_MAXPIECESIZE=128 export v_compressed_level=1 export v_PARALLEL=2 #当天年月日 CUR_DATE=`date +'%Y%m%d%H%M%S'` #获取当天是星期几 WEEKDAY_INDEX=`date +'%w'` echo echo echo "#################################################################################" echo "######################### on "$target_db" #######################################" > $LOGFILE #在线物理备份全库、归档日志,每周天全量备份,其他日期增量备份。同时删除30天前的历史备份集 if [[ $WEEKDAY_INDEX -eq 7 ]]; then echo "############## Begin to backup full database $Begintime ################" >> $LOGFILE echo "#################################################################################" connstr="disql $user/$passwd" $connstr << EOF >> $LOGFILE backup database full backupset '$backup_dest_dir/cosmic_full_${CUR_DATE}' BACKUPINFO '完全备份_${CUR_DATE}' MAXPIECESIZE $v_MAXPIECESIZE compressed level $v_compressed_level PARALLEL $v_PARALLEL; SF_BAKSET_BACKUP_DIR_ADD('DISK','$backup_dest_dir'); call sp_db_bakset_remove_batch('DISK',now()-30); EOF echo "################# Backup full database end `date +"%Y-%m-%d %H:%M:%S"` #######################" >> $LOGFILE else echo "############## Begin to incr backup database $Begintime ################" >> $LOGFILE echo "#################################################################################" connstr="disql $user/$passwd" $connstr << EOF >> $LOGFILE backup database increment with backupdir '$backup_dest_dir' backupset '$backup_dest_dir/cosmic_incr_${CUR_DATE}' BACKUPINFO '增量备份_${CUR_DATE}' MAXPIECESIZE $v_MAXPIECESIZE compressed level $v_compressed_level PARALLEL $v_PARALLEL; SF_BAKSET_BACKUP_DIR_ADD('DISK','$backup_dest_dir'); call sp_db_bakset_remove_batch('DISK',now()-30); EOF echo "################# Backup incr database end `date +"%Y-%m-%d %H:%M:%S"` #######################" >> $LOGFILE fi #备份归档 echo "############## Begin to backup archivelog $Begintime ################" >> $LOGFILE echo "#################################################################################" connstr="disql $user/$passwd" $connstr << EOF >> $LOGFILE backup archivelog all backupset '$backup_dest_dir/cosmic_arch_full_${CUR_DATE}' BACKUPINFO '归档备份_${CUR_DATE}' MAXPIECESIZE $v_MAXPIECESIZE compressed level $v_compressed_level PARALLEL $v_PARALLEL; SF_BAKSET_BACKUP_DIR_ADD('DISK','$backup_dest_dir'); call SP_ARCH_BAKSET_REMOVE_BATCH('DISK',now()-30); EOF echo "################# Backup archivelog end `date +"%Y-%m-%d %H:%M:%S"` #######################" >> $LOGFILE echo "################# all backup end `date +"%Y-%m-%d %H:%M:%S"` #######################" >> $LOGFILE ####dmrman_backup_db.sh脚本结束###
配置执行权限
chmod +x /kingdee/dm8/data/backup/dmrman_backup_db.sh
配置定时任务,每天凌晨3点定时执行备份脚本
crontab -e 0 3 * * * su - dmdba -c "/dm8/backup/bak_dm.sh" >>/kingdee/dm8/data/backup/dmrman_backup_db.sh 2>&1
6 附录
官方文档
https://eco.dameng.com/document/dm/zh-cn/ops/physical-backup-restore.html
达梦数据库物理备份与还原
本文2024-09-23 01:12:57发表“云苍穹知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-cangqiong-144516.html