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"
|
|
|
|
"time"
|
|
|
|
|
2024-01-07 22:52:41 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/i18n"
|
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2024-01-24 17:41:56 +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
|
|
|
|
}
|
2024-01-24 17:41:56 +08:00
|
|
|
record.Records = u.generateLogsPath(*cronjob, record.StartTime)
|
|
|
|
_ = cronjobRepo.UpdateRecords(record.ID, map[string]interface{}{"records": record.Records})
|
|
|
|
script := cronjob.Script
|
2023-06-28 14:30:11 +08:00
|
|
|
if len(cronjob.ContainerName) != 0 {
|
2024-01-24 17:41:56 +08:00
|
|
|
script = fmt.Sprintf("docker exec %s %s", cronjob.ContainerName, cronjob.Script)
|
2023-06-28 14:30:11 +08:00
|
|
|
}
|
2024-01-24 17:41:56 +08:00
|
|
|
err = u.handleShell(cronjob.Type, cronjob.Name, script, record.Records)
|
|
|
|
u.removeExpiredLog(*cronjob)
|
2023-05-24 16:39:36 +08:00
|
|
|
case "curl":
|
|
|
|
if len(cronjob.URL) == 0 {
|
|
|
|
return
|
2023-02-24 18:50:35 +08:00
|
|
|
}
|
2024-01-24 17:41:56 +08: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 17:25:35 +08:00
|
|
|
case "ntp":
|
|
|
|
err = u.handleNtpSync()
|
2024-01-24 17:41:56 +08:00
|
|
|
u.removeExpiredLog(*cronjob)
|
2023-05-23 18:54:05 +08:00
|
|
|
case "cutWebsiteLog":
|
2023-12-29 18:39:39 +08:00
|
|
|
var messageItem []string
|
|
|
|
messageItem, record.File, err = u.handleCutWebsiteLog(cronjob, record.StartTime)
|
|
|
|
message = []byte(strings.Join(messageItem, "\n"))
|
2023-10-18 17:08:24 +08:00
|
|
|
case "clean":
|
|
|
|
messageItem := ""
|
|
|
|
messageItem, err = u.handleSystemClean()
|
|
|
|
message = []byte(messageItem)
|
2024-01-24 17:41:56 +08: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 11:32:45 +08:00
|
|
|
case "log":
|
2024-01-24 17:41:56 +08: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 16:15:59 +08:00
|
|
|
}
|
2023-08-24 22:26:16 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-01-24 17:41:56 +08:00
|
|
|
func (u *CronjobService) handleShell(cronType, cornName, script, logPath string) error {
|
2023-05-24 16:39:36 +08: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 17:41:56 +08:00
|
|
|
return err
|
2023-05-24 16:39:36 +08:00
|
|
|
}
|
|
|
|
}
|
2024-01-24 17:41:56 +08:00
|
|
|
if err := cmd.ExecCronjobWithTimeOut(script, handleDir, logPath, 24*time.Hour); err != nil {
|
|
|
|
return err
|
2023-05-24 16:39:36 +08:00
|
|
|
}
|
2024-01-24 17:41:56 +08:00
|
|
|
return nil
|
2023-05-24 16:39:36 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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 := ""
|
2023-10-27 15:05:25 +08:00
|
|
|
excludes = append(excludes, "*.sock")
|
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, "/"):], "/", "")
|
2023-06-09 11:42:11 +08:00
|
|
|
aheadDir := sourceDir[:strings.LastIndex(sourceDir, "/")]
|
2023-06-25 21:14:14 +08:00
|
|
|
if len(aheadDir) == 0 {
|
|
|
|
aheadDir = "/"
|
|
|
|
}
|
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-09-12 17:48:11 +08:00
|
|
|
commands := fmt.Sprintf("tar --warning=no-file-changed --ignore-failed-read -zcf %s %s %s", targetDir+"/"+name, excludeRules, path)
|
2023-03-08 17:02:06 +08:00
|
|
|
global.LOG.Debug(commands)
|
2023-07-12 15:49:15 +08:00
|
|
|
stdout, err := cmd.ExecWithTimeOut(commands, 24*time.Hour)
|
2022-10-28 18:46:14 +08:00
|
|
|
if err != nil {
|
2023-08-13 06:48:11 +08: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 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-07-12 15:49:15 +08:00
|
|
|
stdout, err := cmd.ExecWithTimeOut(commands, 24*time.Hour)
|
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-12-29 18:39:39 +08:00
|
|
|
func (u *CronjobService) handleCutWebsiteLog(cronjob *model.Cronjob, startTime time.Time) ([]string, string, error) {
|
2023-05-23 18:54:05 +08:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
filePaths []string
|
2023-12-29 18:39:39 +08:00
|
|
|
msgs []string
|
2023-05-23 18:54:05 +08:00
|
|
|
)
|
2024-01-11 11:01:40 +08:00
|
|
|
websites := loadWebsForJob(*cronjob)
|
2023-05-23 18:54:05 +08:00
|
|
|
nginx, err := getAppInstallByKey(constant.AppOpenresty)
|
|
|
|
if err != nil {
|
2023-12-29 18:39:39 +08:00
|
|
|
return msgs, "", nil
|
2023-05-23 18:54:05 +08:00
|
|
|
}
|
|
|
|
baseDir := path.Join(nginx.GetPath(), "www", "sites")
|
|
|
|
fileOp := files.NewFileOp()
|
2024-01-11 11:01:40 +08: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)
|
|
|
|
}
|
|
|
|
|
|
|
|
dstName := fmt.Sprintf("%s_log_%s.gz", website.PrimaryDomain, startTime.Format("20060102150405"))
|
|
|
|
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})
|
|
|
|
global.LOG.Infof(msg)
|
|
|
|
msgs = append(msgs, msg)
|
2023-05-23 18:54:05 +08:00
|
|
|
}
|
2024-01-24 17:41:56 +08:00
|
|
|
u.removeExpiredLog(*cronjob)
|
2023-12-29 18:39:39 +08: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 18:54:05 +08:00
|
|
|
}
|
|
|
|
|
2024-01-24 17:41:56 +08:00
|
|
|
func (u *CronjobService) handleSystemClean() (string, error) {
|
|
|
|
return NewIDeviceService().CleanForCronjob()
|
2023-08-23 22:44:14 +08:00
|
|
|
}
|
|
|
|
|
2024-01-25 11:20:42 +08:00
|
|
|
func loadClientMap(backupAccounts string) (map[string]cronjobUploadHelper, error) {
|
2024-01-24 17:41:56 +08:00
|
|
|
clients := make(map[string]cronjobUploadHelper)
|
|
|
|
accounts, err := backupRepo.List()
|
2023-04-07 10:18:10 +08:00
|
|
|
if err != nil {
|
2024-01-24 17:41:56 +08:00
|
|
|
return nil, err
|
2023-04-07 10:18:10 +08:00
|
|
|
}
|
2024-01-25 11:20:42 +08:00
|
|
|
targets := strings.Split(backupAccounts, ",")
|
2024-01-24 17:41:56 +08:00
|
|
|
for _, target := range targets {
|
|
|
|
if len(target) == 0 {
|
|
|
|
continue
|
2023-04-07 10:18:10 +08:00
|
|
|
}
|
2024-01-24 17:41:56 +08:00
|
|
|
for _, account := range accounts {
|
2024-01-25 11:20:42 +08:00
|
|
|
if target == account.Type {
|
2024-01-24 17:41:56 +08:00
|
|
|
client, err := NewIBackupService().NewClient(&account)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pathItem := account.BackupPath
|
2024-01-29 16:35:06 +08:00
|
|
|
if account.BackupPath != "/" {
|
|
|
|
pathItem = strings.TrimPrefix(account.BackupPath, "/")
|
|
|
|
}
|
2024-01-24 17:41:56 +08:00
|
|
|
clients[target] = cronjobUploadHelper{
|
|
|
|
client: client,
|
|
|
|
backupPath: pathItem,
|
|
|
|
backType: account.Type,
|
|
|
|
}
|
2023-04-11 17:00:29 +08:00
|
|
|
}
|
2023-04-07 10:18:10 +08:00
|
|
|
}
|
2023-08-24 22:26:16 +08:00
|
|
|
}
|
2024-01-24 17:41:56 +08:00
|
|
|
return clients, nil
|
2023-08-24 22:26:16 +08:00
|
|
|
}
|
2023-10-18 17:08:24 +08:00
|
|
|
|
2024-01-24 17:41:56 +08:00
|
|
|
type cronjobUploadHelper struct {
|
|
|
|
backupPath string
|
|
|
|
backType string
|
|
|
|
client cloud_storage.CloudStorageClient
|
2023-10-18 17:08:24 +08:00
|
|
|
}
|
2023-10-20 11:32:45 +08:00
|
|
|
|
2024-01-24 17:41:56 +08:00
|
|
|
func (u *CronjobService) uploadCronjobBackFile(cronjob model.Cronjob, accountMap map[string]cronjobUploadHelper, file string) (string, error) {
|
|
|
|
defer func() {
|
|
|
|
_ = os.Remove(file)
|
|
|
|
}()
|
2024-01-25 11:20:42 +08:00
|
|
|
accounts := strings.Split(cronjob.BackupAccounts, ",")
|
2024-01-24 17:41:56 +08:00
|
|
|
cloudSrc := strings.TrimPrefix(file, global.CONF.System.TmpDir+"/")
|
2024-01-25 11:20:42 +08:00
|
|
|
for _, account := range accounts {
|
|
|
|
if len(account) != 0 {
|
2024-01-26 17:08:19 +08:00
|
|
|
global.LOG.Debugf("start upload file to %s, dir: %s", account, path.Join(accountMap[account].backupPath, cloudSrc))
|
2024-01-25 11:20:42 +08:00
|
|
|
if _, err := accountMap[account].client.Upload(file, path.Join(accountMap[account].backupPath, cloudSrc)); err != nil {
|
2024-01-24 17:41:56 +08:00
|
|
|
return "", err
|
|
|
|
}
|
2024-01-26 17:08:19 +08:00
|
|
|
global.LOG.Debugf("upload successful!")
|
2024-01-24 17:41:56 +08:00
|
|
|
}
|
2023-10-20 11:32:45 +08:00
|
|
|
}
|
2024-01-24 17:41:56 +08:00
|
|
|
return cloudSrc, nil
|
|
|
|
}
|
2023-10-20 11:32:45 +08:00
|
|
|
|
2024-01-24 17:41:56 +08:00
|
|
|
func (u *CronjobService) removeExpiredBackup(cronjob model.Cronjob, accountMap map[string]cronjobUploadHelper, record model.BackupRecord) {
|
|
|
|
global.LOG.Infof("start to handle remove expired, retain copies: %d", cronjob.RetainCopies)
|
|
|
|
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 11:32:45 +08:00
|
|
|
}
|
2024-01-24 17:41:56 +08:00
|
|
|
for i := int(cronjob.RetainCopies); i < len(records); i++ {
|
2024-01-25 11:20:42 +08:00
|
|
|
accounts := strings.Split(cronjob.BackupAccounts, ",")
|
2024-01-24 17:41:56 +08:00
|
|
|
if cronjob.Type == "snapshot" {
|
2024-01-25 11:20:42 +08: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 11:18:45 +08:00
|
|
|
}
|
|
|
|
}
|
2024-01-24 17:41:56 +08:00
|
|
|
_ = snapshotRepo.Delete(commonRepo.WithByName(strings.TrimSuffix(records[i].FileName, ".tar.gz")))
|
|
|
|
} else {
|
2024-01-25 11:20:42 +08: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 11:32:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-24 17:41:56 +08:00
|
|
|
_ = backupRepo.DeleteRecord(context.Background(), commonRepo.WithByID(records[i].ID))
|
2023-10-20 11:32:45 +08:00
|
|
|
}
|
2024-01-24 17:41:56 +08:00
|
|
|
}
|
2023-10-20 11:32:45 +08:00
|
|
|
|
2024-01-24 17:41:56 +08:00
|
|
|
func (u *CronjobService) removeExpiredLog(cronjob model.Cronjob) {
|
|
|
|
global.LOG.Infof("start to handle remove expired, retain copies: %d", cronjob.RetainCopies)
|
|
|
|
records, _ := cronjobRepo.ListRecord(cronjobRepo.WithByJobID(int(cronjob.ID)), commonRepo.WithOrderBy("created_at desc"))
|
|
|
|
if len(records) <= int(cronjob.RetainCopies) {
|
|
|
|
return
|
2023-10-20 11:32:45 +08:00
|
|
|
}
|
2024-01-24 17:41:56 +08: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 11:32:45 +08:00
|
|
|
}
|
|
|
|
}
|
2024-01-24 17:41:56 +08:00
|
|
|
_ = cronjobRepo.DeleteRecord(commonRepo.WithByID(uint(records[i].ID)))
|
|
|
|
_ = os.Remove(records[i].Records)
|
2023-10-20 11:32:45 +08:00
|
|
|
}
|
2024-01-24 17:41:56 +08:00
|
|
|
}
|
2023-10-20 11:32:45 +08:00
|
|
|
|
2024-01-24 17:41:56 +08: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 11:32:45 +08:00
|
|
|
}
|
|
|
|
|
2024-01-24 17:41:56 +08:00
|
|
|
path := fmt.Sprintf("%s/%s.log", dir, startTime.Format("20060102150405"))
|
|
|
|
return path
|
2023-10-20 11:32:45 +08:00
|
|
|
}
|
2023-11-03 18:23:41 +08:00
|
|
|
|
|
|
|
func hasBackup(cronjobType string) bool {
|
|
|
|
return cronjobType == "app" || cronjobType == "database" || cronjobType == "website" || cronjobType == "directory" || cronjobType == "snapshot" || cronjobType == "log"
|
|
|
|
}
|