2023-08-23 17:16:18 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2023-08-24 22:26:16 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2023-08-23 17:16:18 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
2023-09-08 23:12:13 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
2023-08-23 17:16:18 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
|
|
|
)
|
|
|
|
|
|
|
|
type snapHelper struct {
|
|
|
|
SnapID uint
|
2023-08-24 22:26:16 +08:00
|
|
|
Status *model.SnapshotStatus
|
2023-08-23 17:16:18 +08:00
|
|
|
Ctx context.Context
|
|
|
|
FileOp files.FileOp
|
|
|
|
Wg *sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
2023-08-24 22:26:16 +08:00
|
|
|
func snapJson(snap snapHelper, snapJson SnapshotJson, targetDir string) {
|
2023-08-23 17:16:18 +08:00
|
|
|
defer snap.Wg.Done()
|
2023-08-24 22:26:16 +08:00
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"panel_info": constant.Running})
|
2023-08-23 17:16:18 +08:00
|
|
|
status := constant.StatusDone
|
|
|
|
remarkInfo, _ := json.MarshalIndent(snapJson, "", "\t")
|
|
|
|
if err := os.WriteFile(fmt.Sprintf("%s/snapshot.json", targetDir), remarkInfo, 0640); err != nil {
|
|
|
|
status = err.Error()
|
|
|
|
}
|
2023-08-24 22:26:16 +08:00
|
|
|
snap.Status.PanelInfo = status
|
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"panel_info": status})
|
2023-08-23 17:16:18 +08:00
|
|
|
}
|
|
|
|
|
2023-08-24 22:26:16 +08:00
|
|
|
func snapPanel(snap snapHelper, targetDir string) {
|
2023-08-23 17:16:18 +08:00
|
|
|
defer snap.Wg.Done()
|
2023-08-24 22:26:16 +08:00
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"panel": constant.Running})
|
2023-08-23 17:16:18 +08:00
|
|
|
status := constant.StatusDone
|
2023-08-24 22:26:16 +08:00
|
|
|
if err := cpBinary([]string{"/usr/local/bin/1panel", "/usr/local/bin/1pctl", "/etc/systemd/system/1panel.service"}, targetDir); err != nil {
|
2023-08-23 17:16:18 +08:00
|
|
|
status = err.Error()
|
|
|
|
}
|
2023-08-24 22:26:16 +08:00
|
|
|
snap.Status.Panel = status
|
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"panel": status})
|
2023-08-23 17:16:18 +08:00
|
|
|
}
|
|
|
|
|
2023-08-24 22:26:16 +08:00
|
|
|
func snapDaemonJson(snap snapHelper, targetDir string) {
|
2023-08-23 17:16:18 +08:00
|
|
|
defer snap.Wg.Done()
|
|
|
|
status := constant.StatusDone
|
|
|
|
if !snap.FileOp.Stat("/etc/docker/daemon.json") {
|
2023-08-24 22:26:16 +08:00
|
|
|
snap.Status.DaemonJson = status
|
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"daemon_json": status})
|
2023-08-23 17:16:18 +08:00
|
|
|
return
|
|
|
|
}
|
2023-08-24 22:26:16 +08:00
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"daemon_json": constant.Running})
|
|
|
|
if err := cpBinary([]string{"/etc/docker/daemon.json"}, path.Join(targetDir, "daemon.json")); err != nil {
|
2023-08-23 17:16:18 +08:00
|
|
|
status = err.Error()
|
|
|
|
}
|
2023-08-24 22:26:16 +08:00
|
|
|
snap.Status.DaemonJson = status
|
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"daemon_json": status})
|
2023-08-23 17:16:18 +08:00
|
|
|
}
|
|
|
|
|
2023-08-24 22:26:16 +08:00
|
|
|
func snapAppData(snap snapHelper, targetDir string) {
|
2023-08-23 17:16:18 +08:00
|
|
|
defer snap.Wg.Done()
|
2023-08-24 22:26:16 +08:00
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"app_data": constant.Running})
|
2023-08-23 17:16:18 +08:00
|
|
|
appInstalls, err := appInstallRepo.ListBy()
|
|
|
|
if err != nil {
|
2023-08-24 22:26:16 +08:00
|
|
|
snap.Status.AppData = err.Error()
|
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"app_data": err.Error()})
|
2023-08-23 17:16:18 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
runtimes, err := runtimeRepo.List()
|
|
|
|
if err != nil {
|
2023-08-24 22:26:16 +08:00
|
|
|
snap.Status.AppData = err.Error()
|
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"app_data": err.Error()})
|
2023-08-23 17:16:18 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
imageRegex := regexp.MustCompile(`image:\s*(.*)`)
|
|
|
|
var imageSaveList []string
|
|
|
|
existStr, _ := cmd.Exec("docker images | awk '{print $1\":\"$2}' | grep -v REPOSITORY:TAG")
|
|
|
|
existImages := strings.Split(existStr, "\n")
|
|
|
|
duplicateMap := make(map[string]bool)
|
|
|
|
for _, app := range appInstalls {
|
|
|
|
matches := imageRegex.FindAllStringSubmatch(app.DockerCompose, -1)
|
|
|
|
for _, match := range matches {
|
|
|
|
for _, existImage := range existImages {
|
|
|
|
if match[1] == existImage && !duplicateMap[match[1]] {
|
|
|
|
imageSaveList = append(imageSaveList, match[1])
|
|
|
|
duplicateMap[match[1]] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, rumtime := range runtimes {
|
|
|
|
for _, existImage := range existImages {
|
|
|
|
if rumtime.Image == existImage && !duplicateMap[rumtime.Image] {
|
|
|
|
imageSaveList = append(imageSaveList, rumtime.Image)
|
|
|
|
duplicateMap[rumtime.Image] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
global.LOG.Debugf("docker save %s | gzip -c > %s", strings.Join(imageSaveList, " "), path.Join(targetDir, "docker_image.tar"))
|
|
|
|
std, err := cmd.Execf("docker save %s | gzip -c > %s", strings.Join(imageSaveList, " "), path.Join(targetDir, "docker_image.tar"))
|
|
|
|
if err != nil {
|
2023-08-24 22:26:16 +08:00
|
|
|
snap.Status.AppData = err.Error()
|
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"app_data": std})
|
2023-08-23 17:16:18 +08:00
|
|
|
return
|
|
|
|
}
|
2023-08-24 22:26:16 +08:00
|
|
|
snap.Status.AppData = constant.StatusDone
|
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"app_data": constant.StatusDone})
|
2023-08-23 17:16:18 +08:00
|
|
|
}
|
|
|
|
|
2023-08-24 22:26:16 +08:00
|
|
|
func snapBackup(snap snapHelper, localDir, targetDir string) {
|
2023-08-23 17:16:18 +08:00
|
|
|
defer snap.Wg.Done()
|
2023-08-24 22:26:16 +08:00
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"backup_data": constant.Running})
|
2023-08-23 17:16:18 +08:00
|
|
|
status := constant.StatusDone
|
|
|
|
if err := handleSnapTar(localDir, targetDir, "1panel_backup.tar.gz", "./system;"); err != nil {
|
|
|
|
status = err.Error()
|
|
|
|
}
|
2023-08-24 22:26:16 +08:00
|
|
|
snap.Status.BackupData = status
|
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"backup_data": status})
|
2023-08-23 17:16:18 +08:00
|
|
|
}
|
|
|
|
|
2023-08-24 22:26:16 +08:00
|
|
|
func snapPanelData(snap snapHelper, localDir, targetDir string) {
|
2023-08-23 17:16:18 +08:00
|
|
|
defer snap.Wg.Done()
|
2023-08-24 22:26:16 +08:00
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"panel_data": constant.Running})
|
2023-08-23 17:16:18 +08:00
|
|
|
status := constant.StatusDone
|
|
|
|
dataDir := path.Join(global.CONF.System.BaseDir, "1panel")
|
2023-08-24 22:26:16 +08:00
|
|
|
exclusionRules := "./tmp;./log;./cache;"
|
2023-08-23 17:16:18 +08:00
|
|
|
if strings.Contains(localDir, dataDir) {
|
|
|
|
exclusionRules += ("." + strings.ReplaceAll(localDir, dataDir, "") + ";")
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = snapshotRepo.Update(snap.SnapID, map[string]interface{}{"status": "OnSaveData"})
|
|
|
|
if err := handleSnapTar(dataDir, targetDir, "1panel_data.tar.gz", exclusionRules); err != nil {
|
|
|
|
status = err.Error()
|
|
|
|
}
|
|
|
|
_ = snapshotRepo.Update(snap.SnapID, map[string]interface{}{"status": constant.StatusWaiting})
|
2023-08-24 22:26:16 +08:00
|
|
|
|
|
|
|
snap.Status.PanelData = status
|
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"panel_data": status})
|
2023-08-23 17:16:18 +08:00
|
|
|
}
|
|
|
|
|
2023-08-24 22:26:16 +08:00
|
|
|
func snapCompress(snap snapHelper, rootDir string) {
|
2023-08-23 17:16:18 +08:00
|
|
|
defer func() {
|
|
|
|
global.LOG.Debugf("remove snapshot file %s", rootDir)
|
|
|
|
_ = os.RemoveAll(rootDir)
|
|
|
|
}()
|
2023-08-24 22:26:16 +08:00
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"compress": constant.StatusRunning})
|
2023-08-23 17:16:18 +08:00
|
|
|
tmpDir := path.Join(global.CONF.System.TmpDir, "system")
|
|
|
|
fileName := fmt.Sprintf("%s.tar.gz", path.Base(rootDir))
|
|
|
|
if err := snap.FileOp.Compress([]string{rootDir}, tmpDir, fileName, files.TarGz); err != nil {
|
2023-08-24 22:26:16 +08:00
|
|
|
snap.Status.Compress = err.Error()
|
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"compress": err.Error()})
|
2023-08-23 17:16:18 +08:00
|
|
|
return
|
|
|
|
}
|
2023-08-24 22:26:16 +08:00
|
|
|
|
2023-09-08 23:12:13 +08:00
|
|
|
stat, err := os.Stat(path.Join(tmpDir, fileName))
|
|
|
|
if err != nil {
|
|
|
|
snap.Status.Compress = err.Error()
|
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"compress": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
size := common.LoadSizeUnit(float64(stat.Size()))
|
|
|
|
global.LOG.Debugf("compress successful! size of file: %s", size)
|
2023-08-24 22:26:16 +08:00
|
|
|
snap.Status.Compress = constant.StatusDone
|
2023-09-08 23:12:13 +08:00
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"compress": constant.StatusDone, "size": size})
|
2023-08-23 17:16:18 +08:00
|
|
|
}
|
|
|
|
|
2023-08-24 22:26:16 +08:00
|
|
|
func snapUpload(snap snapHelper, account string, file string) {
|
2023-08-23 17:16:18 +08:00
|
|
|
source := path.Join(global.CONF.System.TmpDir, "system", path.Base(file))
|
|
|
|
defer func() {
|
|
|
|
global.LOG.Debugf("remove snapshot file %s", source)
|
|
|
|
_ = os.Remove(source)
|
|
|
|
}()
|
|
|
|
|
2023-08-24 22:26:16 +08:00
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"upload": constant.StatusUploading})
|
2023-08-23 17:16:18 +08:00
|
|
|
backup, err := backupRepo.Get(commonRepo.WithByType(account))
|
|
|
|
if err != nil {
|
2023-08-24 22:26:16 +08:00
|
|
|
snap.Status.Upload = err.Error()
|
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"upload": err.Error()})
|
2023-08-23 17:16:18 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
client, err := NewIBackupService().NewClient(&backup)
|
|
|
|
if err != nil {
|
2023-08-24 22:26:16 +08:00
|
|
|
snap.Status.Upload = err.Error()
|
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"upload": err.Error()})
|
2023-08-23 17:16:18 +08:00
|
|
|
return
|
|
|
|
}
|
2023-09-13 14:08:16 +08:00
|
|
|
target := path.Join(strings.TrimPrefix(backup.BackupPath, "/"), "system_snapshot", path.Base(file))
|
|
|
|
global.LOG.Debugf("start upload snapshot to %s, dir: %s", backup.Type, target)
|
2023-08-23 17:16:18 +08:00
|
|
|
if _, err := client.Upload(source, target); err != nil {
|
2023-08-24 22:26:16 +08:00
|
|
|
snap.Status.Upload = err.Error()
|
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"upload": err.Error()})
|
2023-08-23 17:16:18 +08:00
|
|
|
return
|
|
|
|
}
|
2023-08-24 22:26:16 +08:00
|
|
|
snap.Status.Upload = constant.StatusDone
|
|
|
|
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"upload": constant.StatusDone})
|
2023-08-23 17:16:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleSnapTar(sourceDir, targetDir, name, exclusionRules string) error {
|
|
|
|
if _, err := os.Stat(targetDir); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(targetDir, os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exStr := ""
|
|
|
|
excludes := strings.Split(exclusionRules, ";")
|
|
|
|
for _, exclude := range excludes {
|
|
|
|
if len(exclude) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
exStr += " --exclude "
|
|
|
|
exStr += exclude
|
|
|
|
}
|
|
|
|
|
2023-08-24 22:26:16 +08:00
|
|
|
commands := fmt.Sprintf("tar --warning=no-file-changed --ignore-failed-read -zcf %s %s -C %s .", targetDir+"/"+name, exStr, sourceDir)
|
2023-08-23 17:16:18 +08:00
|
|
|
global.LOG.Debug(commands)
|
|
|
|
stdout, err := cmd.ExecWithTimeOut(commands, 30*time.Minute)
|
|
|
|
if err != nil {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|