2023-01-06 18:53:25 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-01-09 22:55:10 +08:00
|
|
|
"encoding/json"
|
2023-01-06 18:53:25 +08:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-07-27 15:52:25 +08:00
|
|
|
"path"
|
2023-01-09 22:55:10 +08:00
|
|
|
"strings"
|
2023-08-23 17:16:18 +08:00
|
|
|
"sync"
|
2023-01-06 18:53:25 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"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/cmd"
|
2023-09-14 14:06:13 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/compose"
|
2023-01-06 18:53:25 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
"github.com/pkg/errors"
|
2024-03-04 22:59:11 +08:00
|
|
|
"github.com/shirou/gopsutil/v3/host"
|
2023-01-06 18:53:25 +08:00
|
|
|
)
|
|
|
|
|
2023-02-09 16:36:28 +08:00
|
|
|
type SnapshotService struct {
|
|
|
|
OriginalPath string
|
|
|
|
}
|
2023-01-06 18:53:25 +08:00
|
|
|
|
|
|
|
type ISnapshotService interface {
|
2023-02-10 15:55:56 +08:00
|
|
|
SearchWithPage(req dto.SearchWithPage) (int64, interface{}, error)
|
2023-01-09 22:55:10 +08:00
|
|
|
SnapshotCreate(req dto.SnapshotCreate) error
|
|
|
|
SnapshotRecover(req dto.SnapshotRecover) error
|
|
|
|
SnapshotRollback(req dto.SnapshotRecover) error
|
2023-02-13 15:48:18 +08:00
|
|
|
SnapshotImport(req dto.SnapshotImport) error
|
2023-01-09 22:55:10 +08:00
|
|
|
Delete(req dto.BatchDeleteReq) error
|
|
|
|
|
2023-08-23 17:16:18 +08:00
|
|
|
LoadSnapShotStatus(id uint) (*dto.SnapshotStatus, error)
|
|
|
|
|
2023-02-13 15:48:18 +08:00
|
|
|
UpdateDescription(req dto.UpdateDescription) error
|
2023-01-09 22:55:10 +08:00
|
|
|
readFromJson(path string) (SnapshotJson, error)
|
2023-08-24 22:26:16 +08:00
|
|
|
|
2024-01-24 17:41:56 +08:00
|
|
|
HandleSnapshot(isCronjob bool, logPath string, req dto.SnapshotCreate, timeNow string) (string, error)
|
2023-01-06 18:53:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewISnapshotService() ISnapshotService {
|
|
|
|
return &SnapshotService{}
|
|
|
|
}
|
|
|
|
|
2023-02-10 15:55:56 +08:00
|
|
|
func (u *SnapshotService) SearchWithPage(req dto.SearchWithPage) (int64, interface{}, error) {
|
|
|
|
total, systemBackups, err := snapshotRepo.Page(req.Page, req.PageSize, commonRepo.WithLikeName(req.Info))
|
2023-01-06 18:53:25 +08:00
|
|
|
var dtoSnap []dto.SnapshotInfo
|
2023-01-09 22:55:10 +08:00
|
|
|
for _, systemBackup := range systemBackups {
|
2023-01-06 18:53:25 +08:00
|
|
|
var item dto.SnapshotInfo
|
2023-01-09 22:55:10 +08:00
|
|
|
if err := copier.Copy(&item, &systemBackup); err != nil {
|
2023-01-06 18:53:25 +08:00
|
|
|
return 0, nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
|
|
|
dtoSnap = append(dtoSnap, item)
|
|
|
|
}
|
|
|
|
return total, dtoSnap, err
|
|
|
|
}
|
|
|
|
|
2023-02-13 15:48:18 +08:00
|
|
|
func (u *SnapshotService) SnapshotImport(req dto.SnapshotImport) error {
|
|
|
|
if len(req.Names) == 0 {
|
|
|
|
return fmt.Errorf("incorrect snapshot request body: %v", req.Names)
|
|
|
|
}
|
2023-06-29 10:30:10 +08:00
|
|
|
for _, snapName := range req.Names {
|
2023-07-12 16:39:16 +08:00
|
|
|
snap, _ := snapshotRepo.Get(commonRepo.WithByName(strings.ReplaceAll(snapName, ".tar.gz", "")))
|
2023-06-29 10:30:10 +08:00
|
|
|
if snap.ID != 0 {
|
|
|
|
return constant.ErrRecordExist
|
|
|
|
}
|
|
|
|
}
|
2023-02-13 15:48:18 +08:00
|
|
|
for _, snap := range req.Names {
|
2023-09-14 11:38:12 +08:00
|
|
|
shortName := strings.TrimPrefix(snap, "snapshot_")
|
|
|
|
nameItems := strings.Split(shortName, "_")
|
|
|
|
if !strings.HasPrefix(shortName, "1panel_v") || !strings.HasSuffix(shortName, ".tar.gz") || len(nameItems) != 3 {
|
|
|
|
return fmt.Errorf("incorrect snapshot name format of %s", shortName)
|
2023-02-13 15:48:18 +08:00
|
|
|
}
|
2023-03-21 18:46:31 +08:00
|
|
|
if strings.HasSuffix(snap, ".tar.gz") {
|
|
|
|
snap = strings.ReplaceAll(snap, ".tar.gz", "")
|
|
|
|
}
|
2023-02-13 15:48:18 +08:00
|
|
|
itemSnap := model.Snapshot{
|
2024-01-29 10:28:49 +08:00
|
|
|
Name: snap,
|
|
|
|
From: req.From,
|
|
|
|
DefaultDownload: req.From,
|
|
|
|
Version: nameItems[1],
|
|
|
|
Description: req.Description,
|
|
|
|
Status: constant.StatusSuccess,
|
2023-02-13 15:48:18 +08:00
|
|
|
}
|
|
|
|
if err := snapshotRepo.Create(&itemSnap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *SnapshotService) UpdateDescription(req dto.UpdateDescription) error {
|
|
|
|
return snapshotRepo.Update(req.ID, map[string]interface{}{"description": req.Description})
|
|
|
|
}
|
|
|
|
|
2023-08-23 17:16:18 +08:00
|
|
|
func (u *SnapshotService) LoadSnapShotStatus(id uint) (*dto.SnapshotStatus, error) {
|
|
|
|
var data dto.SnapshotStatus
|
|
|
|
status, err := snapshotRepo.GetStatus(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := copier.Copy(&data, &status); err != nil {
|
|
|
|
return nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
|
|
|
return &data, nil
|
|
|
|
}
|
|
|
|
|
2023-01-09 22:55:10 +08:00
|
|
|
type SnapshotJson struct {
|
2023-02-13 15:48:18 +08:00
|
|
|
OldBaseDir string `json:"oldBaseDir"`
|
2023-01-09 22:55:10 +08:00
|
|
|
OldDockerDataDir string `json:"oldDockerDataDir"`
|
2023-05-30 15:30:57 +08:00
|
|
|
OldBackupDataDir string `json:"oldBackupDataDir"`
|
2023-01-09 22:55:10 +08:00
|
|
|
OldPanelDataDir string `json:"oldPanelDataDir"`
|
|
|
|
|
2023-02-13 15:48:18 +08:00
|
|
|
BaseDir string `json:"baseDir"`
|
2023-01-09 22:55:10 +08:00
|
|
|
DockerDataDir string `json:"dockerDataDir"`
|
|
|
|
BackupDataDir string `json:"backupDataDir"`
|
|
|
|
PanelDataDir string `json:"panelDataDir"`
|
|
|
|
LiveRestoreEnabled bool `json:"liveRestoreEnabled"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *SnapshotService) SnapshotCreate(req dto.SnapshotCreate) error {
|
2024-01-24 17:41:56 +08:00
|
|
|
if _, err := u.HandleSnapshot(false, "", req, time.Now().Format("20060102150405")); err != nil {
|
2023-01-06 18:53:25 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-01-09 22:55:10 +08:00
|
|
|
return nil
|
|
|
|
}
|
2023-01-06 18:53:25 +08:00
|
|
|
|
2023-01-09 22:55:10 +08:00
|
|
|
func (u *SnapshotService) SnapshotRecover(req dto.SnapshotRecover) error {
|
2023-10-07 15:46:44 +08:00
|
|
|
global.LOG.Info("start to recover panel by snapshot now")
|
2023-01-09 22:55:10 +08:00
|
|
|
snap, err := snapshotRepo.Get(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
2023-01-06 18:53:25 +08:00
|
|
|
return err
|
|
|
|
}
|
2024-03-04 22:59:11 +08:00
|
|
|
if hasOs(snap.Name) && !strings.Contains(snap.Name, loadOs()) {
|
|
|
|
return fmt.Errorf("Restoring snapshots(%s) between different server architectures(%s) is not supported.", snap.Name, loadOs())
|
|
|
|
}
|
2023-01-09 22:55:10 +08:00
|
|
|
if !req.IsNew && len(snap.InterruptStep) != 0 && len(snap.RollbackStatus) != 0 {
|
|
|
|
return fmt.Errorf("the snapshot has been rolled back and cannot be restored again")
|
|
|
|
}
|
2024-03-01 13:51:34 +08:00
|
|
|
|
2023-08-01 18:40:54 +08:00
|
|
|
baseDir := path.Join(global.CONF.System.TmpDir, fmt.Sprintf("system/%s", snap.Name))
|
2023-01-09 22:55:10 +08:00
|
|
|
if _, err := os.Stat(baseDir); err != nil && os.IsNotExist(err) {
|
|
|
|
_ = os.MkdirAll(baseDir, os.ModePerm)
|
2023-01-06 18:53:25 +08:00
|
|
|
}
|
|
|
|
|
2023-01-09 22:55:10 +08:00
|
|
|
_ = snapshotRepo.Update(snap.ID, map[string]interface{}{"recover_status": constant.StatusWaiting})
|
2023-02-09 16:36:28 +08:00
|
|
|
_ = settingRepo.Update("SystemStatus", "Recovering")
|
2024-03-01 13:51:34 +08:00
|
|
|
go u.HandleSnapshotRecover(snap, true, req)
|
2023-01-09 22:55:10 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *SnapshotService) SnapshotRollback(req dto.SnapshotRecover) error {
|
|
|
|
global.LOG.Info("start to rollback now")
|
|
|
|
snap, err := snapshotRepo.Get(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-01 13:51:34 +08:00
|
|
|
req.IsNew = false
|
|
|
|
snap.InterruptStep = "Readjson"
|
|
|
|
go u.HandleSnapshotRecover(snap, false, req)
|
2023-01-09 22:55:10 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *SnapshotService) readFromJson(path string) (SnapshotJson, error) {
|
|
|
|
var snap SnapshotJson
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
|
|
|
return snap, fmt.Errorf("find snapshot json file in recover package failed, err: %v", err)
|
|
|
|
}
|
|
|
|
fileByte, err := os.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return snap, fmt.Errorf("read file from path %s failed, err: %v", path, err)
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(fileByte, &snap); err != nil {
|
|
|
|
return snap, fmt.Errorf("unmarshal snapjson failed, err: %v", err)
|
|
|
|
}
|
|
|
|
return snap, nil
|
|
|
|
}
|
|
|
|
|
2024-01-24 17:41:56 +08:00
|
|
|
func (u *SnapshotService) HandleSnapshot(isCronjob bool, logPath string, req dto.SnapshotCreate, timeNow string) (string, error) {
|
2023-08-24 22:26:16 +08:00
|
|
|
localDir, err := loadLocalDir()
|
|
|
|
if err != nil {
|
2024-01-24 17:41:56 +08:00
|
|
|
return "", err
|
2023-08-24 22:26:16 +08:00
|
|
|
}
|
|
|
|
var (
|
|
|
|
rootDir string
|
|
|
|
snap model.Snapshot
|
|
|
|
snapStatus model.SnapshotStatus
|
|
|
|
)
|
|
|
|
|
|
|
|
if req.ID == 0 {
|
|
|
|
versionItem, _ := settingRepo.Get(settingRepo.WithByKey("SystemVersion"))
|
2024-03-04 22:59:11 +08:00
|
|
|
|
|
|
|
name := fmt.Sprintf("1panel_%s_%s_%s", versionItem.Value, loadOs(), timeNow)
|
2023-08-24 22:26:16 +08:00
|
|
|
if isCronjob {
|
2024-03-04 22:59:11 +08:00
|
|
|
name = fmt.Sprintf("snapshot_1panel_%s_%s_%s", versionItem.Value, loadOs(), timeNow)
|
2023-08-24 22:26:16 +08:00
|
|
|
}
|
|
|
|
rootDir = path.Join(localDir, "system", name)
|
|
|
|
|
|
|
|
snap = model.Snapshot{
|
2024-01-25 11:20:42 +08:00
|
|
|
Name: name,
|
|
|
|
Description: req.Description,
|
|
|
|
From: req.From,
|
|
|
|
DefaultDownload: req.DefaultDownload,
|
|
|
|
Version: versionItem.Value,
|
|
|
|
Status: constant.StatusWaiting,
|
2023-08-24 22:26:16 +08:00
|
|
|
}
|
|
|
|
_ = snapshotRepo.Create(&snap)
|
|
|
|
snapStatus.SnapID = snap.ID
|
|
|
|
_ = snapshotRepo.CreateStatus(&snapStatus)
|
|
|
|
} else {
|
|
|
|
snap, err = snapshotRepo.Get(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
2024-01-24 17:41:56 +08:00
|
|
|
return "", err
|
2023-08-24 22:26:16 +08:00
|
|
|
}
|
|
|
|
snapStatus, _ = snapshotRepo.GetStatus(snap.ID)
|
|
|
|
if snapStatus.ID == 0 {
|
|
|
|
snapStatus.SnapID = snap.ID
|
|
|
|
_ = snapshotRepo.CreateStatus(&snapStatus)
|
|
|
|
}
|
|
|
|
rootDir = path.Join(localDir, fmt.Sprintf("system/%s", snap.Name))
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
itemHelper := snapHelper{SnapID: snap.ID, Status: &snapStatus, Wg: &wg, FileOp: files.NewFileOp(), Ctx: context.Background()}
|
|
|
|
backupPanelDir := path.Join(rootDir, "1panel")
|
|
|
|
_ = os.MkdirAll(backupPanelDir, os.ModePerm)
|
|
|
|
backupDockerDir := path.Join(rootDir, "docker")
|
|
|
|
_ = os.MkdirAll(backupDockerDir, os.ModePerm)
|
|
|
|
|
|
|
|
jsonItem := SnapshotJson{
|
|
|
|
BaseDir: global.CONF.System.BaseDir,
|
|
|
|
BackupDataDir: localDir,
|
|
|
|
PanelDataDir: path.Join(global.CONF.System.BaseDir, "1panel"),
|
|
|
|
}
|
2024-01-24 17:41:56 +08:00
|
|
|
loadLogByStatus(snapStatus, logPath)
|
2023-08-24 22:26:16 +08:00
|
|
|
if snapStatus.PanelInfo != constant.StatusDone {
|
|
|
|
wg.Add(1)
|
|
|
|
go snapJson(itemHelper, jsonItem, rootDir)
|
|
|
|
}
|
|
|
|
if snapStatus.Panel != constant.StatusDone {
|
|
|
|
wg.Add(1)
|
|
|
|
go snapPanel(itemHelper, backupPanelDir)
|
|
|
|
}
|
|
|
|
if snapStatus.DaemonJson != constant.StatusDone {
|
|
|
|
wg.Add(1)
|
|
|
|
go snapDaemonJson(itemHelper, backupDockerDir)
|
|
|
|
}
|
|
|
|
if snapStatus.AppData != constant.StatusDone {
|
|
|
|
wg.Add(1)
|
|
|
|
go snapAppData(itemHelper, backupDockerDir)
|
|
|
|
}
|
|
|
|
if snapStatus.BackupData != constant.StatusDone {
|
|
|
|
wg.Add(1)
|
|
|
|
go snapBackup(itemHelper, localDir, backupPanelDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isCronjob {
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
if !checkIsAllDone(snap.ID) {
|
|
|
|
_ = snapshotRepo.Update(snap.ID, map[string]interface{}{"status": constant.StatusFailed})
|
|
|
|
return
|
|
|
|
}
|
2024-03-05 11:14:07 +08:00
|
|
|
if snapStatus.PanelData != constant.StatusDone {
|
|
|
|
snapPanelData(itemHelper, localDir, backupPanelDir)
|
|
|
|
}
|
2024-01-02 17:10:13 +08:00
|
|
|
if snapStatus.PanelData != constant.StatusDone {
|
|
|
|
_ = snapshotRepo.Update(snap.ID, map[string]interface{}{"status": constant.StatusFailed})
|
|
|
|
return
|
|
|
|
}
|
2024-03-05 11:14:07 +08:00
|
|
|
if snapStatus.Compress != constant.StatusDone {
|
|
|
|
snapCompress(itemHelper, rootDir)
|
|
|
|
}
|
2023-08-24 22:26:16 +08:00
|
|
|
if snapStatus.Compress != constant.StatusDone {
|
|
|
|
_ = snapshotRepo.Update(snap.ID, map[string]interface{}{"status": constant.StatusFailed})
|
|
|
|
return
|
|
|
|
}
|
2024-03-05 11:14:07 +08:00
|
|
|
if snapStatus.Upload != constant.StatusDone {
|
|
|
|
snapUpload(itemHelper, req.From, fmt.Sprintf("%s.tar.gz", rootDir))
|
|
|
|
}
|
2023-08-24 22:26:16 +08:00
|
|
|
if snapStatus.Upload != constant.StatusDone {
|
|
|
|
_ = snapshotRepo.Update(snap.ID, map[string]interface{}{"status": constant.StatusFailed})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = snapshotRepo.Update(snap.ID, map[string]interface{}{"status": constant.StatusSuccess})
|
|
|
|
}()
|
2024-01-24 17:41:56 +08:00
|
|
|
return "", nil
|
2023-08-24 22:26:16 +08:00
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
if !checkIsAllDone(snap.ID) {
|
|
|
|
_ = snapshotRepo.Update(snap.ID, map[string]interface{}{"status": constant.StatusFailed})
|
2024-01-24 17:41:56 +08:00
|
|
|
loadLogByStatus(snapStatus, logPath)
|
|
|
|
return snap.Name, fmt.Errorf("snapshot %s backup failed", snap.Name)
|
2023-08-24 22:26:16 +08:00
|
|
|
}
|
2024-01-25 11:20:42 +08:00
|
|
|
loadLogByStatus(snapStatus, logPath)
|
2024-01-18 10:38:25 +08:00
|
|
|
snapPanelData(itemHelper, localDir, backupPanelDir)
|
|
|
|
if snapStatus.PanelData != constant.StatusDone {
|
|
|
|
_ = snapshotRepo.Update(snap.ID, map[string]interface{}{"status": constant.StatusFailed})
|
2024-01-24 17:41:56 +08:00
|
|
|
loadLogByStatus(snapStatus, logPath)
|
|
|
|
return snap.Name, fmt.Errorf("snapshot %s 1panel data failed", snap.Name)
|
2024-01-18 10:38:25 +08:00
|
|
|
}
|
2024-01-25 11:20:42 +08:00
|
|
|
loadLogByStatus(snapStatus, logPath)
|
2023-08-24 22:26:16 +08:00
|
|
|
snapCompress(itemHelper, rootDir)
|
|
|
|
if snapStatus.Compress != constant.StatusDone {
|
|
|
|
_ = snapshotRepo.Update(snap.ID, map[string]interface{}{"status": constant.StatusFailed})
|
2024-01-24 17:41:56 +08:00
|
|
|
loadLogByStatus(snapStatus, logPath)
|
|
|
|
return snap.Name, fmt.Errorf("snapshot %s compress failed", snap.Name)
|
2023-08-24 22:26:16 +08:00
|
|
|
}
|
2024-01-25 11:20:42 +08:00
|
|
|
loadLogByStatus(snapStatus, logPath)
|
2023-08-24 22:26:16 +08:00
|
|
|
snapUpload(itemHelper, req.From, fmt.Sprintf("%s.tar.gz", rootDir))
|
|
|
|
if snapStatus.Upload != constant.StatusDone {
|
|
|
|
_ = snapshotRepo.Update(snap.ID, map[string]interface{}{"status": constant.StatusFailed})
|
2024-01-24 17:41:56 +08:00
|
|
|
loadLogByStatus(snapStatus, logPath)
|
|
|
|
return snap.Name, fmt.Errorf("snapshot %s upload failed", snap.Name)
|
2023-08-24 22:26:16 +08:00
|
|
|
}
|
|
|
|
_ = snapshotRepo.Update(snap.ID, map[string]interface{}{"status": constant.StatusSuccess})
|
2024-01-24 17:41:56 +08:00
|
|
|
loadLogByStatus(snapStatus, logPath)
|
|
|
|
return snap.Name, nil
|
2023-08-24 22:26:16 +08:00
|
|
|
}
|
|
|
|
|
2023-01-09 22:55:10 +08:00
|
|
|
func (u *SnapshotService) Delete(req dto.BatchDeleteReq) error {
|
|
|
|
backups, _ := snapshotRepo.GetList(commonRepo.WithIdsIn(req.Ids))
|
|
|
|
localDir, err := loadLocalDir()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, snap := range backups {
|
2023-08-24 22:26:16 +08:00
|
|
|
itemFile := path.Join(localDir, "system", snap.Name)
|
|
|
|
_ = os.RemoveAll(itemFile)
|
|
|
|
|
|
|
|
itemTarFile := path.Join(global.CONF.System.TmpDir, "system", snap.Name+".tar.gz")
|
|
|
|
_ = os.Remove(itemTarFile)
|
|
|
|
|
2023-08-23 17:16:18 +08:00
|
|
|
_ = snapshotRepo.DeleteStatus(snap.ID)
|
2023-01-06 18:53:25 +08:00
|
|
|
}
|
2023-01-09 22:55:10 +08:00
|
|
|
if err := snapshotRepo.Delete(commonRepo.WithIdsIn(req.Ids)); err != nil {
|
2023-01-06 18:53:25 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-09 22:55:10 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-01 13:51:34 +08:00
|
|
|
func updateRecoverStatus(id uint, isRecover bool, interruptStep, status, message string) {
|
|
|
|
if isRecover {
|
|
|
|
if status != constant.StatusSuccess {
|
|
|
|
global.LOG.Errorf("recover failed, err: %s", message)
|
|
|
|
}
|
|
|
|
if err := snapshotRepo.Update(id, map[string]interface{}{
|
|
|
|
"interrupt_step": interruptStep,
|
|
|
|
"recover_status": status,
|
|
|
|
"recover_message": message,
|
|
|
|
"last_recovered_at": time.Now().Format("2006-01-02 15:04:05"),
|
|
|
|
}); err != nil {
|
|
|
|
global.LOG.Errorf("update snap recover status failed, err: %v", err)
|
|
|
|
}
|
|
|
|
_ = settingRepo.Update("SystemStatus", "Free")
|
|
|
|
return
|
2023-01-09 22:55:10 +08:00
|
|
|
}
|
2023-02-09 16:36:28 +08:00
|
|
|
_ = settingRepo.Update("SystemStatus", "Free")
|
2023-01-09 22:55:10 +08:00
|
|
|
if status == constant.StatusSuccess {
|
|
|
|
if err := snapshotRepo.Update(id, map[string]interface{}{
|
|
|
|
"recover_status": "",
|
|
|
|
"recover_message": "",
|
|
|
|
"interrupt_step": "",
|
|
|
|
"rollback_status": "",
|
|
|
|
"rollback_message": "",
|
|
|
|
"last_rollbacked_at": time.Now().Format("2006-01-02 15:04:05"),
|
|
|
|
}); err != nil {
|
|
|
|
global.LOG.Errorf("update snap recover status failed, err: %v", err)
|
2023-01-06 18:53:25 +08:00
|
|
|
}
|
2023-01-09 22:55:10 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
global.LOG.Errorf("rollback failed, err: %s", message)
|
|
|
|
if err := snapshotRepo.Update(id, map[string]interface{}{
|
|
|
|
"rollback_status": status,
|
|
|
|
"rollback_message": message,
|
|
|
|
"last_rollbacked_at": time.Now().Format("2006-01-02 15:04:05"),
|
|
|
|
}); err != nil {
|
|
|
|
global.LOG.Errorf("update snap recover status failed, err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-24 22:26:16 +08:00
|
|
|
func cpBinary(src []string, dst string) error {
|
2023-09-19 17:20:22 +08:00
|
|
|
global.LOG.Debugf(fmt.Sprintf("\\cp -f %s %s", strings.Join(src, " "), dst))
|
|
|
|
stdout, err := cmd.Exec(fmt.Sprintf("\\cp -f %s %s", strings.Join(src, " "), dst))
|
2023-01-09 22:55:10 +08:00
|
|
|
if err != nil {
|
2023-09-19 17:20:22 +08:00
|
|
|
return fmt.Errorf("cp file failed, stdout: %v, err: %v", stdout, err)
|
2023-01-09 22:55:10 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *SnapshotService) handleUnTar(sourceDir, 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-09 14:53:29 +08:00
|
|
|
|
|
|
|
commands := fmt.Sprintf("tar -zxf %s -C %s .", sourceDir, targetDir)
|
|
|
|
global.LOG.Debug(commands)
|
2023-04-18 17:32:59 +08:00
|
|
|
stdout, err := cmd.ExecWithTimeOut(commands, 30*time.Minute)
|
2023-01-09 22:55:10 +08:00
|
|
|
if err != nil {
|
2023-08-02 16:47:30 +08:00
|
|
|
if len(stdout) != 0 {
|
|
|
|
global.LOG.Errorf("do handle untar failed, stdout: %s, err: %v", stdout, err)
|
|
|
|
return fmt.Errorf("do handle untar failed, stdout: %s, err: %v", stdout, err)
|
|
|
|
}
|
2023-01-09 22:55:10 +08:00
|
|
|
}
|
2023-01-06 18:53:25 +08:00
|
|
|
return nil
|
|
|
|
}
|
2023-08-01 18:40:54 +08:00
|
|
|
|
|
|
|
func rebuildAllAppInstall() error {
|
2023-09-14 14:06:13 +08:00
|
|
|
global.LOG.Debug("start to rebuild all app")
|
2023-08-01 18:40:54 +08:00
|
|
|
appInstalls, err := appInstallRepo.ListBy()
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("get all app installed for rebuild failed, err: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2023-09-14 14:06:13 +08:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < len(appInstalls); i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
appInstalls[i].Status = constant.Rebuilding
|
|
|
|
_ = appInstallRepo.Save(context.Background(), &appInstalls[i])
|
|
|
|
go func(app model.AppInstall) {
|
|
|
|
defer wg.Done()
|
|
|
|
dockerComposePath := app.GetComposePath()
|
|
|
|
out, err := compose.Down(dockerComposePath)
|
|
|
|
if err != nil {
|
|
|
|
_ = handleErr(app, err, out)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
out, err = compose.Up(dockerComposePath)
|
|
|
|
if err != nil {
|
|
|
|
_ = handleErr(app, err, out)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
app.Status = constant.Running
|
|
|
|
_ = appInstallRepo.Save(context.Background(), &app)
|
|
|
|
}(appInstalls[i])
|
2023-08-01 18:40:54 +08:00
|
|
|
}
|
2023-09-14 14:06:13 +08:00
|
|
|
wg.Wait()
|
2023-08-01 18:40:54 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-23 17:16:18 +08:00
|
|
|
func checkIsAllDone(snapID uint) bool {
|
|
|
|
status, err := snapshotRepo.GetStatus(snapID)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2024-03-01 13:51:34 +08:00
|
|
|
isOK, _ := checkAllDone(status)
|
|
|
|
return isOK
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkAllDone(status model.SnapshotStatus) (bool, string) {
|
2023-08-23 17:16:18 +08:00
|
|
|
if status.Panel != constant.StatusDone {
|
2024-03-01 13:51:34 +08:00
|
|
|
return false, status.Panel
|
2023-08-23 17:16:18 +08:00
|
|
|
}
|
|
|
|
if status.PanelInfo != constant.StatusDone {
|
2024-03-01 13:51:34 +08:00
|
|
|
return false, status.PanelInfo
|
2023-08-23 17:16:18 +08:00
|
|
|
}
|
|
|
|
if status.DaemonJson != constant.StatusDone {
|
2024-03-01 13:51:34 +08:00
|
|
|
return false, status.DaemonJson
|
2023-08-23 17:16:18 +08:00
|
|
|
}
|
|
|
|
if status.AppData != constant.StatusDone {
|
2024-03-01 13:51:34 +08:00
|
|
|
return false, status.AppData
|
2023-08-23 17:16:18 +08:00
|
|
|
}
|
|
|
|
if status.BackupData != constant.StatusDone {
|
2024-03-01 13:51:34 +08:00
|
|
|
return false, status.BackupData
|
2023-08-23 17:16:18 +08:00
|
|
|
}
|
2024-03-01 13:51:34 +08:00
|
|
|
return true, ""
|
2023-08-23 17:16:18 +08:00
|
|
|
}
|
2023-08-24 22:26:16 +08:00
|
|
|
|
2024-01-24 17:41:56 +08:00
|
|
|
func loadLogByStatus(status model.SnapshotStatus, logPath string) {
|
2023-08-24 22:26:16 +08:00
|
|
|
logs := ""
|
|
|
|
logs += fmt.Sprintf("Write 1Panel basic information: %s \n", status.PanelInfo)
|
|
|
|
logs += fmt.Sprintf("Backup 1Panel system files: %s \n", status.Panel)
|
|
|
|
logs += fmt.Sprintf("Backup Docker configuration file: %s \n", status.DaemonJson)
|
|
|
|
logs += fmt.Sprintf("Backup installed apps from 1Panel: %s \n", status.AppData)
|
|
|
|
logs += fmt.Sprintf("Backup 1Panel data directory: %s \n", status.PanelData)
|
|
|
|
logs += fmt.Sprintf("Backup local backup directory for 1Panel: %s \n", status.BackupData)
|
2023-09-08 23:12:13 +08:00
|
|
|
logs += fmt.Sprintf("Create snapshot file: %s \n", status.Compress)
|
|
|
|
logs += fmt.Sprintf("Snapshot size: %s \n", status.Size)
|
|
|
|
logs += fmt.Sprintf("Upload snapshot file: %s \n", status.Upload)
|
2023-08-24 22:26:16 +08:00
|
|
|
|
2024-01-24 17:41:56 +08:00
|
|
|
file, err := os.OpenFile(logPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
_, _ = file.Write([]byte(logs))
|
2023-08-24 22:26:16 +08:00
|
|
|
}
|
2024-03-04 22:59:11 +08:00
|
|
|
|
|
|
|
func hasOs(name string) bool {
|
|
|
|
return strings.Contains(name, "amd64") ||
|
|
|
|
strings.Contains(name, "arm64") ||
|
|
|
|
strings.Contains(name, "armv7") ||
|
|
|
|
strings.Contains(name, "ppc64le") ||
|
|
|
|
strings.Contains(name, "s390x")
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadOs() string {
|
|
|
|
hostInfo, _ := host.Info()
|
|
|
|
switch hostInfo.KernelArch {
|
|
|
|
case "x86_64":
|
|
|
|
return "amd64"
|
|
|
|
case "armv7l":
|
|
|
|
return "armv7"
|
|
|
|
default:
|
|
|
|
return hostInfo.KernelArch
|
|
|
|
}
|
|
|
|
}
|