Skip to content

Commit

Permalink
feat(mysql): mysql备份恢复删除重试的解压文件 TencentBlueKing#7116
Browse files Browse the repository at this point in the history
  • Loading branch information
seanlook committed Sep 24, 2024
1 parent 58fff3f commit cc5f546
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package restore

import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/jinzhu/copier"
"github.com/pkg/errors"
Expand Down Expand Up @@ -49,7 +52,7 @@ func (m *DBLoader) Init() error {
}
}

if err = m.initDirs(false); err != nil {
if err = m.initDirs(true); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -188,16 +191,26 @@ func (m *DBLoader) initDirs(removeOld bool) error {
if m.WorkID == "" {
m.WorkID = newTimestampString()
}
if removeOld { // 删除旧目录
timeNow := time.Now()
oldDirs, _ := filepath.Glob(fmt.Sprintf("%s/doDr_*", m.WorkDir))
for _, oldDir := range oldDirs {
if dirInfo, err := os.Stat(oldDir); err == nil && dirInfo.IsDir() {
if timeNow.Sub(dirInfo.ModTime()) > 1*time.Minute {
logger.Warn("remove old recover work directory: ", oldDirs)
_ = os.RemoveAll(oldDir)
}
}
}
}

m.taskDir = fmt.Sprintf("%s/doDr_%s/%d", m.WorkDir, m.WorkID, m.TgtInstance.Port)
if err := osutil.CheckAndMkdir("", m.taskDir); err != nil {
return err
}
/*
if m.BackupInfo.indexObj.GetMetafileBasename() == "" {
return errors.New("backup file baseName error")
}
*/
m.targetDir = m.BackupInfo.indexObj.GetTargetDir(m.taskDir)
logger.Info("current recover work directory: ", m.taskDir)
logger.Info("current recover work target directory: ", m.targetDir)
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,15 @@ func (p *PhysicalDumper) PrepareBackupMetaInfo(cnf *config.BackupConfig) (*dbare
}
// parse xtrabackup_info
if err = parseXtraInfo(qpressPath, xtrabackupInfoFileName, tmpFileName, &metaInfo); err != nil {
logger.Log.Warnf("xtrabackup_info file not found, use current time as BackupEndTime")
logger.Log.Warnf("xtrabackup_info file not found, use current time as BackupEndTime, err: %s", err.Error())
metaInfo.BackupBeginTime, _ = time.Parse(time.DateTime, p.backupStartTime.Format(time.DateTime))
metaInfo.BackupEndTime, _ = time.Parse(time.DateTime, p.backupEndTime.Format(time.DateTime))
}
// parse xtrabackup_timestamp_info
if err := parseXtraTimestamp(qpressPath, xtrabackupTimestampFileName, tmpFileName, &metaInfo); err != nil {
// 此时刚备份完成,还没有开始打包,这里把当前时间认为是 consistent_time,不完善!
logger.Log.Warnf("xtrabackup_timestamp_info file not found, use current time as Consistent Time")
logger.Log.Warnf("xtrabackup_timestamp_info file not found, "+
"use current time as Consistent Time, err: %s", err.Error())
metaInfo.BackupConsistentTime, _ = time.Parse(time.DateTime, p.backupEndTime.Format(time.DateTime))
}
// parse xtrabackup_binlog_info 本机的 binlog file,pos
Expand All @@ -280,7 +281,8 @@ func (p *PhysicalDumper) PrepareBackupMetaInfo(cnf *config.BackupConfig) (*dbare
// parse xtrabackup_slave_info 如果是 slave,获取它的 master file,pos
if mysqlRole := strings.ToLower(cnf.Public.MysqlRole); mysqlRole == cst.RoleSlave || mysqlRole == cst.RoleRepeater {
if slaveStatus, err := parseXtraSlaveInfo(qpressPath, xtrabackupSlaveInfoFileName, tmpFileName); err != nil {
return nil, err
logger.Log.Warnf("parse xtrabackup_slave_info with error for role=%s %s:%d , err: %s",
cnf.Public.MysqlRole, cnf.Public.MysqlHost, cnf.Public.MysqlPort, err.Error())
} else {
metaInfo.BinlogInfo.ShowSlaveStatus = slaveStatus
masterHost, masterPort, err := mysqlconn.ShowMysqlSlaveStatus(db)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func openXtrabackupFile(binpath string, fileName string, tmpFileName string) (*b
return bytes.NewBuffer(content), nil
}

// parseXtraInfo get xtrabackup start_time / end_time
// parseXtraInfo get start_time / end_time from xtrabackup_info
// return startTime,endTime,error
func parseXtraInfo(qpress string, fileName string, tmpFileName string, metaInfo *dbareport.IndexContent) error {
fileBytes, err := openXtrabackupFile(qpress, fileName, tmpFileName)
Expand Down

0 comments on commit cc5f546

Please sign in to comment.