2022-09-16 18:53:45 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2022-12-21 15:54:34 +08:00
|
|
|
"context"
|
2022-09-16 18:53:45 +08:00
|
|
|
"encoding/json"
|
2022-10-28 11:02:47 +08:00
|
|
|
"fmt"
|
2022-10-27 23:09:39 +08:00
|
|
|
"os"
|
2023-02-23 16:56:16 +08:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2022-09-16 18:53:45 +08:00
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2022-10-27 23:09:39 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cloud_storage"
|
2022-09-16 18:53:45 +08:00
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BackupService struct{}
|
|
|
|
|
|
|
|
type IBackupService interface {
|
2022-09-19 19:42:06 +08:00
|
|
|
List() ([]dto.BackupInfo, error)
|
2022-11-29 17:39:10 +08:00
|
|
|
SearchRecordsWithPage(search dto.RecordSearch) (int64, []dto.BackupRecords, error)
|
2022-10-28 11:02:47 +08:00
|
|
|
DownloadRecord(info dto.DownloadRecord) (string, error)
|
2022-09-16 18:53:45 +08:00
|
|
|
Create(backupDto dto.BackupOperate) error
|
|
|
|
GetBuckets(backupDto dto.ForBuckets) ([]interface{}, error)
|
2023-01-29 16:38:34 +08:00
|
|
|
Update(ireq dto.BackupOperate) error
|
2022-09-16 18:53:45 +08:00
|
|
|
BatchDelete(ids []uint) error
|
2022-10-27 23:09:39 +08:00
|
|
|
BatchDeleteRecord(ids []uint) error
|
2022-09-29 16:15:59 +08:00
|
|
|
NewClient(backup *model.BackupAccount) (cloud_storage.CloudStorageClient, error)
|
2023-02-13 15:48:18 +08:00
|
|
|
|
|
|
|
ListFiles(req dto.BackupSearchFile) ([]interface{}, error)
|
2023-02-21 19:06:24 +08:00
|
|
|
|
|
|
|
MysqlBackup(db dto.CommonBackup) error
|
|
|
|
MysqlRecover(db dto.CommonRecover) error
|
|
|
|
MysqlRecoverByUpload(req dto.CommonRecover) error
|
|
|
|
|
|
|
|
RedisBackup() error
|
|
|
|
RedisRecover(db dto.CommonRecover) error
|
|
|
|
|
|
|
|
WebsiteBackup(db dto.CommonBackup) error
|
|
|
|
WebsiteRecover(req dto.CommonRecover) error
|
|
|
|
|
|
|
|
AppBackup(db dto.CommonBackup) error
|
|
|
|
AppRecover(req dto.CommonRecover) error
|
2022-09-16 18:53:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIBackupService() IBackupService {
|
|
|
|
return &BackupService{}
|
|
|
|
}
|
|
|
|
|
2022-09-19 19:42:06 +08:00
|
|
|
func (u *BackupService) List() ([]dto.BackupInfo, error) {
|
|
|
|
ops, err := backupRepo.List(commonRepo.WithOrderBy("created_at desc"))
|
2022-09-16 18:53:45 +08:00
|
|
|
var dtobas []dto.BackupInfo
|
2023-01-28 16:51:58 +08:00
|
|
|
ossExist, s3Exist, sftpExist, minioExist := false, false, false, false
|
2022-09-16 18:53:45 +08:00
|
|
|
for _, group := range ops {
|
2023-01-28 16:51:58 +08:00
|
|
|
switch group.Type {
|
|
|
|
case "OSS":
|
|
|
|
ossExist = true
|
|
|
|
case "S3":
|
|
|
|
s3Exist = true
|
|
|
|
case "SFTP":
|
|
|
|
sftpExist = true
|
|
|
|
case "MINIO":
|
|
|
|
minioExist = true
|
|
|
|
}
|
2022-09-16 18:53:45 +08:00
|
|
|
var item dto.BackupInfo
|
|
|
|
if err := copier.Copy(&item, &group); err != nil {
|
2022-09-19 19:42:06 +08:00
|
|
|
return nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
2022-09-16 18:53:45 +08:00
|
|
|
}
|
|
|
|
dtobas = append(dtobas, item)
|
|
|
|
}
|
2023-01-28 16:51:58 +08:00
|
|
|
if !ossExist {
|
|
|
|
dtobas = append(dtobas, dto.BackupInfo{Type: "OSS"})
|
|
|
|
}
|
|
|
|
if !s3Exist {
|
|
|
|
dtobas = append(dtobas, dto.BackupInfo{Type: "S3"})
|
|
|
|
}
|
|
|
|
if !sftpExist {
|
|
|
|
dtobas = append(dtobas, dto.BackupInfo{Type: "SFTP"})
|
|
|
|
}
|
|
|
|
if !minioExist {
|
|
|
|
dtobas = append(dtobas, dto.BackupInfo{Type: "MINIO"})
|
|
|
|
}
|
2022-09-19 19:42:06 +08:00
|
|
|
return dtobas, err
|
2022-09-16 18:53:45 +08:00
|
|
|
}
|
|
|
|
|
2022-11-29 17:39:10 +08:00
|
|
|
func (u *BackupService) SearchRecordsWithPage(search dto.RecordSearch) (int64, []dto.BackupRecords, error) {
|
2022-10-27 23:09:39 +08:00
|
|
|
total, records, err := backupRepo.PageRecord(
|
|
|
|
search.Page, search.PageSize,
|
|
|
|
commonRepo.WithOrderBy("created_at desc"),
|
|
|
|
commonRepo.WithByName(search.Name),
|
|
|
|
commonRepo.WithByType(search.Type),
|
|
|
|
backupRepo.WithByDetailName(search.DetailName),
|
|
|
|
)
|
|
|
|
var dtobas []dto.BackupRecords
|
|
|
|
for _, group := range records {
|
|
|
|
var item dto.BackupRecords
|
|
|
|
if err := copier.Copy(&item, &group); err != nil {
|
|
|
|
return 0, nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
|
|
|
dtobas = append(dtobas, item)
|
|
|
|
}
|
|
|
|
return total, dtobas, err
|
|
|
|
}
|
|
|
|
|
2022-10-28 11:02:47 +08:00
|
|
|
func (u *BackupService) DownloadRecord(info dto.DownloadRecord) (string, error) {
|
|
|
|
if info.Source == "LOCAL" {
|
2022-10-28 18:46:14 +08:00
|
|
|
return info.FileDir + "/" + info.FileName, nil
|
2022-10-28 11:02:47 +08:00
|
|
|
}
|
|
|
|
backup, _ := backupRepo.Get(commonRepo.WithByType(info.Source))
|
|
|
|
if backup.ID == 0 {
|
|
|
|
return "", constant.ErrRecordNotFound
|
|
|
|
}
|
|
|
|
varMap := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal([]byte(backup.Vars), &varMap); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
varMap["type"] = backup.Type
|
|
|
|
varMap["bucket"] = backup.Bucket
|
|
|
|
switch backup.Type {
|
|
|
|
case constant.Sftp:
|
2022-12-22 15:03:43 +08:00
|
|
|
varMap["username"] = backup.AccessKey
|
2022-10-28 11:02:47 +08:00
|
|
|
varMap["password"] = backup.Credential
|
|
|
|
case constant.OSS, constant.S3, constant.MinIo:
|
2022-12-22 15:03:43 +08:00
|
|
|
varMap["accessKey"] = backup.AccessKey
|
2022-10-28 11:02:47 +08:00
|
|
|
varMap["secretKey"] = backup.Credential
|
|
|
|
}
|
|
|
|
backClient, err := cloud_storage.NewCloudStorageClient(varMap)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("new cloud storage client failed, err: %v", err)
|
|
|
|
}
|
2023-01-29 16:38:34 +08:00
|
|
|
tempPath := fmt.Sprintf("%sdownload%s", constant.DataDir, info.FileDir)
|
2022-10-28 11:02:47 +08:00
|
|
|
if _, err := os.Stat(tempPath); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(tempPath, os.ModePerm); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
targetPath := tempPath + info.FileName
|
|
|
|
if _, err = os.Stat(targetPath); err != nil && os.IsNotExist(err) {
|
|
|
|
isOK, err := backClient.Download(info.FileName, targetPath)
|
|
|
|
if !isOK {
|
|
|
|
return "", fmt.Errorf("cloud storage download failed, err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return targetPath, nil
|
|
|
|
}
|
|
|
|
|
2022-09-16 18:53:45 +08:00
|
|
|
func (u *BackupService) Create(backupDto dto.BackupOperate) error {
|
2022-09-19 19:42:06 +08:00
|
|
|
backup, _ := backupRepo.Get(commonRepo.WithByType(backupDto.Type))
|
2022-09-16 18:53:45 +08:00
|
|
|
if backup.ID != 0 {
|
|
|
|
return constant.ErrRecordExist
|
|
|
|
}
|
|
|
|
if err := copier.Copy(&backup, &backupDto); err != nil {
|
|
|
|
return errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
|
|
|
if err := backupRepo.Create(&backup); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *BackupService) GetBuckets(backupDto dto.ForBuckets) ([]interface{}, error) {
|
|
|
|
varMap := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal([]byte(backupDto.Vars), &varMap); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
varMap["type"] = backupDto.Type
|
|
|
|
switch backupDto.Type {
|
|
|
|
case constant.Sftp:
|
2022-12-22 15:03:43 +08:00
|
|
|
varMap["username"] = backupDto.AccessKey
|
2022-09-16 18:53:45 +08:00
|
|
|
varMap["password"] = backupDto.Credential
|
|
|
|
case constant.OSS, constant.S3, constant.MinIo:
|
2022-12-22 15:03:43 +08:00
|
|
|
varMap["accessKey"] = backupDto.AccessKey
|
2022-09-16 18:53:45 +08:00
|
|
|
varMap["secretKey"] = backupDto.Credential
|
|
|
|
}
|
|
|
|
client, err := cloud_storage.NewCloudStorageClient(varMap)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return client.ListBuckets()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *BackupService) BatchDelete(ids []uint) error {
|
|
|
|
return backupRepo.Delete(commonRepo.WithIdsIn(ids))
|
|
|
|
}
|
|
|
|
|
2022-10-27 23:09:39 +08:00
|
|
|
func (u *BackupService) BatchDeleteRecord(ids []uint) error {
|
|
|
|
records, err := backupRepo.ListRecord(commonRepo.WithIdsIn(ids))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, record := range records {
|
|
|
|
if record.Source == "LOCAL" {
|
2022-12-01 16:21:49 +08:00
|
|
|
if err := os.Remove(record.FileDir + "/" + record.FileName); err != nil {
|
2022-10-27 23:09:39 +08:00
|
|
|
global.LOG.Errorf("remove file %s failed, err: %v", record.FileDir+record.FileName, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
backupAccount, err := backupRepo.Get(commonRepo.WithByName(record.Source))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
client, err := u.NewClient(&backupAccount)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err = client.Delete(record.FileDir + record.FileName); err != nil {
|
|
|
|
global.LOG.Errorf("remove file %s from %s failed, err: %v", record.FileDir+record.FileName, record.Source, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-21 15:54:34 +08:00
|
|
|
return backupRepo.DeleteRecord(context.Background(), commonRepo.WithIdsIn(ids))
|
2022-10-27 23:09:39 +08:00
|
|
|
}
|
|
|
|
|
2023-01-29 16:38:34 +08:00
|
|
|
func (u *BackupService) Update(req dto.BackupOperate) error {
|
|
|
|
backup, err := backupRepo.Get(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
|
|
|
return constant.ErrRecordNotFound
|
|
|
|
}
|
2023-02-23 16:56:16 +08:00
|
|
|
varMap := make(map[string]interface{})
|
2023-01-29 16:38:34 +08:00
|
|
|
if err := json.Unmarshal([]byte(req.Vars), &varMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
upMap := make(map[string]interface{})
|
|
|
|
upMap["bucket"] = req.Bucket
|
|
|
|
upMap["credential"] = req.Credential
|
|
|
|
upMap["vars"] = req.Vars
|
|
|
|
if err := backupRepo.Update(req.ID, upMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if backup.Type == "LOCAL" {
|
|
|
|
if dir, ok := varMap["dir"]; ok {
|
2023-02-23 16:56:16 +08:00
|
|
|
if dirStr, isStr := dir.(string); isStr {
|
|
|
|
if err := updateBackupDir(dirStr); err != nil {
|
|
|
|
upMap["vars"] = backup.Vars
|
|
|
|
_ = backupRepo.Update(req.ID, upMap)
|
|
|
|
return err
|
|
|
|
}
|
2023-01-29 16:38:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2022-09-16 18:53:45 +08:00
|
|
|
}
|
2022-09-29 16:15:59 +08:00
|
|
|
|
2023-02-13 15:48:18 +08:00
|
|
|
func (u *BackupService) ListFiles(req dto.BackupSearchFile) ([]interface{}, error) {
|
|
|
|
backup, err := backupRepo.Get(backupRepo.WithByType(req.Type))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
client, err := u.NewClient(&backup)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return client.ListObjects("system_snapshot/")
|
|
|
|
}
|
|
|
|
|
2022-09-29 16:15:59 +08:00
|
|
|
func (u *BackupService) NewClient(backup *model.BackupAccount) (cloud_storage.CloudStorageClient, error) {
|
|
|
|
varMap := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal([]byte(backup.Vars), &varMap); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
varMap["type"] = backup.Type
|
|
|
|
if backup.Type == "LOCAL" {
|
|
|
|
return nil, errors.New("not support")
|
|
|
|
}
|
|
|
|
varMap["bucket"] = backup.Bucket
|
|
|
|
switch backup.Type {
|
|
|
|
case constant.Sftp:
|
2022-12-22 15:03:43 +08:00
|
|
|
varMap["username"] = backup.AccessKey
|
2022-09-29 16:15:59 +08:00
|
|
|
varMap["password"] = backup.Credential
|
|
|
|
case constant.OSS, constant.S3, constant.MinIo:
|
2022-12-22 15:03:43 +08:00
|
|
|
varMap["accessKey"] = backup.AccessKey
|
2022-09-29 16:15:59 +08:00
|
|
|
varMap["secretKey"] = backup.Credential
|
|
|
|
}
|
|
|
|
|
|
|
|
backClient, err := cloud_storage.NewCloudStorageClient(varMap)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return backClient, nil
|
|
|
|
}
|
2022-10-28 18:46:14 +08:00
|
|
|
|
2022-11-09 15:08:38 +08:00
|
|
|
func loadLocalDir() (string, error) {
|
|
|
|
backup, err := backupRepo.Get(commonRepo.WithByType("LOCAL"))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-10-28 18:46:14 +08:00
|
|
|
varMap := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal([]byte(backup.Vars), &varMap); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if _, ok := varMap["dir"]; !ok {
|
|
|
|
return "", errors.New("load local backup dir failed")
|
|
|
|
}
|
|
|
|
baseDir, ok := varMap["dir"].(string)
|
|
|
|
if ok {
|
|
|
|
if _, err := os.Stat(baseDir); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(baseDir, os.ModePerm); err != nil {
|
2023-02-21 19:06:24 +08:00
|
|
|
return "", fmt.Errorf("mkdir %s failed, err: %v", baseDir, err)
|
2022-10-28 18:46:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return baseDir, nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("error type dir: %T", varMap["dir"])
|
|
|
|
}
|
2023-01-29 16:38:34 +08:00
|
|
|
|
|
|
|
func updateBackupDir(dir string) error {
|
|
|
|
oldDir := global.CONF.System.Backup
|
|
|
|
if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(dir, os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-02-23 16:56:16 +08:00
|
|
|
if strings.HasSuffix(oldDir, "/") {
|
|
|
|
oldDir = oldDir[:strings.LastIndex(oldDir, "/")]
|
2023-01-29 16:38:34 +08:00
|
|
|
}
|
2023-02-23 16:56:16 +08:00
|
|
|
cmd := exec.Command("cp", "-r", oldDir+"/*", dir)
|
|
|
|
stdout, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(string(stdout))
|
2023-01-29 16:38:34 +08:00
|
|
|
}
|
2023-02-23 16:56:16 +08:00
|
|
|
global.CONF.System.Backup = dir
|
2023-01-29 16:38:34 +08:00
|
|
|
return nil
|
|
|
|
}
|