2022-09-16 10:53:45 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2022-12-21 07:54:34 +00:00
|
|
|
"context"
|
2023-07-04 08:16:12 +00:00
|
|
|
"encoding/base64"
|
2022-09-16 10:53:45 +00:00
|
|
|
"encoding/json"
|
2022-10-28 03:02:47 +00:00
|
|
|
"fmt"
|
2023-06-23 15:06:13 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2022-10-27 15:09:39 +00:00
|
|
|
"os"
|
2023-05-12 14:30:26 +00:00
|
|
|
"path"
|
2023-02-23 08:56:16 +00:00
|
|
|
"strings"
|
2022-09-16 10:53:45 +00:00
|
|
|
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2023-04-17 08:06:28 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2022-10-27 15:09:39 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cloud_storage"
|
2023-05-10 06:08:14 +00:00
|
|
|
fileUtils "github.com/1Panel-dev/1Panel/backend/utils/files"
|
2022-09-16 10:53:45 +00:00
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BackupService struct{}
|
|
|
|
|
|
|
|
type IBackupService interface {
|
2022-09-19 11:42:06 +00:00
|
|
|
List() ([]dto.BackupInfo, error)
|
2022-11-29 09:39:10 +00:00
|
|
|
SearchRecordsWithPage(search dto.RecordSearch) (int64, []dto.BackupRecords, error)
|
2023-07-04 08:16:12 +00:00
|
|
|
LoadOneDriveInfo() (string, error)
|
2022-10-28 03:02:47 +00:00
|
|
|
DownloadRecord(info dto.DownloadRecord) (string, error)
|
2022-09-16 10:53:45 +00:00
|
|
|
Create(backupDto dto.BackupOperate) error
|
|
|
|
GetBuckets(backupDto dto.ForBuckets) ([]interface{}, error)
|
2023-01-29 08:38:34 +00:00
|
|
|
Update(ireq dto.BackupOperate) error
|
2023-04-17 08:06:28 +00:00
|
|
|
Delete(id uint) error
|
2022-10-27 15:09:39 +00:00
|
|
|
BatchDeleteRecord(ids []uint) error
|
2022-09-29 08:15:59 +00:00
|
|
|
NewClient(backup *model.BackupAccount) (cloud_storage.CloudStorageClient, error)
|
2023-02-13 07:48:18 +00:00
|
|
|
|
2023-08-08 08:44:12 +00:00
|
|
|
ListFiles(req dto.BackupSearchFile) ([]string, error)
|
2023-02-21 11:06:24 +00: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 10:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIBackupService() IBackupService {
|
|
|
|
return &BackupService{}
|
|
|
|
}
|
|
|
|
|
2022-09-19 11:42:06 +00:00
|
|
|
func (u *BackupService) List() ([]dto.BackupInfo, error) {
|
|
|
|
ops, err := backupRepo.List(commonRepo.WithOrderBy("created_at desc"))
|
2022-09-16 10:53:45 +00:00
|
|
|
var dtobas []dto.BackupInfo
|
2023-04-11 04:10:25 +00:00
|
|
|
dtobas = append(dtobas, u.loadByType("LOCAL", ops))
|
2023-04-03 10:51:11 +00:00
|
|
|
dtobas = append(dtobas, u.loadByType("OSS", ops))
|
|
|
|
dtobas = append(dtobas, u.loadByType("S3", ops))
|
|
|
|
dtobas = append(dtobas, u.loadByType("SFTP", ops))
|
|
|
|
dtobas = append(dtobas, u.loadByType("MINIO", ops))
|
|
|
|
dtobas = append(dtobas, u.loadByType("COS", ops))
|
|
|
|
dtobas = append(dtobas, u.loadByType("KODO", ops))
|
2023-06-23 15:06:13 +00:00
|
|
|
dtobas = append(dtobas, u.loadByType("OneDrive", ops))
|
2022-09-19 11:42:06 +00:00
|
|
|
return dtobas, err
|
2022-09-16 10:53:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 09:39:10 +00:00
|
|
|
func (u *BackupService) SearchRecordsWithPage(search dto.RecordSearch) (int64, []dto.BackupRecords, error) {
|
2022-10-27 15:09:39 +00: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
|
|
|
|
}
|
|
|
|
|
2023-07-04 08:16:12 +00:00
|
|
|
func (u *BackupService) LoadOneDriveInfo() (string, error) {
|
|
|
|
OneDriveID, err := settingRepo.Get(settingRepo.WithByKey("OneDriveID"))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
idItem, err := base64.StdEncoding.DecodeString(OneDriveID.Value)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(idItem), err
|
|
|
|
}
|
|
|
|
|
2022-10-28 03:02:47 +00:00
|
|
|
func (u *BackupService) DownloadRecord(info dto.DownloadRecord) (string, error) {
|
|
|
|
if info.Source == "LOCAL" {
|
2022-10-28 10:46:14 +00:00
|
|
|
return info.FileDir + "/" + info.FileName, nil
|
2022-10-28 03:02:47 +00: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["bucket"] = backup.Bucket
|
|
|
|
switch backup.Type {
|
|
|
|
case constant.Sftp:
|
2022-12-22 07:03:43 +00:00
|
|
|
varMap["username"] = backup.AccessKey
|
2022-10-28 03:02:47 +00:00
|
|
|
varMap["password"] = backup.Credential
|
2023-04-03 10:51:11 +00:00
|
|
|
case constant.OSS, constant.S3, constant.MinIo, constant.Cos, constant.Kodo:
|
2022-12-22 07:03:43 +00:00
|
|
|
varMap["accessKey"] = backup.AccessKey
|
2022-10-28 03:02:47 +00:00
|
|
|
varMap["secretKey"] = backup.Credential
|
2023-06-23 15:06:13 +00:00
|
|
|
case constant.OneDrive:
|
|
|
|
varMap["accessToken"] = backup.Credential
|
2022-10-28 03:02:47 +00:00
|
|
|
}
|
2023-06-23 15:06:13 +00:00
|
|
|
backClient, err := cloud_storage.NewCloudStorageClient(backup.Type, varMap)
|
2022-10-28 03:02:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("new cloud storage client failed, err: %v", err)
|
|
|
|
}
|
2023-05-12 14:30:26 +00:00
|
|
|
targetPath := fmt.Sprintf("%s/download/%s/%s", constant.DataDir, info.FileDir, info.FileName)
|
2023-05-13 07:32:28 +00:00
|
|
|
if _, err := os.Stat(path.Dir(targetPath)); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(path.Dir(targetPath), os.ModePerm); err != nil {
|
|
|
|
global.LOG.Errorf("mkdir %s failed, err: %v", path.Dir(targetPath), err)
|
2022-10-28 03:02:47 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-12 14:30:26 +00:00
|
|
|
srcPath := fmt.Sprintf("%s/%s", info.FileDir, info.FileName)
|
2023-07-11 03:01:09 +00:00
|
|
|
if len(backup.BackupPath) != 0 {
|
|
|
|
itemPath := strings.TrimPrefix(backup.BackupPath, "/")
|
|
|
|
itemPath = strings.TrimSuffix(itemPath, "/") + "/"
|
|
|
|
srcPath = itemPath + srcPath
|
|
|
|
}
|
2023-05-12 14:30:26 +00:00
|
|
|
if exist, _ := backClient.Exist(srcPath); exist {
|
|
|
|
isOK, err := backClient.Download(srcPath, targetPath)
|
2022-10-28 03:02:47 +00:00
|
|
|
if !isOK {
|
|
|
|
return "", fmt.Errorf("cloud storage download failed, err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return targetPath, nil
|
|
|
|
}
|
|
|
|
|
2022-09-16 10:53:45 +00:00
|
|
|
func (u *BackupService) Create(backupDto dto.BackupOperate) error {
|
2022-09-19 11:42:06 +00:00
|
|
|
backup, _ := backupRepo.Get(commonRepo.WithByType(backupDto.Type))
|
2022-09-16 10:53:45 +00:00
|
|
|
if backup.ID != 0 {
|
|
|
|
return constant.ErrRecordExist
|
|
|
|
}
|
|
|
|
if err := copier.Copy(&backup, &backupDto); err != nil {
|
|
|
|
return errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
2023-06-23 15:06:13 +00:00
|
|
|
|
|
|
|
if backupDto.Type == constant.OneDrive {
|
|
|
|
if err := u.loadAccessToken(&backup); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-09-16 10:53:45 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
switch backupDto.Type {
|
|
|
|
case constant.Sftp:
|
2022-12-22 07:03:43 +00:00
|
|
|
varMap["username"] = backupDto.AccessKey
|
2022-09-16 10:53:45 +00:00
|
|
|
varMap["password"] = backupDto.Credential
|
2023-04-03 10:51:11 +00:00
|
|
|
case constant.OSS, constant.S3, constant.MinIo, constant.Cos, constant.Kodo:
|
2022-12-22 07:03:43 +00:00
|
|
|
varMap["accessKey"] = backupDto.AccessKey
|
2022-09-16 10:53:45 +00:00
|
|
|
varMap["secretKey"] = backupDto.Credential
|
|
|
|
}
|
2023-06-23 15:06:13 +00:00
|
|
|
client, err := cloud_storage.NewCloudStorageClient(backupDto.Type, varMap)
|
2022-09-16 10:53:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return client.ListBuckets()
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:06:28 +00:00
|
|
|
func (u *BackupService) Delete(id uint) error {
|
|
|
|
cronjobs, _ := cronjobRepo.List(cronjobRepo.WithByBackupID(id))
|
|
|
|
if len(cronjobs) != 0 {
|
|
|
|
return buserr.New(constant.ErrBackupInUsed)
|
|
|
|
}
|
|
|
|
return backupRepo.Delete(commonRepo.WithByID(id))
|
2022-09-16 10:53:45 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 15:09:39 +00: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 08:21:49 +00:00
|
|
|
if err := os.Remove(record.FileDir + "/" + record.FileName); err != nil {
|
2022-10-27 15:09:39 +00:00
|
|
|
global.LOG.Errorf("remove file %s failed, err: %v", record.FileDir+record.FileName, err)
|
|
|
|
}
|
|
|
|
} else {
|
2023-05-12 14:30:26 +00:00
|
|
|
backupAccount, err := backupRepo.Get(commonRepo.WithByType(record.Source))
|
2022-10-27 15:09:39 +00:00
|
|
|
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 07:54:34 +00:00
|
|
|
return backupRepo.DeleteRecord(context.Background(), commonRepo.WithIdsIn(ids))
|
2022-10-27 15:09:39 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 08:38:34 +00: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 08:56:16 +00:00
|
|
|
varMap := make(map[string]interface{})
|
2023-01-29 08:38:34 +00:00
|
|
|
if err := json.Unmarshal([]byte(req.Vars), &varMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-14 02:19:29 +00:00
|
|
|
|
|
|
|
oldVars := backup.Vars
|
|
|
|
oldDir, err := loadLocalDir()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-29 08:38:34 +00:00
|
|
|
upMap := make(map[string]interface{})
|
|
|
|
upMap["bucket"] = req.Bucket
|
|
|
|
upMap["credential"] = req.Credential
|
2023-07-06 06:30:25 +00:00
|
|
|
upMap["backup_path"] = req.BackupPath
|
2023-01-29 08:38:34 +00:00
|
|
|
upMap["vars"] = req.Vars
|
2023-06-23 15:06:13 +00:00
|
|
|
backup.Vars = req.Vars
|
|
|
|
|
|
|
|
if req.Type == constant.OneDrive {
|
|
|
|
if err := u.loadAccessToken(&backup); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
upMap["credential"] = backup.Credential
|
|
|
|
upMap["vars"] = backup.Vars
|
|
|
|
}
|
2023-01-29 08:38:34 +00:00
|
|
|
if err := backupRepo.Update(req.ID, upMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if backup.Type == "LOCAL" {
|
|
|
|
if dir, ok := varMap["dir"]; ok {
|
2023-02-23 08:56:16 +00:00
|
|
|
if dirStr, isStr := dir.(string); isStr {
|
2023-03-09 16:27:41 +00:00
|
|
|
if strings.HasSuffix(dirStr, "/") {
|
|
|
|
dirStr = dirStr[:strings.LastIndex(dirStr, "/")]
|
|
|
|
}
|
2023-05-10 06:08:14 +00:00
|
|
|
if err := copyDir(oldDir, dirStr); err != nil {
|
2023-03-14 02:19:29 +00:00
|
|
|
_ = backupRepo.Update(req.ID, (map[string]interface{}{"vars": oldVars}))
|
2023-02-23 08:56:16 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-01-29 08:38:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2022-09-16 10:53:45 +00:00
|
|
|
}
|
2022-09-29 08:15:59 +00:00
|
|
|
|
2023-08-08 08:44:12 +00:00
|
|
|
func (u *BackupService) ListFiles(req dto.BackupSearchFile) ([]string, error) {
|
2023-02-13 07:48:18 +00:00
|
|
|
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
|
|
|
|
}
|
2023-08-07 07:50:28 +00:00
|
|
|
prefix := "system_snapshot"
|
|
|
|
if len(backup.BackupPath) != 0 {
|
2023-08-08 08:44:12 +00:00
|
|
|
prefix = path.Join(strings.TrimPrefix(backup.BackupPath, "/"), prefix)
|
2023-08-07 07:50:28 +00:00
|
|
|
}
|
2023-08-08 08:44:12 +00:00
|
|
|
files, err := client.ListObjects(prefix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var datas []string
|
|
|
|
for _, file := range files {
|
|
|
|
if len(file) != 0 {
|
|
|
|
datas = append(datas, path.Base(file))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return datas, nil
|
2023-02-13 07:48:18 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 08:15:59 +00: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
|
|
|
|
}
|
|
|
|
if backup.Type == "LOCAL" {
|
|
|
|
return nil, errors.New("not support")
|
|
|
|
}
|
|
|
|
varMap["bucket"] = backup.Bucket
|
|
|
|
switch backup.Type {
|
|
|
|
case constant.Sftp:
|
2022-12-22 07:03:43 +00:00
|
|
|
varMap["username"] = backup.AccessKey
|
2022-09-29 08:15:59 +00:00
|
|
|
varMap["password"] = backup.Credential
|
2023-04-03 10:51:11 +00:00
|
|
|
case constant.OSS, constant.S3, constant.MinIo, constant.Cos, constant.Kodo:
|
2022-12-22 07:03:43 +00:00
|
|
|
varMap["accessKey"] = backup.AccessKey
|
2022-09-29 08:15:59 +00:00
|
|
|
varMap["secretKey"] = backup.Credential
|
2023-06-23 15:06:13 +00:00
|
|
|
case constant.OneDrive:
|
|
|
|
varMap["accessToken"] = backup.Credential
|
2022-09-29 08:15:59 +00:00
|
|
|
}
|
|
|
|
|
2023-06-23 15:06:13 +00:00
|
|
|
backClient, err := cloud_storage.NewCloudStorageClient(backup.Type, varMap)
|
2022-09-29 08:15:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return backClient, nil
|
|
|
|
}
|
2022-10-28 10:46:14 +00:00
|
|
|
|
2023-04-03 10:51:11 +00:00
|
|
|
func (u *BackupService) loadByType(accountType string, accounts []model.BackupAccount) dto.BackupInfo {
|
|
|
|
for _, account := range accounts {
|
|
|
|
if account.Type == accountType {
|
|
|
|
var item dto.BackupInfo
|
|
|
|
if err := copier.Copy(&item, &account); err != nil {
|
|
|
|
global.LOG.Errorf("copy backup account to dto backup info failed, err: %v", err)
|
|
|
|
}
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dto.BackupInfo{Type: accountType}
|
|
|
|
}
|
|
|
|
|
2023-06-23 15:06:13 +00:00
|
|
|
func (u *BackupService) loadAccessToken(backup *model.BackupAccount) error {
|
|
|
|
varMap := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal([]byte(backup.Vars), &varMap); err != nil {
|
|
|
|
return fmt.Errorf("unmarshal backup vars failed, err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data := url.Values{}
|
2023-07-04 08:16:12 +00:00
|
|
|
data.Set("client_id", global.CONF.System.OneDriveID)
|
|
|
|
data.Set("client_secret", global.CONF.System.OneDriveSc)
|
2023-06-23 15:06:13 +00:00
|
|
|
data.Set("grant_type", "authorization_code")
|
|
|
|
data.Set("code", varMap["code"].(string))
|
|
|
|
data.Set("redirect_uri", constant.OneDriveRedirectURI)
|
|
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest("POST", "https://login.microsoftonline.com/common/oauth2/v2.0/token", strings.NewReader(data.Encode()))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("new http post client for access token failed, err: %v", err)
|
|
|
|
}
|
|
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("request for access token failed, err: %v", err)
|
|
|
|
}
|
|
|
|
delete(varMap, "code")
|
|
|
|
respBody, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("read data from response body failed, err: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
token := map[string]interface{}{}
|
|
|
|
if err := json.Unmarshal(respBody, &token); err != nil {
|
|
|
|
return fmt.Errorf("unmarshal data from response body failed, err: %v", err)
|
|
|
|
}
|
|
|
|
accessToken, ok := token["refresh_token"].(string)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("no such access token in response")
|
|
|
|
}
|
|
|
|
|
|
|
|
itemVars, err := json.Marshal(varMap)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("json marshal var map failed, err: %v", err)
|
|
|
|
}
|
|
|
|
backup.Credential = accessToken
|
|
|
|
backup.Vars = string(itemVars)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-09 07:08:38 +00:00
|
|
|
func loadLocalDir() (string, error) {
|
|
|
|
backup, err := backupRepo.Get(commonRepo.WithByType("LOCAL"))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-10-28 10:46:14 +00: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 11:06:24 +00:00
|
|
|
return "", fmt.Errorf("mkdir %s failed, err: %v", baseDir, err)
|
2022-10-28 10:46:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return baseDir, nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("error type dir: %T", varMap["dir"])
|
|
|
|
}
|
2023-01-29 08:38:34 +00:00
|
|
|
|
2023-05-10 06:08:14 +00:00
|
|
|
func copyDir(src, dst string) error {
|
|
|
|
srcInfo, err := os.Stat(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-01-29 08:38:34 +00:00
|
|
|
}
|
2023-05-10 06:08:14 +00:00
|
|
|
if err = os.MkdirAll(dst, srcInfo.Mode()); err != nil {
|
|
|
|
return err
|
2023-01-29 08:38:34 +00:00
|
|
|
}
|
2023-05-10 06:08:14 +00:00
|
|
|
files, err := os.ReadDir(src)
|
2023-02-23 08:56:16 +00:00
|
|
|
if err != nil {
|
2023-05-10 06:08:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fileOP := fileUtils.NewFileOp()
|
|
|
|
for _, file := range files {
|
|
|
|
srcPath := fmt.Sprintf("%s/%s", src, file.Name())
|
|
|
|
dstPath := fmt.Sprintf("%s/%s", dst, file.Name())
|
|
|
|
if file.IsDir() {
|
|
|
|
if err = copyDir(srcPath, dstPath); err != nil {
|
|
|
|
global.LOG.Errorf("copy dir %s to %s failed, err: %v", srcPath, dstPath, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := fileOP.CopyFile(srcPath, dst); err != nil {
|
|
|
|
global.LOG.Errorf("copy file %s to %s failed, err: %v", srcPath, dstPath, err)
|
|
|
|
}
|
|
|
|
}
|
2023-01-29 08:38:34 +00:00
|
|
|
}
|
2023-05-10 06:08:14 +00:00
|
|
|
|
2023-01-29 08:38:34 +00:00
|
|
|
return nil
|
|
|
|
}
|