2022-09-29 16:15:59 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2022-12-21 15:54:34 +08:00
|
|
|
"context"
|
2022-09-29 16:15:59 +08:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"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"
|
2022-09-29 16:15:59 +08:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/robfig/cron/v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (u *CronjobService) HandleJob(cronjob *model.Cronjob) {
|
|
|
|
var (
|
|
|
|
message []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
record := cronjobRepo.StartRecords(cronjob.ID, "")
|
2022-10-28 18:46:14 +08:00
|
|
|
record.FromLocal = cronjob.KeepLocal
|
2023-02-24 18:50:35 +08:00
|
|
|
go func() {
|
|
|
|
switch cronjob.Type {
|
|
|
|
case "shell":
|
|
|
|
if len(cronjob.Script) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
stdout, errExec := cmd.Exec(cronjob.Script)
|
|
|
|
if errExec != nil {
|
|
|
|
err = errExec
|
|
|
|
}
|
|
|
|
message = []byte(stdout)
|
|
|
|
case "website":
|
|
|
|
record.File, err = u.HandleBackup(cronjob, record.StartTime)
|
|
|
|
case "database":
|
|
|
|
record.File, err = u.HandleBackup(cronjob, record.StartTime)
|
|
|
|
case "directory":
|
|
|
|
if len(cronjob.SourceDir) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
record.File, err = u.HandleBackup(cronjob, record.StartTime)
|
|
|
|
case "curl":
|
|
|
|
if len(cronjob.URL) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
stdout, errCurl := cmd.Exec("curl " + cronjob.URL)
|
|
|
|
if err != nil {
|
|
|
|
err = errCurl
|
|
|
|
}
|
|
|
|
message = []byte(stdout)
|
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
|
|
|
}
|
|
|
|
|
2022-10-28 18:46:14 +08:00
|
|
|
func (u *CronjobService) HandleBackup(cronjob *model.Cronjob, startTime time.Time) (string, error) {
|
|
|
|
var (
|
|
|
|
backupDir string
|
|
|
|
fileName string
|
2023-02-21 19:06:24 +08:00
|
|
|
record model.BackupRecord
|
2022-10-28 18:46:14 +08:00
|
|
|
)
|
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
|
|
|
|
}
|
2022-11-30 15:47:11 +08:00
|
|
|
fileName = fmt.Sprintf("db_%s_%s.sql.gz", cronjob.DBName, startTime.Format("20060102150405"))
|
2023-02-21 19:06:24 +08:00
|
|
|
backupDir = fmt.Sprintf("%s/database/mysql/%s/%s", localDir, app.Name, cronjob.DBName)
|
|
|
|
if err = handleMysqlBackup(app, backupDir, cronjob.DBName, fileName); err != nil {
|
2022-10-28 18:46:14 +08:00
|
|
|
return "", err
|
|
|
|
}
|
2023-02-21 19:06:24 +08:00
|
|
|
record.Type = "mysql"
|
2023-03-01 19:11:51 +08:00
|
|
|
record.Name = app.Name
|
|
|
|
record.DetailName = cronjob.DBName
|
2022-11-30 15:47:11 +08:00
|
|
|
case "website":
|
2023-02-21 19:06:24 +08:00
|
|
|
fileName = fmt.Sprintf("website_%s_%s.tar.gz", cronjob.Website, startTime.Format("20060102150405"))
|
|
|
|
backupDir = fmt.Sprintf("%s/website/%s", localDir, cronjob.Website)
|
|
|
|
website, err := websiteRepo.GetFirst(websiteRepo.WithDomain(cronjob.Website))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := handleWebsiteBackup(&website, backupDir, fileName); err != nil {
|
2022-11-30 15:47:11 +08:00
|
|
|
return "", err
|
|
|
|
}
|
2023-02-21 19:06:24 +08:00
|
|
|
record.Type = "website"
|
|
|
|
record.Name = website.PrimaryDomain
|
2022-11-30 15:47:11 +08:00
|
|
|
default:
|
2023-02-24 12:26:23 +08:00
|
|
|
fileName = fmt.Sprintf("directory%s_%s.tar.gz", strings.ReplaceAll(cronjob.SourceDir, "/", "_"), startTime.Format("20060102150405"))
|
2023-02-21 19:06:24 +08:00
|
|
|
backupDir = fmt.Sprintf("%s/%s/%s", localDir, 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-03-17 16:46:46 +08:00
|
|
|
|
|
|
|
itemFileDir := strings.ReplaceAll(backupDir, localDir+"/", "")
|
2023-02-21 19:06:24 +08:00
|
|
|
if len(record.Name) != 0 {
|
|
|
|
record.FileName = fileName
|
|
|
|
record.FileDir = backupDir
|
|
|
|
record.Source = "LOCAL"
|
|
|
|
record.BackupType = backup.Type
|
|
|
|
if !cronjob.KeepLocal && backup.Type != "LOCAL" {
|
|
|
|
record.Source = backup.Type
|
2023-03-17 16:46:46 +08:00
|
|
|
record.FileDir = itemFileDir
|
2023-02-21 19:06:24 +08:00
|
|
|
}
|
|
|
|
if err := backupRepo.CreateRecord(&record); err != nil {
|
|
|
|
global.LOG.Errorf("save backup record failed, err: %v", err)
|
2022-10-28 18:46:14 +08:00
|
|
|
return "", err
|
2022-09-29 16:15:59 +08:00
|
|
|
}
|
|
|
|
}
|
2022-10-28 18:46:14 +08:00
|
|
|
|
2023-02-21 19:06:24 +08:00
|
|
|
fullPath := fmt.Sprintf("%s/%s", record.FileDir, fileName)
|
2022-10-28 18:46:14 +08:00
|
|
|
if backup.Type == "LOCAL" {
|
2023-03-12 23:23:49 +08:00
|
|
|
u.HandleRmExpired(backup.Type, backupDir, cronjob, nil)
|
2023-02-21 19:06:24 +08:00
|
|
|
return fullPath, nil
|
2022-10-28 18:46:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if !cronjob.KeepLocal {
|
2023-02-21 19:06:24 +08:00
|
|
|
defer func() {
|
|
|
|
_ = os.RemoveAll(fmt.Sprintf("%s/%s", backupDir, fileName))
|
|
|
|
}()
|
2022-10-28 18:46:14 +08:00
|
|
|
}
|
2022-09-29 16:15:59 +08:00
|
|
|
client, err := NewIBackupService().NewClient(&backup)
|
|
|
|
if err != nil {
|
2023-02-21 19:06:24 +08:00
|
|
|
return fullPath, err
|
2022-09-29 16:15:59 +08:00
|
|
|
}
|
2023-03-17 16:46:46 +08:00
|
|
|
if _, err = client.Upload(backupDir+"/"+fileName, itemFileDir+"/"+fileName); err != nil {
|
2023-02-21 19:06:24 +08:00
|
|
|
return fullPath, err
|
2022-09-29 16:15:59 +08:00
|
|
|
}
|
2023-03-17 16:46:46 +08:00
|
|
|
u.HandleRmExpired(backup.Type, itemFileDir, cronjob, client)
|
|
|
|
if cronjob.KeepLocal {
|
|
|
|
u.HandleRmExpired("LOCAL", backupDir, cronjob, client)
|
|
|
|
}
|
2023-02-21 19:06:24 +08:00
|
|
|
return fullPath, nil
|
2022-09-29 16:15:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *CronjobService) HandleDelete(id uint) error {
|
|
|
|
cronjob, _ := cronjobRepo.Get(commonRepo.WithByID(id))
|
|
|
|
if cronjob.ID == 0 {
|
|
|
|
return errors.New("find cronjob in db failed")
|
|
|
|
}
|
|
|
|
commonDir := fmt.Sprintf("%s/%s/", cronjob.Type, cronjob.Name)
|
|
|
|
global.Cron.Remove(cron.EntryID(cronjob.EntryID))
|
2022-12-28 18:35:53 +08:00
|
|
|
global.LOG.Infof("stop cronjob entryID: %d", cronjob.EntryID)
|
2022-09-29 16:15:59 +08:00
|
|
|
_ = cronjobRepo.DeleteRecord(cronjobRepo.WithByJobID(int(id)))
|
|
|
|
|
2023-01-29 16:38:34 +08:00
|
|
|
dir := fmt.Sprintf("%s/task/%s/%s", constant.DataDir, cronjob.Type, cronjob.Name)
|
2022-12-14 19:34:42 +08:00
|
|
|
if _, err := os.Stat(dir); err == nil {
|
|
|
|
if err := os.RemoveAll(dir); err != nil {
|
2023-01-29 16:38:34 +08:00
|
|
|
global.LOG.Errorf("rm file %s/task/%s failed, err: %v", constant.DataDir, commonDir, err)
|
2022-12-14 19:34:42 +08:00
|
|
|
}
|
2022-09-29 16:15:59 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-21 19:06:24 +08:00
|
|
|
func (u *CronjobService) HandleRmExpired(backType, backupDir 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)
|
2022-09-29 16:15:59 +08:00
|
|
|
if backType != "LOCAL" {
|
2022-10-28 18:46:14 +08:00
|
|
|
currentObjs, err := backClient.ListObjects(backupDir + "/")
|
2022-09-29 16:15:59 +08:00
|
|
|
if err != nil {
|
2022-10-28 18:46:14 +08:00
|
|
|
global.LOG.Errorf("list bucket object %s failed, err: %v", backupDir, err)
|
2022-09-29 16:15:59 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := 0; i < len(currentObjs)-int(cronjob.RetainCopies); i++ {
|
|
|
|
_, _ = backClient.Delete(currentObjs[i].(string))
|
|
|
|
}
|
2023-03-17 16:46:46 +08:00
|
|
|
return
|
2022-09-29 16:15:59 +08:00
|
|
|
}
|
2023-02-21 19:06:24 +08:00
|
|
|
files, err := ioutil.ReadDir(backupDir)
|
2022-09-29 16:15:59 +08:00
|
|
|
if err != nil {
|
2023-02-21 19:06:24 +08:00
|
|
|
global.LOG.Errorf("read dir %s failed, err: %v", backupDir, err)
|
2022-09-29 16:15:59 +08:00
|
|
|
return
|
|
|
|
}
|
2022-11-08 19:03:38 +08:00
|
|
|
if len(files) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2023-03-10 00:27:41 +08:00
|
|
|
|
|
|
|
prefix := ""
|
|
|
|
switch cronjob.Type {
|
|
|
|
case "database":
|
|
|
|
prefix = "db_"
|
|
|
|
case "website":
|
|
|
|
prefix = "website_"
|
|
|
|
case "directory":
|
|
|
|
prefix = "directory_"
|
|
|
|
}
|
|
|
|
|
|
|
|
dbCopies := uint64(0)
|
|
|
|
for i := len(files) - 1; i >= 0; i-- {
|
|
|
|
if strings.HasPrefix(files[i].Name(), prefix) {
|
|
|
|
dbCopies++
|
|
|
|
if dbCopies > cronjob.RetainCopies {
|
|
|
|
_ = os.Remove(backupDir + "/" + files[i].Name())
|
|
|
|
_ = backupRepo.DeleteRecord(context.Background(), backupRepo.WithByFileName(files[i].Name()))
|
2022-11-08 19:03:38 +08:00
|
|
|
}
|
|
|
|
}
|
2022-09-29 16:15:59 +08:00
|
|
|
}
|
|
|
|
records, _ := cronjobRepo.ListRecord(cronjobRepo.WithByJobID(int(cronjob.ID)))
|
|
|
|
if len(records) > int(cronjob.RetainCopies) {
|
|
|
|
for i := int(cronjob.RetainCopies); i < len(records); i++ {
|
|
|
|
_ = cronjobRepo.DeleteRecord(cronjobRepo.WithByJobID(int(records[i].ID)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-09-29 16:15:59 +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-02-24 12:26:23 +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)
|
|
|
|
stdout, err := cmd.Exec(commands)
|
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)
|
|
|
|
stdout, err := cmd.Exec(commands)
|
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
|
|
|
|
}
|