2022-09-29 08:15:59 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2023-04-11 09:00:29 +00:00
|
|
|
"context"
|
2022-09-29 08:15:59 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-04-11 09:00:29 +00:00
|
|
|
"path"
|
2024-08-28 05:45:22 +00:00
|
|
|
pathUtils "path"
|
2022-09-29 08:15:59 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2024-01-07 14:52:41 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/i18n"
|
|
|
|
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2024-01-24 09:41:56 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/repo"
|
2022-10-17 08:32:31 +00: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 02:29:55 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
2023-05-24 08:39:36 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
2023-05-24 09:25:35 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/ntp"
|
2022-09-29 08:15:59 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (u *CronjobService) HandleJob(cronjob *model.Cronjob) {
|
|
|
|
var (
|
|
|
|
message []byte
|
|
|
|
err error
|
|
|
|
)
|
2023-03-18 02:03:40 +00:00
|
|
|
record := cronjobRepo.StartRecords(cronjob.ID, cronjob.KeepLocal, "")
|
2023-02-24 10:50:35 +00:00
|
|
|
go func() {
|
|
|
|
switch cronjob.Type {
|
|
|
|
case "shell":
|
|
|
|
if len(cronjob.Script) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2024-01-24 09:41:56 +00:00
|
|
|
record.Records = u.generateLogsPath(*cronjob, record.StartTime)
|
|
|
|
_ = cronjobRepo.UpdateRecords(record.ID, map[string]interface{}{"records": record.Records})
|
|
|
|
script := cronjob.Script
|
2023-06-28 06:30:11 +00:00
|
|
|
if len(cronjob.ContainerName) != 0 {
|
2024-04-03 08:50:07 +00:00
|
|
|
command := "sh"
|
|
|
|
if len(cronjob.Command) != 0 {
|
|
|
|
command = cronjob.Command
|
|
|
|
}
|
|
|
|
script = fmt.Sprintf("docker exec %s %s -c \"%s\"", cronjob.ContainerName, command, strings.ReplaceAll(cronjob.Script, "\"", "\\\""))
|
2023-06-28 06:30:11 +00:00
|
|
|
}
|
2024-01-24 09:41:56 +00:00
|
|
|
err = u.handleShell(cronjob.Type, cronjob.Name, script, record.Records)
|
|
|
|
u.removeExpiredLog(*cronjob)
|
2023-05-24 08:39:36 +00:00
|
|
|
case "curl":
|
|
|
|
if len(cronjob.URL) == 0 {
|
|
|
|
return
|
2023-02-24 10:50:35 +00:00
|
|
|
}
|
2024-01-24 09:41:56 +00:00
|
|
|
record.Records = u.generateLogsPath(*cronjob, record.StartTime)
|
|
|
|
_ = cronjobRepo.UpdateRecords(record.ID, map[string]interface{}{"records": record.Records})
|
|
|
|
err = u.handleShell(cronjob.Type, cronjob.Name, fmt.Sprintf("curl '%s'", cronjob.URL), record.Records)
|
|
|
|
u.removeExpiredLog(*cronjob)
|
2023-05-24 09:25:35 +00:00
|
|
|
case "ntp":
|
|
|
|
err = u.handleNtpSync()
|
2024-01-24 09:41:56 +00:00
|
|
|
u.removeExpiredLog(*cronjob)
|
2023-05-23 10:54:05 +00:00
|
|
|
case "cutWebsiteLog":
|
2023-12-29 10:39:39 +00:00
|
|
|
var messageItem []string
|
|
|
|
messageItem, record.File, err = u.handleCutWebsiteLog(cronjob, record.StartTime)
|
|
|
|
message = []byte(strings.Join(messageItem, "\n"))
|
2023-10-18 09:08:24 +00:00
|
|
|
case "clean":
|
|
|
|
messageItem := ""
|
|
|
|
messageItem, err = u.handleSystemClean()
|
|
|
|
message = []byte(messageItem)
|
2024-01-24 09:41:56 +00:00
|
|
|
u.removeExpiredLog(*cronjob)
|
|
|
|
case "website":
|
|
|
|
err = u.handleWebsite(*cronjob, record.StartTime)
|
|
|
|
case "app":
|
|
|
|
err = u.handleApp(*cronjob, record.StartTime)
|
|
|
|
case "database":
|
|
|
|
err = u.handleDatabase(*cronjob, record.StartTime)
|
|
|
|
case "directory":
|
|
|
|
if len(cronjob.SourceDir) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = u.handleDirectory(*cronjob, record.StartTime)
|
2023-10-20 03:32:45 +00:00
|
|
|
case "log":
|
2024-01-24 09:41:56 +00:00
|
|
|
err = u.handleSystemLog(*cronjob, record.StartTime)
|
|
|
|
case "snapshot":
|
|
|
|
record.Records = u.generateLogsPath(*cronjob, record.StartTime)
|
|
|
|
_ = cronjobRepo.UpdateRecords(record.ID, map[string]interface{}{"records": record.Records})
|
|
|
|
err = u.handleSnapshot(*cronjob, record.StartTime, record.Records)
|
2022-09-29 08:15:59 +00:00
|
|
|
}
|
2023-08-24 14:26:16 +00:00
|
|
|
|
2022-09-29 08:15:59 +00:00
|
|
|
if err != nil {
|
2024-03-06 02:36:07 +00:00
|
|
|
if len(message) != 0 {
|
|
|
|
record.Records, _ = mkdirAndWriteFile(cronjob, record.StartTime, message)
|
|
|
|
}
|
|
|
|
cronjobRepo.EndRecords(record, constant.StatusFailed, err.Error(), record.Records)
|
2023-02-24 10:50:35 +00:00
|
|
|
return
|
2022-09-29 08:15:59 +00:00
|
|
|
}
|
2023-02-24 10:50:35 +00: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 11:34:42 +00:00
|
|
|
}
|
2023-02-24 10:50:35 +00:00
|
|
|
cronjobRepo.EndRecords(record, constant.StatusSuccess, "", record.Records)
|
|
|
|
}()
|
2022-09-29 08:15:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-24 09:41:56 +00:00
|
|
|
func (u *CronjobService) handleShell(cronType, cornName, script, logPath string) error {
|
2023-05-24 08:39:36 +00:00
|
|
|
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 {
|
2024-01-24 09:41:56 +00:00
|
|
|
return err
|
2023-05-24 08:39:36 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-24 09:41:56 +00:00
|
|
|
if err := cmd.ExecCronjobWithTimeOut(script, handleDir, logPath, 24*time.Hour); err != nil {
|
|
|
|
return err
|
2023-05-24 08:39:36 +00:00
|
|
|
}
|
2024-01-24 09:41:56 +00:00
|
|
|
return nil
|
2023-05-24 08:39:36 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 09:25:35 +00: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
|
|
|
|
}
|
2024-06-28 06:04:08 +00:00
|
|
|
if err := ntp.UpdateSystemTime(ntime.Format(constant.DateTimeLayout)); err != nil {
|
2023-05-24 09:25:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-04 08:48:31 +00:00
|
|
|
func handleTar(sourceDir, targetDir, name, exclusionRules string, secret string) error {
|
2022-09-29 08:15:59 +00:00
|
|
|
if _, err := os.Stat(targetDir); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(targetDir, os.ModePerm); err != nil {
|
2022-10-28 10:46:14 +00:00
|
|
|
return err
|
2022-09-29 08:15:59 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-14 06:19:26 +00:00
|
|
|
|
2023-06-05 10:31:26 +00:00
|
|
|
excludes := strings.Split(exclusionRules, ",")
|
2023-02-14 06:19:26 +00:00
|
|
|
excludeRules := ""
|
2022-09-29 08:15:59 +00:00
|
|
|
for _, exclude := range excludes {
|
|
|
|
if len(exclude) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2023-05-23 10:54:05 +00:00
|
|
|
excludeRules += " --exclude " + exclude
|
2022-09-29 08:15:59 +00:00
|
|
|
}
|
2023-02-14 06:19:26 +00:00
|
|
|
path := ""
|
2023-02-24 04:26:23 +00:00
|
|
|
if strings.Contains(sourceDir, "/") {
|
2022-09-29 08:15:59 +00:00
|
|
|
itemDir := strings.ReplaceAll(sourceDir[strings.LastIndex(sourceDir, "/"):], "/", "")
|
2023-06-09 03:42:11 +00:00
|
|
|
aheadDir := sourceDir[:strings.LastIndex(sourceDir, "/")]
|
2023-06-25 13:14:14 +00:00
|
|
|
if len(aheadDir) == 0 {
|
|
|
|
aheadDir = "/"
|
|
|
|
}
|
2023-02-14 06:19:26 +00:00
|
|
|
path += fmt.Sprintf("-C %s %s", aheadDir, itemDir)
|
2022-09-29 08:15:59 +00:00
|
|
|
} else {
|
2023-02-14 06:19:26 +00:00
|
|
|
path = sourceDir
|
2022-09-29 08:15:59 +00:00
|
|
|
}
|
2023-02-14 06:19:26 +00:00
|
|
|
|
2024-06-04 08:48:31 +00:00
|
|
|
commands := ""
|
2024-06-06 10:43:38 +00:00
|
|
|
|
|
|
|
if len(secret) != 0 {
|
2024-06-27 03:09:55 +00:00
|
|
|
extraCmd := "| openssl enc -aes-256-cbc -salt -k '" + secret + "' -out"
|
2024-08-28 05:45:22 +00:00
|
|
|
commands = fmt.Sprintf("tar --warning=no-file-changed --ignore-failed-read --exclude-from=<(find %s -type s -print) -zcf %s %s %s %s", sourceDir, " -"+excludeRules, path, extraCmd, targetDir+"/"+name)
|
2024-06-05 06:31:26 +00:00
|
|
|
global.LOG.Debug(strings.ReplaceAll(commands, fmt.Sprintf(" %s ", secret), "******"))
|
|
|
|
} else {
|
2024-08-28 05:45:22 +00:00
|
|
|
itemPrefix := pathUtils.Base(sourceDir)
|
|
|
|
if itemPrefix == "/" {
|
|
|
|
itemPrefix = ""
|
|
|
|
}
|
|
|
|
commands = fmt.Sprintf("tar --warning=no-file-changed --ignore-failed-read --exclude-from=<(find %s -type s -printf '%s' | sed 's|^|%s/|') -zcf %s %s %s", sourceDir, "%P\n", itemPrefix, targetDir+"/"+name, excludeRules, path)
|
2024-06-05 06:31:26 +00:00
|
|
|
global.LOG.Debug(commands)
|
|
|
|
}
|
2023-07-12 07:49:15 +00:00
|
|
|
stdout, err := cmd.ExecWithTimeOut(commands, 24*time.Hour)
|
2022-10-28 10:46:14 +00:00
|
|
|
if err != nil {
|
2023-08-12 22:48:11 +00:00
|
|
|
if len(stdout) != 0 {
|
|
|
|
global.LOG.Errorf("do handle tar failed, stdout: %s, err: %v", stdout, err)
|
|
|
|
return fmt.Errorf("do handle tar failed, stdout: %s, err: %v", stdout, err)
|
|
|
|
}
|
2022-10-28 10:46:14 +00:00
|
|
|
}
|
|
|
|
return nil
|
2022-09-29 08:15:59 +00:00
|
|
|
}
|
2022-11-04 11:02:15 +00:00
|
|
|
|
2024-06-04 08:48:31 +00:00
|
|
|
func handleUnTar(sourceFile, targetDir string, secret string) error {
|
2022-11-04 11:02:15 +00:00
|
|
|
if _, err := os.Stat(targetDir); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(targetDir, os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-06-04 08:48:31 +00:00
|
|
|
commands := ""
|
2024-06-06 10:43:38 +00:00
|
|
|
if len(secret) != 0 {
|
2024-06-27 03:09:55 +00:00
|
|
|
extraCmd := "openssl enc -d -aes-256-cbc -k '" + secret + "' -in " + sourceFile + " | "
|
2024-06-04 08:48:31 +00:00
|
|
|
commands = fmt.Sprintf("%s tar -zxvf - -C %s", extraCmd, targetDir+" > /dev/null 2>&1")
|
2024-06-05 06:31:26 +00:00
|
|
|
global.LOG.Debug(strings.ReplaceAll(commands, fmt.Sprintf(" %s ", secret), "******"))
|
|
|
|
} else {
|
2024-06-06 10:43:38 +00:00
|
|
|
commands = fmt.Sprintf("tar zxvfC %s %s", sourceFile, targetDir)
|
2024-06-05 06:31:26 +00:00
|
|
|
global.LOG.Debug(commands)
|
|
|
|
}
|
2024-06-06 10:43:38 +00:00
|
|
|
|
2023-07-12 07:49:15 +00:00
|
|
|
stdout, err := cmd.ExecWithTimeOut(commands, 24*time.Hour)
|
2022-11-04 11:02:15 +00:00
|
|
|
if err != nil {
|
2023-03-08 09:02:06 +00:00
|
|
|
global.LOG.Errorf("do handle untar failed, stdout: %s, err: %v", stdout, err)
|
|
|
|
return errors.New(stdout)
|
2022-11-04 11:02:15 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-07 02:18:10 +00:00
|
|
|
|
2023-12-29 10:39:39 +00:00
|
|
|
func (u *CronjobService) handleCutWebsiteLog(cronjob *model.Cronjob, startTime time.Time) ([]string, string, error) {
|
2023-05-23 10:54:05 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
filePaths []string
|
2023-12-29 10:39:39 +00:00
|
|
|
msgs []string
|
2023-05-23 10:54:05 +00:00
|
|
|
)
|
2024-01-11 03:01:40 +00:00
|
|
|
websites := loadWebsForJob(*cronjob)
|
2023-05-23 10:54:05 +00:00
|
|
|
nginx, err := getAppInstallByKey(constant.AppOpenresty)
|
|
|
|
if err != nil {
|
2023-12-29 10:39:39 +00:00
|
|
|
return msgs, "", nil
|
2023-05-23 10:54:05 +00:00
|
|
|
}
|
|
|
|
baseDir := path.Join(nginx.GetPath(), "www", "sites")
|
|
|
|
fileOp := files.NewFileOp()
|
2024-01-11 03:01:40 +00:00
|
|
|
for _, website := range websites {
|
|
|
|
websiteLogDir := path.Join(baseDir, website.Alias, "log")
|
|
|
|
srcAccessLogPath := path.Join(websiteLogDir, "access.log")
|
|
|
|
srcErrorLogPath := path.Join(websiteLogDir, "error.log")
|
|
|
|
dstLogDir := path.Join(global.CONF.System.Backup, "log", "website", website.Alias)
|
|
|
|
if !fileOp.Stat(dstLogDir) {
|
|
|
|
_ = os.MkdirAll(dstLogDir, 0755)
|
|
|
|
}
|
|
|
|
|
2024-06-28 06:04:08 +00:00
|
|
|
dstName := fmt.Sprintf("%s_log_%s.gz", website.PrimaryDomain, startTime.Format(constant.DateTimeSlimLayout))
|
2024-01-11 03:01:40 +00:00
|
|
|
dstFilePath := path.Join(dstLogDir, dstName)
|
|
|
|
filePaths = append(filePaths, dstFilePath)
|
|
|
|
|
|
|
|
if err = backupLogFile(dstFilePath, websiteLogDir, fileOp); err != nil {
|
|
|
|
websiteErr := buserr.WithNameAndErr("ErrCutWebsiteLog", website.PrimaryDomain, err)
|
|
|
|
err = websiteErr
|
|
|
|
msgs = append(msgs, websiteErr.Error())
|
|
|
|
global.LOG.Error(websiteErr.Error())
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
_ = fileOp.WriteFile(srcAccessLogPath, strings.NewReader(""), 0755)
|
|
|
|
_ = fileOp.WriteFile(srcErrorLogPath, strings.NewReader(""), 0755)
|
|
|
|
}
|
|
|
|
msg := i18n.GetMsgWithMap("CutWebsiteLogSuccess", map[string]interface{}{"name": website.PrimaryDomain, "path": dstFilePath})
|
|
|
|
msgs = append(msgs, msg)
|
2023-05-23 10:54:05 +00:00
|
|
|
}
|
2024-01-24 09:41:56 +00:00
|
|
|
u.removeExpiredLog(*cronjob)
|
2023-12-29 10:39:39 +00:00
|
|
|
return msgs, strings.Join(filePaths, ","), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func backupLogFile(dstFilePath, websiteLogDir string, fileOp files.FileOp) error {
|
|
|
|
if err := cmd.ExecCmd(fmt.Sprintf("tar -czf %s -C %s %s", dstFilePath, websiteLogDir, strings.Join([]string{"access.log", "error.log"}, " "))); err != nil {
|
|
|
|
dstDir := path.Dir(dstFilePath)
|
|
|
|
if err = fileOp.Copy(path.Join(websiteLogDir, "access.log"), dstDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = fileOp.Copy(path.Join(websiteLogDir, "error.log"), dstDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = cmd.ExecCmd(fmt.Sprintf("tar -czf %s -C %s %s", dstFilePath, dstDir, strings.Join([]string{"access.log", "error.log"}, " "))); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_ = fileOp.DeleteFile(path.Join(dstDir, "access.log"))
|
|
|
|
_ = fileOp.DeleteFile(path.Join(dstDir, "error.log"))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
2023-05-23 10:54:05 +00:00
|
|
|
}
|
|
|
|
|
2024-01-24 09:41:56 +00:00
|
|
|
func (u *CronjobService) handleSystemClean() (string, error) {
|
|
|
|
return NewIDeviceService().CleanForCronjob()
|
2023-08-23 14:44:14 +00:00
|
|
|
}
|
|
|
|
|
2024-01-25 03:20:42 +00:00
|
|
|
func loadClientMap(backupAccounts string) (map[string]cronjobUploadHelper, error) {
|
2024-01-24 09:41:56 +00:00
|
|
|
clients := make(map[string]cronjobUploadHelper)
|
|
|
|
accounts, err := backupRepo.List()
|
2023-04-07 02:18:10 +00:00
|
|
|
if err != nil {
|
2024-01-24 09:41:56 +00:00
|
|
|
return nil, err
|
2023-04-07 02:18:10 +00:00
|
|
|
}
|
2024-01-25 03:20:42 +00:00
|
|
|
targets := strings.Split(backupAccounts, ",")
|
2024-01-24 09:41:56 +00:00
|
|
|
for _, target := range targets {
|
|
|
|
if len(target) == 0 {
|
|
|
|
continue
|
2023-04-07 02:18:10 +00:00
|
|
|
}
|
2024-01-24 09:41:56 +00:00
|
|
|
for _, account := range accounts {
|
2024-01-25 03:20:42 +00:00
|
|
|
if target == account.Type {
|
2024-01-24 09:41:56 +00:00
|
|
|
client, err := NewIBackupService().NewClient(&account)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pathItem := account.BackupPath
|
2024-01-29 08:35:06 +00:00
|
|
|
if account.BackupPath != "/" {
|
|
|
|
pathItem = strings.TrimPrefix(account.BackupPath, "/")
|
|
|
|
}
|
2024-01-24 09:41:56 +00:00
|
|
|
clients[target] = cronjobUploadHelper{
|
|
|
|
client: client,
|
|
|
|
backupPath: pathItem,
|
|
|
|
backType: account.Type,
|
|
|
|
}
|
2023-04-11 09:00:29 +00:00
|
|
|
}
|
2023-04-07 02:18:10 +00:00
|
|
|
}
|
2023-08-24 14:26:16 +00:00
|
|
|
}
|
2024-01-24 09:41:56 +00:00
|
|
|
return clients, nil
|
2023-08-24 14:26:16 +00:00
|
|
|
}
|
2023-10-18 09:08:24 +00:00
|
|
|
|
2024-01-24 09:41:56 +00:00
|
|
|
type cronjobUploadHelper struct {
|
|
|
|
backupPath string
|
|
|
|
backType string
|
|
|
|
client cloud_storage.CloudStorageClient
|
2023-10-18 09:08:24 +00:00
|
|
|
}
|
2023-10-20 03:32:45 +00:00
|
|
|
|
2024-01-24 09:41:56 +00:00
|
|
|
func (u *CronjobService) uploadCronjobBackFile(cronjob model.Cronjob, accountMap map[string]cronjobUploadHelper, file string) (string, error) {
|
|
|
|
defer func() {
|
|
|
|
_ = os.Remove(file)
|
|
|
|
}()
|
2024-01-25 03:20:42 +00:00
|
|
|
accounts := strings.Split(cronjob.BackupAccounts, ",")
|
2024-01-24 09:41:56 +00:00
|
|
|
cloudSrc := strings.TrimPrefix(file, global.CONF.System.TmpDir+"/")
|
2024-01-25 03:20:42 +00:00
|
|
|
for _, account := range accounts {
|
|
|
|
if len(account) != 0 {
|
2024-01-26 09:08:19 +00:00
|
|
|
global.LOG.Debugf("start upload file to %s, dir: %s", account, path.Join(accountMap[account].backupPath, cloudSrc))
|
2024-01-25 03:20:42 +00:00
|
|
|
if _, err := accountMap[account].client.Upload(file, path.Join(accountMap[account].backupPath, cloudSrc)); err != nil {
|
2024-01-24 09:41:56 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2024-01-26 09:08:19 +00:00
|
|
|
global.LOG.Debugf("upload successful!")
|
2024-01-24 09:41:56 +00:00
|
|
|
}
|
2023-10-20 03:32:45 +00:00
|
|
|
}
|
2024-01-24 09:41:56 +00:00
|
|
|
return cloudSrc, nil
|
|
|
|
}
|
2023-10-20 03:32:45 +00:00
|
|
|
|
2024-01-24 09:41:56 +00:00
|
|
|
func (u *CronjobService) removeExpiredBackup(cronjob model.Cronjob, accountMap map[string]cronjobUploadHelper, record model.BackupRecord) {
|
|
|
|
var opts []repo.DBOption
|
|
|
|
opts = append(opts, commonRepo.WithByFrom("cronjob"))
|
|
|
|
opts = append(opts, backupRepo.WithByCronID(cronjob.ID))
|
|
|
|
opts = append(opts, commonRepo.WithOrderBy("created_at desc"))
|
|
|
|
if record.ID != 0 {
|
|
|
|
opts = append(opts, backupRepo.WithByType(record.Type))
|
|
|
|
opts = append(opts, commonRepo.WithByName(record.Name))
|
|
|
|
opts = append(opts, backupRepo.WithByDetailName(record.DetailName))
|
|
|
|
}
|
|
|
|
records, _ := backupRepo.ListRecord(opts...)
|
|
|
|
if len(records) <= int(cronjob.RetainCopies) {
|
|
|
|
return
|
2023-10-20 03:32:45 +00:00
|
|
|
}
|
2024-01-24 09:41:56 +00:00
|
|
|
for i := int(cronjob.RetainCopies); i < len(records); i++ {
|
2024-01-25 03:20:42 +00:00
|
|
|
accounts := strings.Split(cronjob.BackupAccounts, ",")
|
2024-01-24 09:41:56 +00:00
|
|
|
if cronjob.Type == "snapshot" {
|
2024-01-25 03:20:42 +00:00
|
|
|
for _, account := range accounts {
|
|
|
|
if len(account) != 0 {
|
|
|
|
_, _ = accountMap[account].client.Delete(path.Join(accountMap[account].backupPath, "system_snapshot", records[i].FileName))
|
2023-11-03 03:18:45 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-24 09:41:56 +00:00
|
|
|
_ = snapshotRepo.Delete(commonRepo.WithByName(strings.TrimSuffix(records[i].FileName, ".tar.gz")))
|
|
|
|
} else {
|
2024-01-25 03:20:42 +00:00
|
|
|
for _, account := range accounts {
|
|
|
|
if len(account) != 0 {
|
|
|
|
_, _ = accountMap[account].client.Delete(path.Join(accountMap[account].backupPath, records[i].FileDir, records[i].FileName))
|
2023-10-20 03:32:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-24 09:41:56 +00:00
|
|
|
_ = backupRepo.DeleteRecord(context.Background(), commonRepo.WithByID(records[i].ID))
|
2023-10-20 03:32:45 +00:00
|
|
|
}
|
2024-01-24 09:41:56 +00:00
|
|
|
}
|
2023-10-20 03:32:45 +00:00
|
|
|
|
2024-01-24 09:41:56 +00:00
|
|
|
func (u *CronjobService) removeExpiredLog(cronjob model.Cronjob) {
|
|
|
|
records, _ := cronjobRepo.ListRecord(cronjobRepo.WithByJobID(int(cronjob.ID)), commonRepo.WithOrderBy("created_at desc"))
|
|
|
|
if len(records) <= int(cronjob.RetainCopies) {
|
|
|
|
return
|
2023-10-20 03:32:45 +00:00
|
|
|
}
|
2024-01-24 09:41:56 +00:00
|
|
|
for i := int(cronjob.RetainCopies); i < len(records); i++ {
|
|
|
|
if len(records[i].File) != 0 {
|
|
|
|
files := strings.Split(records[i].File, ",")
|
|
|
|
for _, file := range files {
|
|
|
|
_ = os.Remove(file)
|
2023-10-20 03:32:45 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-24 09:41:56 +00:00
|
|
|
_ = cronjobRepo.DeleteRecord(commonRepo.WithByID(uint(records[i].ID)))
|
|
|
|
_ = os.Remove(records[i].Records)
|
2023-10-20 03:32:45 +00:00
|
|
|
}
|
2024-01-24 09:41:56 +00:00
|
|
|
}
|
2023-10-20 03:32:45 +00:00
|
|
|
|
2024-01-24 09:41:56 +00:00
|
|
|
func (u *CronjobService) generateLogsPath(cronjob model.Cronjob, startTime time.Time) string {
|
|
|
|
dir := fmt.Sprintf("%s/task/%s/%s", constant.DataDir, cronjob.Type, cronjob.Name)
|
|
|
|
if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
|
|
|
|
_ = os.MkdirAll(dir, os.ModePerm)
|
2023-10-20 03:32:45 +00:00
|
|
|
}
|
|
|
|
|
2024-06-28 06:04:08 +00:00
|
|
|
path := fmt.Sprintf("%s/%s.log", dir, startTime.Format(constant.DateTimeSlimLayout))
|
2024-01-24 09:41:56 +00:00
|
|
|
return path
|
2023-10-20 03:32:45 +00:00
|
|
|
}
|
2023-11-03 10:23:41 +00:00
|
|
|
|
|
|
|
func hasBackup(cronjobType string) bool {
|
|
|
|
return cronjobType == "app" || cronjobType == "database" || cronjobType == "website" || cronjobType == "directory" || cronjobType == "snapshot" || cronjobType == "log"
|
|
|
|
}
|