Skip to content

Commit

Permalink
fix(mysql): dbbackup 日志目录优化 #7106
Browse files Browse the repository at this point in the history
  • Loading branch information
seanlook committed Sep 24, 2024
1 parent e2e4e33 commit 986ea80
Show file tree
Hide file tree
Showing 23 changed files with 272 additions and 162 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (

"github.com/gin-gonic/gin"
"github.com/pkg/errors"

"dbm-services/common/go-pubpkg/cmutil"
)

// GenerateConfigVersion godoc
Expand Down Expand Up @@ -124,8 +126,7 @@ func (cf *Config) GenerateConfigVersion(ctx *gin.Context) {
// 还有一种极端情况,多个请求并行generate,但时间是错开不在 1s内,也能generate成功
if resp, err = simpleconfig.GenerateConfigFile(model.DB.Self, r2, r.Method, nil); err != nil {
//logger.Warn("simpleconfig.GenerateConfigFile err: %+v", err)
if util.IsErrorString(err, "Error 1062: Duplicate entry") ||
util.IsErrorString(err, "Error 1213: Deadlock found when trying to get lock") ||
if mysqlErr := cmutil.NewMySQLError(err).Code; mysqlErr == 1062 || mysqlErr == 1213 ||
util.IsErrorString(err, "revision is applied already:") {
// 前面已经判断不存在,现在写入报重复,说明有其它请求 generate version 了。直接读取
logger.Info("level_node has applied versioned, query configs instead of generate")
Expand All @@ -137,6 +138,7 @@ func (cf *Config) GenerateConfigVersion(ctx *gin.Context) {
return
}
} else {
logger.Errorf("level_node has applied versioned, return error: %s", err.Error())
handler.SendResponse(ctx, err, nil)
return
}
Expand Down
12 changes: 11 additions & 1 deletion dbm-services/common/go-pubpkg/cmutil/mysql_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,18 @@ var mysqlErrors = map[int]MySQLError{
Message: "Can't connect to MySQL server",
regexStr: regexp.MustCompile(`ERROR 2003 .*: Can't connect to MySQL server.*`),
},
1062: {
Code: 1062,
Message: "Duplicate entry",
regexStr: regexp.MustCompile(`(?i)Error 1062.*: Duplicate entry.*`),
},
1213: {
Code: 1213,
Message: "Deadlock found when trying to get lock",
regexStr: regexp.MustCompile(`(?i)Error 1213.*: Deadlock found when trying to get lock.*`),
},
}
var codeParser = regexp.MustCompile(`Error (\d+) .*`)
var codeParser = regexp.MustCompile(`(?i)Error (\d+) .*`)

// NewMySQLError 对象化 mysql error, err入参要求非 nil
func NewMySQLError(err error) MySQLError {
Expand Down
4 changes: 4 additions & 0 deletions dbm-services/go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,7 @@ github.com/Microsoft/hcsshim/test v0.0.0-20210227013316-43a75bb4edd3 h1:4FA+QBay
github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I=
github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE=
github.com/PuerkitoBio/goquery v1.5.1 h1:PSPBGne8NIUWw+/7vFBV+kG2J/5MOjbzc7154OaKCSE=
github.com/QcloudApi/qcloud_sign_golang v0.0.0-20141224014652-e4130a326409 h1:DTQ/38ao/CfXsrK0cSAL+h4R/u0VVvfWLZEOlLwEROI=
github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d h1:UrqY+r/OJnIp5u0s1SbQ8dVfLCZJsnvazdBP5hS4iRs=
github.com/Shopify/toxiproxy v2.1.4+incompatible h1:TKdv8HiTLgE5wdJuEML90aBgNWsokNbMijUGhmcoBJc=
github.com/agiledragon/gomonkey/v2 v2.3.1/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
Expand Down Expand Up @@ -1165,6 +1166,7 @@ github.com/godbus/dbus/v5 v5.0.6 h1:mkgN1ofwASrYnJ5W6U/BxG15eXXXjirgZc7CLqkcaro=
github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw=
github.com/gogo/googleapis v1.4.0 h1:zgVt4UpGxcqVOw97aRGxT4svlcmdK35fynLNctY32zI=
github.com/golang-jwt/jwt/v4 v4.4.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4=
github.com/golang/glog v1.1.0/go.mod h1:pfYeQZ3JWZoXTV5sFc986z3HTpwQs9At6P4ImfuP3NQ=
Expand Down Expand Up @@ -1229,6 +1231,7 @@ github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7FsgI=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/grafov/m3u8 v0.12.0 h1:T6iTwTsSEtMcwkayef+FJO8kj+Sglr4Lh81Zj8Ked/4=
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 h1:pdN6V1QBWetyv/0+wjACpqVH+eVULgEjkurDLq3goeM=
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 h1:+9834+KizmvFV7pXQGSXQTsaWhq2GjuNUt0aUU0YBYw=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92BcuyuQ/YW4NSIpoGtfXNho=
Expand Down Expand Up @@ -1504,6 +1507,7 @@ github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1F
github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 h1:kdXcSzyDtseVEc4yCz2qF8ZrQvIDBJLl4S1c3GCXmoI=
github.com/tchap/go-patricia v2.2.6+incompatible h1:JvoDL7JSoIP2HDE8AbDH3zC8QBPxmzYe32HHy5yQ+Ck=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.563 h1:FoX+MK4vHThvPO6FbP5q98zD8S3n+d5+DbtK7skl++c=
github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802 h1:uruHq4dN7GR16kFc5fp3d1RIYzJW5onx8Ybykw2YQFA=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
github.com/tv42/httpunix v0.0.0-20191220191345-2ba4b9c3382c h1:u6SKchux2yDvFQnDHS3lPnIRmfVJ5Sxy3ao2SIdysLQ=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (c *Component) GenerateBackupConfig() error {

backupConfig.LogicalBackup.Regex = ""
if c.Params.BackupType == "logical" {
//backupConfig.LogicalBackup.UseMysqldump = "auto"
backupConfig.LogicalBackup.UseMysqldump = "auto"
ignoreDbs := slices.DeleteFunc(native.DBSys, func(s string) bool {
return s == "infodba_schema"
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (f *BackupIndexFile) ValidateFiles() error {

// UntarFiles merge and untar
// set targetDir
func (f *BackupIndexFile) UntarFiles(untarDir string) error {
func (f *BackupIndexFile) UntarFiles(untarDir string, removeOriginal bool) error {
if untarDir == "" {
return errors.Errorf("untar target dir should not be emtpy")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (m *DBLoader) Init() error {
}
}

if err = m.initDirs(); err != nil {
if err = m.initDirs(false); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -151,7 +151,7 @@ func (m *DBLoader) Start() error {
cmutil.ExecCommand(false, "", "chown", "-R", "mysql.mysql", m.taskDir)
}()
logger.Info("开始解压 taskDir=%s", m.taskDir)
if err := m.BackupInfo.indexObj.UntarFiles(m.taskDir); err != nil {
if err := m.BackupInfo.indexObj.UntarFiles(m.taskDir, false); err != nil {
return err
}
logger.Info("开始数据恢复 targetDir=%s", m.targetDir)
Expand Down Expand Up @@ -180,7 +180,8 @@ func (m *DBLoader) ReturnChangeMaster() (*mysqlutil.ChangeMaster, error) {
}
}

func (m *DBLoader) initDirs() error {
// initDirs 如果 removeOld = true,会删除当前任务目录下,之前的解压目录,可能是重试导致的废弃目录
func (m *DBLoader) initDirs(removeOld bool) error {
if m.BackupInfo.WorkDir == "" {
return errors.Errorf("work_dir %s should not be empty", m.WorkDir)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package monitor

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

"dbm-services/common/go-pubpkg/logger"
"dbm-services/mysql/db-tools/dbactuator/pkg/components"
"dbm-services/mysql/db-tools/dbactuator/pkg/components/peripheraltools/internal"
"dbm-services/mysql/db-tools/dbactuator/pkg/native"
"dbm-services/mysql/db-tools/dbactuator/pkg/util"
"dbm-services/mysql/db-tools/dbactuator/pkg/util/osutil"
"fmt"
"os"
"path/filepath"
)

func (c *MySQLMonitorComp) GenerateExporterConfig() (err error) {
Expand All @@ -29,7 +30,7 @@ func generateExporterConfigIns(mmp *MySQLMonitorParam, instance *internal.Instan
)

if mmp.MachineType == "proxy" {
f, err := os.OpenFile(exporterConfigPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
f, err := os.OpenFile(exporterConfigPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
logger.Error(err.Error())
return err
Expand Down
7 changes: 4 additions & 3 deletions dbm-services/mysql/db-tools/mysql-dbbackup/cmd/subcmd_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ func init() {
}

var dumpCmd = &cobra.Command{
Use: "dumpbackup",
Short: "Run backup",
Long: `Run backup using config, include logical and physical`,
Use: "dumpbackup",
Short: "Run backup",
Long: `Run backup using config, include logical and physical`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer func() {
cmutil.ExecCommand(false, "", "chown", "-R", "mysql.mysql", cst.DbbackupGoInstallPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ func init() {
}

var dumpLogicalCmd = &cobra.Command{
Use: "logical",
Short: "logical dump using mydumper or mysqldump",
Use: "logical",
Short: "logical dump using mydumper or mysqldump",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
var err error
if err = logger.InitLog("dbbackup_dump.log"); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ func findSpiderBackupConfigFile(cnfFiles []string) (string, error) {
}

var spiderScheduleCmd = &cobra.Command{
Use: "schedule",
Short: "spiderbackup schedule",
Long: `Start spider global backup. Will initialize backup tasks using one backup-id, only run on spider master`,
Use: "schedule",
SilenceUsage: true,
Short: "spiderbackup schedule",
Long: `Start spider global backup. Will initialize backup tasks using one backup-id on spider master`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := logger.InitLog("dbbackup_spider.log"); err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package backupexe

import (
"bytes"
"context"
"fmt"
"os"
Expand Down Expand Up @@ -144,9 +145,7 @@ func (l *LogicalDumper) Execute(enableTimeOut bool) error {
logger.Log.Info("logical dump command: ", cmd.String())

outFile, err := os.Create(
filepath.Join(
l.dbbackupHome,
"logs",
filepath.Join(logger.GetLogDir(),
fmt.Sprintf("mydumper_%d_%d.log", l.cnf.Public.MysqlPort, int(time.Now().Weekday()))))
if err != nil {
logger.Log.Error("create log file failed: ", err)
Expand All @@ -157,11 +156,13 @@ func (l *LogicalDumper) Execute(enableTimeOut bool) error {
}()

cmd.Stdout = outFile
cmd.Stderr = outFile
//cmd.Stderr = outFile
var stderr bytes.Buffer
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
logger.Log.Error("run logical backup failed: ", err)
return err
logger.Log.Error("run logical backup failed: ", err, stderr.String())
return errors.WithMessage(err, stderr.String())
}
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package backupexe

import (
"bytes"
"context"
"fmt"
"os"
Expand Down Expand Up @@ -226,11 +227,8 @@ func (l *LogicalDumperMysqldump) Execute(enableTimeOut bool) (err error) {

logger.Log.Info("logical dump command with mysqldump: ", cmd.String())

outFile, err := os.Create(
filepath.Join(
l.dbbackupHome,
"logs",
fmt.Sprintf("mysqldump_%d.log", int(time.Now().Weekday()))))
outFile, err := os.Create(filepath.Join(logger.GetLogDir(),
fmt.Sprintf("mysqldump_%d.log", int(time.Now().Weekday()))))
if err != nil {
logger.Log.Error("create log file failed: ", err)
return err
Expand All @@ -239,7 +237,9 @@ func (l *LogicalDumperMysqldump) Execute(enableTimeOut bool) (err error) {
_ = outFile.Close()
}()
cmd.Stdout = outFile
cmd.Stderr = outFile
//cmd.Stderr = outFile
var stderr bytes.Buffer
cmd.Stderr = &stderr

mysqldumpBeginTime := time.Now().Format("2006-01-02 15:04:05")
l.backupInfo.BackupBeginTime, err = time.ParseInLocation(cst.MydumperTimeLayout, mysqldumpBeginTime, time.Local)
Expand All @@ -248,8 +248,8 @@ func (l *LogicalDumperMysqldump) Execute(enableTimeOut bool) (err error) {
}
err = cmd.Run()
if err != nil {
logger.Log.Error("run logical backup(with mysqldump) failed: ", err)
return err
logger.Log.Error("run logical backup(with mysqldump) failed: ", err, stderr.String())
return errors.WithMessage(err, stderr.String())
}
mysqldumpEndTime := time.Now().Format("2006-01-02 15:04:05")
l.backupInfo.BackupEndTime, err = time.ParseInLocation(cst.MydumperTimeLayout, mysqldumpEndTime, time.Local)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package backupexe

import (
"bytes"
"context"
"fmt"
"os"
Expand Down Expand Up @@ -190,11 +191,8 @@ func (p *PhysicalDumper) Execute(enableTimeOut bool) error {
fmt.Sprintf(`%s %s`, binPath, strings.Join(args, " ")))
}

outFile, err := os.Create(
filepath.Join(
p.dbbackupHome,
"logs",
fmt.Sprintf("xtrabackup_%d_%d.log", p.cnf.Public.MysqlPort, int(time.Now().Weekday()))))
outFile, err := os.Create(filepath.Join(logger.GetLogDir(),
fmt.Sprintf("xtrabackup_%d_%d.log", p.cnf.Public.MysqlPort, int(time.Now().Weekday()))))
if err != nil {
logger.Log.Error("create log file failed: ", err)
return err
Expand All @@ -204,13 +202,15 @@ func (p *PhysicalDumper) Execute(enableTimeOut bool) error {
}()

cmd.Stdout = outFile
cmd.Stderr = outFile
//cmd.Stderr = outFile
var stderr bytes.Buffer
cmd.Stderr = &stderr
logger.Log.Info("xtrabackup command: ", cmd.String())

err = cmd.Run()
if err != nil {
logger.Log.Error("run physical backup failed: ", err)
return err
logger.Log.Error("run physical backup failed: ", err, stderr.Bytes())
return errors.WithMessage(err, stderr.String())
}
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ func (p *PhysicalLoader) Execute() error {
return nil
}

// decompress todo use qpress command instead
func (p *PhysicalLoader) decompress() error {
binPath := filepath.Join(p.dbbackupHome, p.innodbCmd.innobackupexBin)

args := []string{
"--decompress",
"--decompress", "--remove-original",
//fmt.Sprintf("--qpress=%s", filepath.Join(p.dbbackupHome, "/bin", "qpress")),
fmt.Sprintf("--parallel=%d", p.cnf.PhysicalLoad.Threads),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,29 @@ var Log *logrus.Logger

const DefaultLogFileName = "dbbackup.log"

// GetLogDir get log dir
func GetLogDir() string {
logDir := viper.GetString("log-dir")
if logDir == "" {
executable, _ := os.Executable()
logDir = filepath.Join(filepath.Dir(executable), "logs")
}
return logDir
}

// InitLog Initialize dbbackupLog
// 如果 logDir 为空,则 log记录到 dbbackup/logs 下
// 如果 logFileName 包含相对目录,则根据在命令当前目录下创建相对目录
// 如果 logFileName 包含绝对目录,则以绝对目录的 log file 来记录
func InitLog(logFileName string) (err error) {
logDir := viper.GetString("log-dir")
Log = logrus.New()
Log.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
})
if logFileName == "" {
logFileName = DefaultLogFileName
}
if logDir == "" {
executable, _ := os.Executable()
logDir = filepath.Join(filepath.Dir(executable), "logs")
}
logDir := GetLogDir()
if !cmutil.IsDirectory(logDir) {
_ = os.Mkdir(logDir, 0755)
}
Expand Down
5 changes: 0 additions & 5 deletions dbm-services/redis/db-tools/dbactuator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ require (

require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/clbanning/mxj v1.8.4 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/glebarez/go-sqlite v1.21.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand All @@ -40,14 +38,11 @@ require (
github.com/leodido/go-urn v1.2.3 // indirect
github.com/lufia/plan9stats v0.0.0-20230110061619-bbe2e5e100de // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mozillazg/go-httpheader v0.4.0 // indirect
github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rogpeppe/go-internal v1.8.0 // indirect
github.com/smartystreets/assertions v1.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tencentyun/cos-go-sdk-v5 v0.7.54 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
Expand Down
Loading

0 comments on commit 986ea80

Please sign in to comment.