2023-02-21 11:06:24 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2023-09-15 07:26:19 +00:00
|
|
|
"strconv"
|
2023-02-21 11:06:24 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
2023-07-27 06:32:23 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/mysql/client"
|
2023-02-21 11:06:24 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (u *BackupService) MysqlBackup(req dto.CommonBackup) error {
|
|
|
|
localDir, err := loadLocalDir()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
timeNow := time.Now().Format("20060102150405")
|
2023-09-15 07:26:19 +00:00
|
|
|
databaseID, err := strconv.Atoi(req.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
database, err := databaseRepo.Get(commonRepo.WithByID(uint(databaseID)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dirName := fmt.Sprintf("%s-%s", database.From, database.Name)
|
|
|
|
targetDir := path.Join(localDir, fmt.Sprintf("database/%s/%s/%s", req.Type, dirName, req.DetailName))
|
2023-02-21 11:06:24 +00:00
|
|
|
fileName := fmt.Sprintf("%s_%s.sql.gz", req.DetailName, timeNow)
|
2023-07-27 06:32:23 +00:00
|
|
|
|
2023-09-15 07:26:19 +00:00
|
|
|
if err := handleMysqlBackup(uint(databaseID), req.DetailName, targetDir, fileName); err != nil {
|
2023-02-21 11:06:24 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-07-27 06:32:23 +00:00
|
|
|
|
2023-02-21 11:06:24 +00:00
|
|
|
record := &model.BackupRecord{
|
2023-08-29 02:50:15 +00:00
|
|
|
Type: req.Type,
|
2023-09-15 10:48:14 +00:00
|
|
|
Name: fmt.Sprintf("%v", database.ID),
|
2023-02-21 11:06:24 +00:00
|
|
|
DetailName: req.DetailName,
|
|
|
|
Source: "LOCAL",
|
|
|
|
BackupType: "LOCAL",
|
2023-07-27 06:32:23 +00:00
|
|
|
FileDir: targetDir,
|
2023-02-21 11:06:24 +00:00
|
|
|
FileName: fileName,
|
|
|
|
}
|
|
|
|
if err := backupRepo.CreateRecord(record); err != nil {
|
|
|
|
global.LOG.Errorf("save backup record failed, err: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *BackupService) MysqlRecover(req dto.CommonRecover) error {
|
2023-07-27 06:32:23 +00:00
|
|
|
if err := handleMysqlRecover(req, false); err != nil {
|
2023-02-21 11:06:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *BackupService) MysqlRecoverByUpload(req dto.CommonRecover) error {
|
|
|
|
file := req.File
|
|
|
|
fileName := path.Base(req.File)
|
2023-03-07 07:51:48 +00:00
|
|
|
if strings.HasSuffix(fileName, ".tar.gz") {
|
2023-02-21 11:06:24 +00:00
|
|
|
fileNameItem := time.Now().Format("20060102150405")
|
|
|
|
dstDir := fmt.Sprintf("%s/%s", path.Dir(req.File), fileNameItem)
|
|
|
|
if _, err := os.Stat(dstDir); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(dstDir, os.ModePerm); err != nil {
|
|
|
|
return fmt.Errorf("mkdir %s failed, err: %v", dstDir, err)
|
|
|
|
}
|
|
|
|
}
|
2023-03-07 07:51:48 +00:00
|
|
|
if err := handleUnTar(req.File, dstDir); err != nil {
|
2023-02-21 11:06:24 +00:00
|
|
|
_ = os.RemoveAll(dstDir)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
global.LOG.Infof("decompress file %s successful, now start to check test.sql is exist", req.File)
|
|
|
|
hasTestSql := false
|
|
|
|
_ = filepath.Walk(dstDir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !info.IsDir() && info.Name() == "test.sql" {
|
|
|
|
hasTestSql = true
|
|
|
|
file = path
|
|
|
|
fileName = "test.sql"
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if !hasTestSql {
|
|
|
|
_ = os.RemoveAll(dstDir)
|
2023-03-08 07:33:43 +00:00
|
|
|
return fmt.Errorf("no such file named test.sql in %s", fileName)
|
2023-02-21 11:06:24 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
_ = os.RemoveAll(dstDir)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2023-07-27 06:32:23 +00:00
|
|
|
req.File = path.Dir(file) + "/" + fileName
|
|
|
|
if err := handleMysqlRecover(req, false); err != nil {
|
2023-02-21 11:06:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
global.LOG.Info("recover from uploads successful!")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-15 07:26:19 +00:00
|
|
|
func handleMysqlBackup(databaseID uint, dbName, targetDir, fileName string) error {
|
|
|
|
cli, _, _, err := LoadMysqlClientByFrom(databaseID)
|
2023-07-27 06:32:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-02-21 11:06:24 +00:00
|
|
|
}
|
2023-09-15 07:26:19 +00:00
|
|
|
dbInfo, err := mysqlRepo.Get(commonRepo.WithByName(dbName), mysqlRepo.WithByDatabase(databaseID))
|
2023-07-27 06:32:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
backupInfo := client.BackupInfo{
|
|
|
|
Name: dbName,
|
|
|
|
Format: dbInfo.Format,
|
|
|
|
TargetDir: targetDir,
|
|
|
|
FileName: fileName,
|
2023-02-21 11:06:24 +00:00
|
|
|
|
2023-07-27 06:32:23 +00:00
|
|
|
Timeout: 300,
|
|
|
|
}
|
|
|
|
if err := cli.Backup(backupInfo); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-21 11:06:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 06:32:23 +00:00
|
|
|
func handleMysqlRecover(req dto.CommonRecover, isRollback bool) error {
|
2023-03-01 03:38:08 +00:00
|
|
|
isOk := false
|
2023-07-27 06:32:23 +00:00
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
if !fileOp.Stat(req.File) {
|
|
|
|
return errors.New(fmt.Sprintf("%s file is not exist", req.File))
|
|
|
|
}
|
2023-09-15 07:26:19 +00:00
|
|
|
databaseID, err := strconv.Atoi(req.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cli, _, _, err := LoadMysqlClientByFrom(uint(databaseID))
|
2023-07-27 06:32:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-15 07:26:19 +00:00
|
|
|
dbInfo, err := mysqlRepo.Get(commonRepo.WithByName(req.DetailName), mysqlRepo.WithByDatabase(uint(databaseID)))
|
2023-07-27 06:32:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-01 14:05:14 +00:00
|
|
|
if !isRollback {
|
2023-08-29 02:50:15 +00:00
|
|
|
rollbackFile := path.Join(global.CONF.System.TmpDir, fmt.Sprintf("database/%s/%s_%s.sql.gz", req.Type, req.DetailName, time.Now().Format("20060102150405")))
|
2023-07-27 06:32:23 +00:00
|
|
|
if err := cli.Backup(client.BackupInfo{
|
|
|
|
Name: req.DetailName,
|
|
|
|
Format: dbInfo.Format,
|
|
|
|
TargetDir: path.Dir(rollbackFile),
|
|
|
|
FileName: path.Base(rollbackFile),
|
|
|
|
|
|
|
|
Timeout: 300,
|
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("backup mysql db %s for rollback before recover failed, err: %v", req.DetailName, err)
|
2023-03-01 03:38:08 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if !isOk {
|
2023-03-08 15:58:42 +00:00
|
|
|
global.LOG.Info("recover failed, start to rollback now")
|
2023-07-27 06:32:23 +00:00
|
|
|
if err := cli.Recover(client.RecoverInfo{
|
|
|
|
Name: req.DetailName,
|
|
|
|
Format: dbInfo.Format,
|
|
|
|
SourceFile: rollbackFile,
|
|
|
|
|
|
|
|
Timeout: 300,
|
|
|
|
}); err != nil {
|
|
|
|
global.LOG.Errorf("rollback mysql db %s from %s failed, err: %v", req.DetailName, rollbackFile, err)
|
2023-03-01 03:38:08 +00:00
|
|
|
}
|
2023-07-27 06:32:23 +00:00
|
|
|
global.LOG.Infof("rollback mysql db %s from %s successful", req.DetailName, rollbackFile)
|
2023-03-01 03:38:08 +00:00
|
|
|
_ = os.RemoveAll(rollbackFile)
|
|
|
|
} else {
|
|
|
|
_ = os.RemoveAll(rollbackFile)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2023-07-27 06:32:23 +00:00
|
|
|
if err := cli.Recover(client.RecoverInfo{
|
|
|
|
Name: req.DetailName,
|
|
|
|
Format: dbInfo.Format,
|
|
|
|
SourceFile: req.File,
|
|
|
|
|
|
|
|
Timeout: 300,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
2023-02-21 11:06:24 +00:00
|
|
|
}
|
2023-08-04 06:10:43 +00:00
|
|
|
isOk = true
|
2023-02-21 11:06:24 +00:00
|
|
|
return nil
|
|
|
|
}
|