Skip to content

Commit

Permalink
feat: 适配面板备份
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Oct 27, 2024
1 parent 530ed71 commit 9075b43
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 88 deletions.
26 changes: 20 additions & 6 deletions internal/data/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,28 @@ func (r *backupRepo) createPanel(to string) error {
}

start := time.Now()
if err := io.Compress(filepath.Dir(filepath.Join(app.Root, "panel")), []string{
".",
"/usr/local/sbin/panel-cli",
"/usr/local/etc/panel/config.yml",
}, backup); err != nil {

temp, err := io.TempDir("panel-backup")
if err != nil {
return err
}
defer io.Remove(temp)

Check failure on line 340 in internal/data/backup.go

View workflow job for this annotation

GitHub Actions / golanci-lint

Error return value of `io.Remove` is not checked (errcheck)

if err = io.Cp(filepath.Join(app.Root, "panel"), temp); err != nil {
return err
}
if err = io.Cp("/usr/local/sbin/panel-cli", temp); err != nil {
return err
}
if err = io.Cp("/usr/local/etc/panel/config.yml", temp); err != nil {
return err
}

_ = io.Chmod(temp, 0600)
if err = io.Compress(temp, nil, backup); err != nil {
return err
}
if err := io.Chmod(backup, 0600); err != nil {
if err = io.Chmod(backup, 0600); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions internal/data/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {
if app.IsCli {
fmt.Println("|-恢复面板数据...")
}
if err := io.UnCompress("/tmp/panel-storage.zip", filepath.Join(app.Root, "panel")); err != nil {
if err := io.UnCompress("/tmp/panel-storage.zip", filepath.Join(app.Root, "panel", "storage")); err != nil {
return fmt.Errorf("恢复面板数据失败:%w", err)
}
if !io.Exists(filepath.Join(app.Root, "panel/storage/app.db")) {
Expand Down Expand Up @@ -455,7 +455,7 @@ func (r *settingRepo) FixPanel() error {
}
}

// tmp目录下如果有storage备份,则解压回去
// tmp 目录下如果有 storage 备份,则解压回去
if app.IsCli {
fmt.Println("|-恢复面板数据...")
}
Expand Down
8 changes: 4 additions & 4 deletions internal/service/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ func (s *CliService) BackupWebsite(ctx context.Context, cmd *cli.Command) error
fmt.Println("|-备份类型:网站")
fmt.Printf("|-备份目标:%s\n", cmd.String("name"))
if err := s.backupRepo.Create(biz.BackupTypeWebsite, cmd.String("name"), cmd.String("path")); err != nil {
return fmt.Errorf("|-备份失败:%v", err)
return fmt.Errorf("备份失败:%v", err)
}
fmt.Println(s.hr)
fmt.Printf("☆ 备份成功 [%s]\n", time.Now().Format(time.DateTime))
Expand All @@ -480,7 +480,7 @@ func (s *CliService) BackupDatabase(ctx context.Context, cmd *cli.Command) error
fmt.Printf("|-数据库:%s\n", cmd.String("type"))
fmt.Printf("|-备份目标:%s\n", cmd.String("name"))
if err := s.backupRepo.Create(biz.BackupType(cmd.String("type")), cmd.String("name"), cmd.String("path")); err != nil {
return fmt.Errorf("|-备份失败:%v", err)
return fmt.Errorf("备份失败:%v", err)
}
fmt.Println(s.hr)
fmt.Printf("☆ 备份成功 [%s]\n", time.Now().Format(time.DateTime))
Expand All @@ -494,7 +494,7 @@ func (s *CliService) BackupPanel(ctx context.Context, cmd *cli.Command) error {
fmt.Println(s.hr)
fmt.Println("|-备份类型:面板")
if err := s.backupRepo.Create(biz.BackupTypePanel, "", cmd.String("path")); err != nil {
return fmt.Errorf("|-备份失败:%v", err)
return fmt.Errorf("备份失败:%v", err)
}
fmt.Println(s.hr)
fmt.Printf("☆ 备份成功 [%s]\n", time.Now().Format(time.DateTime))
Expand All @@ -518,7 +518,7 @@ func (s *CliService) BackupClear(ctx context.Context, cmd *cli.Command) error {
fmt.Printf("|-清理目标:%s\n", cmd.String("file"))
fmt.Printf("|-保留份数:%d\n", cmd.Int("save"))
if err = s.backupRepo.ClearExpired(path, cmd.String("file"), int(cmd.Int("save"))); err != nil {
return fmt.Errorf("|-清理失败:%v", err)
return fmt.Errorf("清理失败:%v", err)
}
fmt.Println(s.hr)
fmt.Printf("☆ 清理成功 [%s]\n", time.Now().Format(time.DateTime))
Expand Down
12 changes: 5 additions & 7 deletions pkg/io/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,18 @@ func Compress(dir string, src []string, dst string) error {
for i, s := range src {
if strings.HasPrefix(s, dir) {
s = strings.TrimPrefix(s, dir)
src[i] = strings.TrimPrefix(s, "/")
}
if src[i] == "" {
src[i] = "."
if s != "" && s[0] == '/' {
src[i] = strings.TrimPrefix(s, "/")
}
}
}

cmd := new(exec.Cmd)
cmd.Dir = dir

format, err := formatArchiveByPath(dst)
if err != nil {
return err
}

cmd := new(exec.Cmd)
switch format {
case Zip:
cmd = exec.Command("zip", append([]string{"-qr", "-o", dst}, src...)...)
Expand All @@ -62,6 +59,7 @@ func Compress(dir string, src []string, dst string) error {
return errors.New("unsupported format")
}

cmd.Dir = dir
return cmd.Run()
}

Expand Down
72 changes: 3 additions & 69 deletions pkg/io/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -52,81 +51,16 @@ func Empty(path string) bool {
}

func Mv(src, dst string) error {
if err := os.Rename(src, dst); err != nil {
// 在不同的文件系统中无法使用 os.Rename
if _, err = shell.Execf(`mv -f '%s' '%s'`, src, dst); err != nil {
return err
}
}

return nil
_, err := shell.Execf(`mv -f '%s' '%s'`, src, dst)
return err
}

// Cp 复制文件或目录
func Cp(src, dst string) error {
srcInfo, err := os.Stat(src)
if err != nil {
return err
}

if srcInfo.IsDir() {
return copyDir(src, dst)
}
return copyFile(src, dst)
}

func copyFile(src, dst string) error {
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()

dstFile, err := os.Create(dst)
if err != nil {
return err
}
defer dstFile.Close()

_, err = io.Copy(dstFile, srcFile)
_, err := shell.Execf(`cp -rf '%s' '%s'`, src, dst)
return err
}

func copyDir(src, dst string) error {
srcInfo, err := os.Stat(src)
if err != nil {
return err
}

err = os.MkdirAll(dst, srcInfo.Mode())
if err != nil {
return err
}

entries, err := os.ReadDir(src)
if err != nil {
return err
}

for _, entry := range entries {
srcPath := filepath.Join(src, entry.Name())
dstPath := filepath.Join(dst, entry.Name())

if entry.IsDir() {
err = copyDir(srcPath, dstPath)
if err != nil {
return err
}
} else {
err = copyFile(srcPath, dstPath)
if err != nil {
return err
}
}
}
return nil
}

// Size 获取路径大小
func Size(path string) (int64, error) {
var size int64
Expand Down

0 comments on commit 9075b43

Please sign in to comment.