2022-09-29 16:15:59 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2023-04-11 17:00:29 +08:00
|
|
|
"context"
|
2022-09-29 16:15:59 +08:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-04-11 17:00:29 +08:00
|
|
|
"path"
|
2022-09-29 16:15:59 +08:00
|
|
|
"strings"
|
2023-05-23 18:54:05 +08:00
|
|
|
"sync"
|
2022-09-29 16:15:59 +08:00
|
|
|
"time"
|
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2023-04-07 10:18:10 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/repo"
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cloud_storage"
|
2023-02-07 10:29:55 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
2023-05-24 16:39:36 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
2023-05-24 17:25:35 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/ntp"
|
2022-09-29 16:15:59 +08:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (u *CronjobService) HandleJob(cronjob *model.Cronjob) {
|
|
|
|
var (
|
|
|
|
message []byte
|
|
|
|
err error
|
|
|
|
)
|
2023-03-18 10:03:40 +08:00
|
|
|
record := cronjobRepo.StartRecords(cronjob.ID, cronjob.KeepLocal, "")
|
2023-02-24 18:50:35 +08:00
|
|
|
go func() {
|
|
|
|
switch cronjob.Type {
|
|
|
|
case "shell":
|
|
|
|
if len(cronjob.Script) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2023-05-24 16:39:36 +08:00
|
|
|
message, err = u.handleShell(cronjob.Type, cronjob.Name, cronjob.Script)
|
|
|
|
u.HandleRmExpired("LOCAL", "", cronjob, nil)
|
|
|
|
case "curl":
|
|
|
|
if len(cronjob.URL) == 0 {
|
|
|
|
return
|
2023-02-24 18:50:35 +08:00
|
|
|
}
|
2023-05-24 16:39:36 +08:00
|
|
|
message, err = u.handleShell(cronjob.Type, cronjob.Name, fmt.Sprintf("curl '%s'", cronjob.URL))
|
2023-04-11 17:00:29 +08:00
|
|
|
u.HandleRmExpired("LOCAL", "", cronjob, nil)
|
2023-05-24 17:25:35 +08:00
|
|
|
case "ntp":
|
|
|
|
err = u.handleNtpSync()
|
|
|
|
u.HandleRmExpired("LOCAL", "", cronjob, nil)
|
2023-02-24 18:50:35 +08:00
|
|
|
case "website":
|
2023-05-24 16:39:36 +08:00
|
|
|
record.File, err = u.handleBackup(cronjob, record.StartTime)
|
2023-02-24 18:50:35 +08:00
|
|
|
case "database":
|
2023-05-24 16:39:36 +08:00
|
|
|
record.File, err = u.handleBackup(cronjob, record.StartTime)
|
2023-02-24 18:50:35 +08:00
|
|
|
case "directory":
|
|
|
|
if len(cronjob.SourceDir) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2023-05-24 16:39:36 +08:00
|
|
|
record.File, err = u.handleBackup(cronjob, record.StartTime)
|
2023-05-23 18:54:05 +08:00
|
|
|
case "cutWebsiteLog":
|
|
|
|
record.File, err = u.handleCutWebsiteLog(cronjob, record.StartTime)
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("cut website log file failed, err: %v", err)
|
|
|
|
}
|
2022-09-29 16:15:59 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
2023-02-24 18:50:35 +08:00
|
|
|
cronjobRepo.EndRecords(record, constant.StatusFailed, err.Error(), string(message))
|
|
|
|
return
|
2022-09-29 16:15:59 +08:00
|
|
|
}
|
2023-02-24 18:50:35 +08:00
|
|
|
if len(message) != 0 {
|
|
|
|
record.Records, err = mkdirAndWriteFile(cronjob, record.StartTime, message)
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("save file %s failed, err: %v", record.Records, err)
|
|
|
|
}
|
2022-12-14 19:34:42 +08:00
|
|
|
}
|
2023-02-24 18:50:35 +08:00
|
|
|
cronjobRepo.EndRecords(record, constant.StatusSuccess, "", record.Records)
|
|
|
|
}()
|
2022-09-29 16:15:59 +08:00
|
|
|
}
|
|
|
|
|
2023-05-24 16:39:36 +08:00
|
|
|
func (u *CronjobService) handleShell(cronType, cornName, script string) ([]byte, error) {
|
|
|
|
handleDir := fmt.Sprintf("%s/task/%s/%s", constant.DataDir, cronType, cornName)
|
|
|
|
if _, err := os.Stat(handleDir); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(handleDir, os.ModePerm); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2023-06-06 18:29:22 +08:00
|
|
|
stdout, err := cmd.ExecCronjobWithTimeOut(script, handleDir, 24*time.Hour)
|
2023-05-24 16:39:36 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []byte(stdout), nil
|
|
|
|
}
|
|
|
|
|
2023-05-24 17:25:35 +08:00
|
|
|
func (u *CronjobService) handleNtpSync() error {
|
|
|
|
ntpServer, err := settingRepo.Get(settingRepo.WithByKey("NtpSite"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ntime, err := ntp.GetRemoteTime(ntpServer.Value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := ntp.UpdateSystemTime(ntime.Format("2006-01-02 15:04:05")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:39:36 +08:00
|
|
|
func (u *CronjobService) handleBackup(cronjob *model.Cronjob, startTime time.Time) (string, error) {
|
2022-09-29 16:15:59 +08:00
|
|
|
backup, err := backupRepo.Get(commonRepo.WithByID(uint(cronjob.TargetDirID)))
|
|
|
|
if err != nil {
|
2022-10-28 18:46:14 +08:00
|
|
|
return "", err
|
2022-09-29 16:15:59 +08:00
|
|
|
}
|
2023-02-21 19:06:24 +08:00
|
|
|
localDir, err := loadLocalDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2022-10-28 18:46:14 +08:00
|
|
|
}
|
2023-02-21 19:06:24 +08:00
|
|
|
global.LOG.Infof("start to backup %s %s to %s", cronjob.Type, cronjob.Name, backup.Type)
|
2022-10-28 18:46:14 +08:00
|
|
|
|
2022-11-30 15:47:11 +08:00
|
|
|
switch cronjob.Type {
|
|
|
|
case "database":
|
2022-12-21 12:21:27 +08:00
|
|
|
app, err := appInstallRepo.LoadBaseInfo("mysql", "")
|
2022-11-29 17:39:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-04-11 17:00:29 +08:00
|
|
|
paths, err := u.handleDatabase(*cronjob, app, backup, startTime)
|
|
|
|
return strings.Join(paths, ","), err
|
2022-11-30 15:47:11 +08:00
|
|
|
case "website":
|
2023-04-11 17:00:29 +08:00
|
|
|
paths, err := u.handleWebsite(*cronjob, backup, startTime)
|
|
|
|
return strings.Join(paths, ","), err
|
2022-11-30 15:47:11 +08:00
|
|
|
default:
|
2023-04-07 10:18:10 +08:00
|
|
|
fileName := fmt.Sprintf("directory%s_%s.tar.gz", strings.ReplaceAll(cronjob.SourceDir, "/", "_"), startTime.Format("20060102150405"))
|
|
|
|
backupDir := fmt.Sprintf("%s/%s/%s", localDir, cronjob.Type, cronjob.Name)
|
|
|
|
itemFileDir := fmt.Sprintf("%s/%s", cronjob.Type, cronjob.Name)
|
2022-12-28 18:35:53 +08:00
|
|
|
global.LOG.Infof("handle tar %s to %s", backupDir, fileName)
|
2023-03-10 00:27:41 +08:00
|
|
|
if err := handleTar(cronjob.SourceDir, backupDir, fileName, cronjob.ExclusionRules); err != nil {
|
2023-02-21 19:06:24 +08:00
|
|
|
return "", err
|
|
|
|
}
|
2023-04-11 17:00:29 +08:00
|
|
|
var client cloud_storage.CloudStorageClient
|
|
|
|
if backup.Type != "LOCAL" {
|
|
|
|
if !cronjob.KeepLocal {
|
|
|
|
defer func() {
|
|
|
|
_ = os.RemoveAll(fmt.Sprintf("%s/%s", backupDir, fileName))
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
client, err = NewIBackupService().NewClient(&backup)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if _, err = client.Upload(backupDir+"/"+fileName, itemFileDir+"/"+fileName); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-04-07 10:18:10 +08:00
|
|
|
}
|
2023-04-11 17:00:29 +08:00
|
|
|
u.HandleRmExpired(backup.Type, localDir, cronjob, client)
|
|
|
|
if backup.Type == "LOCAL" || cronjob.KeepLocal {
|
|
|
|
return fmt.Sprintf("%s/%s/%s/%s", localDir, cronjob.Type, cronjob.Name, fileName), nil
|
2023-04-07 10:18:10 +08:00
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s/%s/%s", cronjob.Type, cronjob.Name, fileName), nil
|
2022-09-29 16:15:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-11 17:00:29 +08:00
|
|
|
func (u *CronjobService) HandleRmExpired(backType, localDir string, cronjob *model.Cronjob, backClient cloud_storage.CloudStorageClient) {
|
2022-12-28 18:35:53 +08:00
|
|
|
global.LOG.Infof("start to handle remove expired, retain copies: %d", cronjob.RetainCopies)
|
2023-04-09 22:22:13 +08:00
|
|
|
records, _ := cronjobRepo.ListRecord(cronjobRepo.WithByJobID(int(cronjob.ID)), commonRepo.WithOrderBy("created_at desc"))
|
2022-09-29 16:15:59 +08:00
|
|
|
if len(records) > int(cronjob.RetainCopies) {
|
|
|
|
for i := int(cronjob.RetainCopies); i < len(records); i++ {
|
2023-05-24 17:25:35 +08:00
|
|
|
if len(records[i].File) != 0 {
|
|
|
|
files := strings.Split(records[i].File, ",")
|
|
|
|
for _, file := range files {
|
|
|
|
if backType != "LOCAL" {
|
|
|
|
_, _ = backClient.Delete(strings.ReplaceAll(file, localDir+"/", ""))
|
|
|
|
_ = os.Remove(file)
|
|
|
|
} else {
|
|
|
|
_ = os.Remove(file)
|
|
|
|
}
|
|
|
|
_ = backupRepo.DeleteRecord(context.TODO(), backupRepo.WithByFileName(path.Base(file)))
|
2023-04-11 17:00:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-09 22:22:13 +08:00
|
|
|
_ = cronjobRepo.DeleteRecord(commonRepo.WithByID(uint(records[i].ID)))
|
|
|
|
_ = os.Remove(records[i].Records)
|
2022-09-29 16:15:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 18:46:14 +08:00
|
|
|
func handleTar(sourceDir, targetDir, name, exclusionRules string) error {
|
2022-09-29 16:15:59 +08:00
|
|
|
if _, err := os.Stat(targetDir); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(targetDir, os.ModePerm); err != nil {
|
2022-10-28 18:46:14 +08:00
|
|
|
return err
|
2022-09-29 16:15:59 +08:00
|
|
|
}
|
|
|
|
}
|
2023-02-14 14:19:26 +08:00
|
|
|
|
2023-06-05 18:31:26 +08:00
|
|
|
excludes := strings.Split(exclusionRules, ",")
|
2023-02-14 14:19:26 +08:00
|
|
|
excludeRules := ""
|
2022-09-29 16:15:59 +08:00
|
|
|
for _, exclude := range excludes {
|
|
|
|
if len(exclude) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2023-05-23 18:54:05 +08:00
|
|
|
excludeRules += " --exclude " + exclude
|
2022-09-29 16:15:59 +08:00
|
|
|
}
|
2023-02-14 14:19:26 +08:00
|
|
|
path := ""
|
2023-02-24 12:26:23 +08:00
|
|
|
if strings.Contains(sourceDir, "/") {
|
2022-09-29 16:15:59 +08:00
|
|
|
itemDir := strings.ReplaceAll(sourceDir[strings.LastIndex(sourceDir, "/"):], "/", "")
|
|
|
|
aheadDir := strings.ReplaceAll(sourceDir, itemDir, "")
|
2023-02-14 14:19:26 +08:00
|
|
|
path += fmt.Sprintf("-C %s %s", aheadDir, itemDir)
|
2022-09-29 16:15:59 +08:00
|
|
|
} else {
|
2023-02-14 14:19:26 +08:00
|
|
|
path = sourceDir
|
2022-09-29 16:15:59 +08:00
|
|
|
}
|
2023-02-14 14:19:26 +08:00
|
|
|
|
2023-03-08 17:02:06 +08:00
|
|
|
commands := fmt.Sprintf("tar zcvf %s %s %s", targetDir+"/"+name, excludeRules, path)
|
|
|
|
global.LOG.Debug(commands)
|
2023-04-17 14:03:01 +08:00
|
|
|
stdout, err := cmd.ExecWithTimeOut(commands, 5*time.Minute)
|
2022-10-28 18:46:14 +08:00
|
|
|
if err != nil {
|
2023-03-08 17:02:06 +08:00
|
|
|
global.LOG.Errorf("do handle tar failed, stdout: %s, err: %v", stdout, err)
|
|
|
|
return errors.New(stdout)
|
2022-10-28 18:46:14 +08:00
|
|
|
}
|
|
|
|
return nil
|
2022-09-29 16:15:59 +08:00
|
|
|
}
|
2022-11-04 19:02:15 +08:00
|
|
|
|
|
|
|
func handleUnTar(sourceFile, targetDir string) error {
|
|
|
|
if _, err := os.Stat(targetDir); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(targetDir, os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 17:02:06 +08:00
|
|
|
commands := fmt.Sprintf("tar zxvfC %s %s", sourceFile, targetDir)
|
|
|
|
global.LOG.Debug(commands)
|
2023-04-17 14:03:01 +08:00
|
|
|
stdout, err := cmd.ExecWithTimeOut(commands, 5*time.Minute)
|
2022-11-04 19:02:15 +08:00
|
|
|
if err != nil {
|
2023-03-08 17:02:06 +08:00
|
|
|
global.LOG.Errorf("do handle untar failed, stdout: %s, err: %v", stdout, err)
|
|
|
|
return errors.New(stdout)
|
2022-11-04 19:02:15 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-07 10:18:10 +08:00
|
|
|
|
2023-04-11 17:00:29 +08:00
|
|
|
func (u *CronjobService) handleDatabase(cronjob model.Cronjob, app *repo.RootInfo, backup model.BackupAccount, startTime time.Time) ([]string, error) {
|
|
|
|
var paths []string
|
2023-04-07 10:18:10 +08:00
|
|
|
localDir, err := loadLocalDir()
|
|
|
|
if err != nil {
|
2023-04-11 17:00:29 +08:00
|
|
|
return paths, err
|
2023-04-07 10:18:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var dblist []string
|
|
|
|
if cronjob.DBName == "all" {
|
|
|
|
mysqlService := NewIMysqlService()
|
|
|
|
dblist, err = mysqlService.ListDBName()
|
|
|
|
if err != nil {
|
2023-04-11 17:00:29 +08:00
|
|
|
return paths, err
|
2023-04-07 10:18:10 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dblist = append(dblist, cronjob.DBName)
|
|
|
|
}
|
2023-04-11 17:00:29 +08:00
|
|
|
|
|
|
|
var client cloud_storage.CloudStorageClient
|
|
|
|
if backup.Type != "LOCAL" {
|
|
|
|
client, err = NewIBackupService().NewClient(&backup)
|
|
|
|
if err != nil {
|
|
|
|
return paths, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-07 10:18:10 +08:00
|
|
|
for _, dbName := range dblist {
|
2023-04-11 17:00:29 +08:00
|
|
|
var record model.BackupRecord
|
|
|
|
|
|
|
|
record.Type = "mysql"
|
|
|
|
record.Name = app.Name
|
|
|
|
record.Source = "LOCAL"
|
|
|
|
record.BackupType = backup.Type
|
|
|
|
|
2023-04-07 10:18:10 +08:00
|
|
|
backupDir := fmt.Sprintf("%s/database/mysql/%s/%s", localDir, app.Name, dbName)
|
|
|
|
record.FileName = fmt.Sprintf("db_%s_%s.sql.gz", dbName, startTime.Format("20060102150405"))
|
|
|
|
if err = handleMysqlBackup(app, backupDir, dbName, record.FileName); err != nil {
|
2023-04-11 17:00:29 +08:00
|
|
|
return paths, err
|
2023-04-07 10:18:10 +08:00
|
|
|
}
|
|
|
|
record.DetailName = dbName
|
|
|
|
record.FileDir = backupDir
|
|
|
|
itemFileDir := strings.ReplaceAll(backupDir, localDir+"/", "")
|
|
|
|
if !cronjob.KeepLocal && backup.Type != "LOCAL" {
|
|
|
|
record.Source = backup.Type
|
|
|
|
record.FileDir = itemFileDir
|
|
|
|
}
|
2023-04-11 17:00:29 +08:00
|
|
|
paths = append(paths, fmt.Sprintf("%s/%s", record.FileDir, record.FileName))
|
|
|
|
|
2023-04-09 22:22:13 +08:00
|
|
|
if err := backupRepo.CreateRecord(&record); err != nil {
|
|
|
|
global.LOG.Errorf("save backup record failed, err: %v", err)
|
2023-04-11 17:00:29 +08:00
|
|
|
return paths, err
|
2023-04-07 10:18:10 +08:00
|
|
|
}
|
2023-04-11 17:00:29 +08:00
|
|
|
if backup.Type != "LOCAL" {
|
|
|
|
if !cronjob.KeepLocal {
|
|
|
|
defer func() {
|
|
|
|
_ = os.RemoveAll(fmt.Sprintf("%s/%s", backupDir, record.FileName))
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
if _, err = client.Upload(backupDir+"/"+record.FileName, itemFileDir+"/"+record.FileName); err != nil {
|
|
|
|
return paths, err
|
|
|
|
}
|
2023-04-07 10:18:10 +08:00
|
|
|
}
|
|
|
|
}
|
2023-04-11 17:00:29 +08:00
|
|
|
u.HandleRmExpired(backup.Type, localDir, &cronjob, client)
|
|
|
|
return paths, nil
|
2023-04-07 10:18:10 +08:00
|
|
|
}
|
|
|
|
|
2023-05-23 18:54:05 +08:00
|
|
|
func (u *CronjobService) handleCutWebsiteLog(cronjob *model.Cronjob, startTime time.Time) (string, error) {
|
|
|
|
var (
|
|
|
|
websites []string
|
|
|
|
err error
|
|
|
|
filePaths []string
|
|
|
|
)
|
|
|
|
if cronjob.Website == "all" {
|
|
|
|
websites, _ = NewIWebsiteService().GetWebsiteOptions()
|
|
|
|
if len(websites) == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
websites = append(websites, cronjob.Website)
|
|
|
|
}
|
|
|
|
|
|
|
|
nginx, err := getAppInstallByKey(constant.AppOpenresty)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
baseDir := path.Join(nginx.GetPath(), "www", "sites")
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(websites))
|
|
|
|
for _, websiteName := range websites {
|
|
|
|
name := websiteName
|
|
|
|
go func() {
|
|
|
|
website, _ := websiteRepo.GetFirst(websiteRepo.WithDomain(name))
|
|
|
|
if website.ID == 0 {
|
|
|
|
wg.Done()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
websiteLogDir := path.Join(baseDir, website.PrimaryDomain, "log")
|
|
|
|
srcAccessLogPath := path.Join(websiteLogDir, "access.log")
|
|
|
|
srcErrorLogPath := path.Join(websiteLogDir, "error.log")
|
|
|
|
dstLogDir := path.Join(global.CONF.System.Backup, "log", "website", website.PrimaryDomain)
|
|
|
|
if !fileOp.Stat(dstLogDir) {
|
|
|
|
_ = os.MkdirAll(dstLogDir, 0755)
|
|
|
|
}
|
|
|
|
|
|
|
|
dstName := fmt.Sprintf("%s_log_%s.gz", website.PrimaryDomain, startTime.Format("20060102150405"))
|
|
|
|
filePaths = append(filePaths, path.Join(dstLogDir, dstName))
|
|
|
|
if err = fileOp.Compress([]string{srcAccessLogPath, srcErrorLogPath}, dstLogDir, dstName, files.Gz); err != nil {
|
|
|
|
global.LOG.Errorf("There was an error in compressing the website[%s] access.log", website.PrimaryDomain)
|
|
|
|
} else {
|
|
|
|
_ = fileOp.WriteFile(srcAccessLogPath, strings.NewReader(""), 0755)
|
|
|
|
_ = fileOp.WriteFile(srcErrorLogPath, strings.NewReader(""), 0755)
|
|
|
|
}
|
|
|
|
global.LOG.Infof("The website[%s] log file was successfully rotated in the directory [%s]", website.PrimaryDomain, dstLogDir)
|
|
|
|
var record model.BackupRecord
|
|
|
|
record.Type = "cutWebsiteLog"
|
|
|
|
record.Name = cronjob.Website
|
|
|
|
record.Source = "LOCAL"
|
|
|
|
record.BackupType = "LOCAL"
|
|
|
|
record.FileDir = dstLogDir
|
|
|
|
record.FileName = dstName
|
|
|
|
if err = backupRepo.CreateRecord(&record); err != nil {
|
|
|
|
global.LOG.Errorf("save backup record failed, err: %v", err)
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
u.HandleRmExpired("LOCAL", "", cronjob, nil)
|
|
|
|
return strings.Join(filePaths, ","), nil
|
|
|
|
}
|
|
|
|
|
2023-04-11 17:00:29 +08:00
|
|
|
func (u *CronjobService) handleWebsite(cronjob model.Cronjob, backup model.BackupAccount, startTime time.Time) ([]string, error) {
|
|
|
|
var paths []string
|
2023-04-07 10:18:10 +08:00
|
|
|
localDir, err := loadLocalDir()
|
|
|
|
if err != nil {
|
2023-04-11 17:00:29 +08:00
|
|
|
return paths, err
|
2023-04-07 10:18:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var weblist []string
|
|
|
|
if cronjob.Website == "all" {
|
|
|
|
weblist, err = NewIWebsiteService().GetWebsiteOptions()
|
|
|
|
if err != nil {
|
2023-04-11 17:00:29 +08:00
|
|
|
return paths, err
|
2023-04-07 10:18:10 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
weblist = append(weblist, cronjob.Website)
|
|
|
|
}
|
2023-04-11 17:00:29 +08:00
|
|
|
|
|
|
|
var client cloud_storage.CloudStorageClient
|
|
|
|
if backup.Type != "LOCAL" {
|
|
|
|
client, err = NewIBackupService().NewClient(&backup)
|
|
|
|
if err != nil {
|
|
|
|
return paths, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-07 10:18:10 +08:00
|
|
|
for _, websiteItem := range weblist {
|
2023-04-11 17:00:29 +08:00
|
|
|
var record model.BackupRecord
|
|
|
|
record.Type = "website"
|
|
|
|
record.Name = cronjob.Website
|
|
|
|
record.Source = "LOCAL"
|
|
|
|
record.BackupType = backup.Type
|
2023-04-07 10:18:10 +08:00
|
|
|
website, err := websiteRepo.GetFirst(websiteRepo.WithDomain(websiteItem))
|
|
|
|
if err != nil {
|
2023-04-11 17:00:29 +08:00
|
|
|
return paths, err
|
2023-04-07 10:18:10 +08:00
|
|
|
}
|
|
|
|
backupDir := fmt.Sprintf("%s/website/%s", localDir, website.PrimaryDomain)
|
|
|
|
record.FileDir = backupDir
|
|
|
|
itemFileDir := strings.ReplaceAll(backupDir, localDir+"/", "")
|
|
|
|
if !cronjob.KeepLocal && backup.Type != "LOCAL" {
|
|
|
|
record.Source = backup.Type
|
|
|
|
record.FileDir = strings.ReplaceAll(backupDir, localDir+"/", "")
|
|
|
|
}
|
|
|
|
record.FileName = fmt.Sprintf("website_%s_%s.tar.gz", website.PrimaryDomain, startTime.Format("20060102150405"))
|
2023-04-11 17:00:29 +08:00
|
|
|
paths = append(paths, fmt.Sprintf("%s/%s", record.FileDir, record.FileName))
|
2023-04-07 10:18:10 +08:00
|
|
|
if err := handleWebsiteBackup(&website, backupDir, record.FileName); err != nil {
|
2023-04-11 17:00:29 +08:00
|
|
|
return paths, err
|
2023-04-07 10:18:10 +08:00
|
|
|
}
|
|
|
|
record.Name = website.PrimaryDomain
|
2023-04-09 22:22:13 +08:00
|
|
|
if err := backupRepo.CreateRecord(&record); err != nil {
|
|
|
|
global.LOG.Errorf("save backup record failed, err: %v", err)
|
2023-04-11 17:00:29 +08:00
|
|
|
return paths, err
|
2023-04-07 10:18:10 +08:00
|
|
|
}
|
2023-04-11 17:00:29 +08:00
|
|
|
if backup.Type != "LOCAL" {
|
|
|
|
if !cronjob.KeepLocal {
|
|
|
|
defer func() {
|
|
|
|
_ = os.RemoveAll(fmt.Sprintf("%s/%s", backupDir, record.FileName))
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
if _, err = client.Upload(backupDir+"/"+record.FileName, itemFileDir+"/"+record.FileName); err != nil {
|
|
|
|
return paths, err
|
|
|
|
}
|
2023-04-07 10:18:10 +08:00
|
|
|
}
|
|
|
|
}
|
2023-04-11 17:00:29 +08:00
|
|
|
u.HandleRmExpired(backup.Type, localDir, &cronjob, client)
|
|
|
|
return paths, nil
|
2023-04-07 10:18:10 +08:00
|
|
|
}
|