mirror of
https://github.com/Awuqing/BackupX.git
synced 2026-05-26 02:29:33 +08:00
优化: 多模块功能修复与体验改进 (#34)
1. 保留策略清理后自动删除空文件夹(新增 StorageDirCleaner 接口) 2. 备份任务删除时清理远端文件但保留备份记录 3. 节点管理修复:本机 IP/版本检测、Heartbeat OS/Arch 修正、新增编辑功能 4. 审计日志规范化:统一格式、丰富详情、节点操作增加审计记录 5. 系统设置移除一键更新操作,仅保留版本检查 6. Rclone 配置项分层展示(必填 + 高级可选折叠) 7. DirectoryPicker 目录选择器样式优化
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"backupx/server/internal/apperror"
|
||||
"backupx/server/internal/model"
|
||||
"backupx/server/internal/repository"
|
||||
"backupx/server/internal/storage"
|
||||
"backupx/server/internal/storage/codec"
|
||||
)
|
||||
|
||||
@@ -81,10 +82,12 @@ type BackupTaskScheduler interface {
|
||||
}
|
||||
|
||||
type BackupTaskService struct {
|
||||
tasks repository.BackupTaskRepository
|
||||
targets repository.StorageTargetRepository
|
||||
cipher *codec.ConfigCipher
|
||||
scheduler BackupTaskScheduler
|
||||
tasks repository.BackupTaskRepository
|
||||
targets repository.StorageTargetRepository
|
||||
records repository.BackupRecordRepository
|
||||
storageRegistry *storage.Registry
|
||||
cipher *codec.ConfigCipher
|
||||
scheduler BackupTaskScheduler
|
||||
}
|
||||
|
||||
func NewBackupTaskService(
|
||||
@@ -95,6 +98,12 @@ func NewBackupTaskService(
|
||||
return &BackupTaskService{tasks: tasks, targets: targets, cipher: cipher}
|
||||
}
|
||||
|
||||
// SetRecordsAndStorage 注入备份记录仓库和存储注册表,用于任务删除时清理远端文件。
|
||||
func (s *BackupTaskService) SetRecordsAndStorage(records repository.BackupRecordRepository, registry *storage.Registry) {
|
||||
s.records = records
|
||||
s.storageRegistry = registry
|
||||
}
|
||||
|
||||
func (s *BackupTaskService) SetScheduler(scheduler BackupTaskScheduler) {
|
||||
s.scheduler = scheduler
|
||||
}
|
||||
@@ -185,26 +194,80 @@ func (s *BackupTaskService) Update(ctx context.Context, id uint, input BackupTas
|
||||
return s.Get(ctx, item.ID)
|
||||
}
|
||||
|
||||
func (s *BackupTaskService) Delete(ctx context.Context, id uint) error {
|
||||
// DeleteResult 描述任务删除的结果信息,用于审计日志。
|
||||
type DeleteResult struct {
|
||||
TaskName string
|
||||
RecordCount int
|
||||
CleanedFiles int
|
||||
}
|
||||
|
||||
func (s *BackupTaskService) Delete(ctx context.Context, id uint) (*DeleteResult, error) {
|
||||
existing, err := s.tasks.FindByID(ctx, id)
|
||||
if err != nil {
|
||||
return apperror.Internal("BACKUP_TASK_GET_FAILED", "无法获取备份任务详情", err)
|
||||
return nil, apperror.Internal("BACKUP_TASK_GET_FAILED", "无法获取备份任务详情", err)
|
||||
}
|
||||
if existing == nil {
|
||||
return apperror.New(http.StatusNotFound, "BACKUP_TASK_NOT_FOUND", "备份任务不存在", fmt.Errorf("backup task %d not found", id))
|
||||
}
|
||||
if s.scheduler != nil {
|
||||
if err := s.scheduler.RemoveTask(ctx, id); err != nil {
|
||||
return apperror.Internal("BACKUP_TASK_SCHEDULE_FAILED", "无法移除备份任务调度", err)
|
||||
}
|
||||
}
|
||||
if err := s.tasks.Delete(ctx, id); err != nil {
|
||||
return apperror.Internal("BACKUP_TASK_DELETE_FAILED", "无法删除备份任务", err)
|
||||
return nil, apperror.New(http.StatusNotFound, "BACKUP_TASK_NOT_FOUND", "备份任务不存在", fmt.Errorf("backup task %d not found", id))
|
||||
}
|
||||
if s.scheduler != nil {
|
||||
_ = s.scheduler.RemoveTask(ctx, id)
|
||||
}
|
||||
return nil
|
||||
|
||||
// 清理远端存储文件(尽力而为,不阻止删除)
|
||||
result := &DeleteResult{TaskName: existing.Name}
|
||||
result.RecordCount, result.CleanedFiles = s.cleanupRemoteFiles(ctx, id)
|
||||
|
||||
if err := s.tasks.Delete(ctx, id); err != nil {
|
||||
return nil, apperror.Internal("BACKUP_TASK_DELETE_FAILED", "无法删除备份任务", err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// cleanupRemoteFiles 尽力删除任务相关的远端备份文件,返回记录数和成功删除的文件数。
|
||||
func (s *BackupTaskService) cleanupRemoteFiles(ctx context.Context, taskID uint) (recordCount int, cleanedFiles int) {
|
||||
if s.records == nil || s.storageRegistry == nil {
|
||||
return 0, 0
|
||||
}
|
||||
records, err := s.records.ListByTask(ctx, taskID)
|
||||
if err != nil {
|
||||
return 0, 0
|
||||
}
|
||||
recordCount = len(records)
|
||||
// 缓存 provider 避免同一存储目标重复创建连接
|
||||
providerCache := make(map[uint]storage.StorageProvider)
|
||||
for _, record := range records {
|
||||
if strings.TrimSpace(record.StoragePath) == "" {
|
||||
continue
|
||||
}
|
||||
provider, ok := providerCache[record.StorageTargetID]
|
||||
if !ok {
|
||||
provider, err = s.resolveStorageProvider(ctx, record.StorageTargetID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
providerCache[record.StorageTargetID] = provider
|
||||
}
|
||||
if err := provider.Delete(ctx, record.StoragePath); err == nil {
|
||||
cleanedFiles++
|
||||
}
|
||||
}
|
||||
return recordCount, cleanedFiles
|
||||
}
|
||||
|
||||
func (s *BackupTaskService) resolveStorageProvider(ctx context.Context, targetID uint) (storage.StorageProvider, error) {
|
||||
target, err := s.targets.FindByID(ctx, targetID)
|
||||
if err != nil || target == nil {
|
||||
return nil, fmt.Errorf("target %d not found", targetID)
|
||||
}
|
||||
configMap := map[string]any{}
|
||||
if err := s.cipher.DecryptJSON(target.ConfigCiphertext, &configMap); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
provider, err := s.storageRegistry.Create(ctx, target.Type, configMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return provider, nil
|
||||
}
|
||||
|
||||
func (s *BackupTaskService) Toggle(ctx context.Context, id uint, enabled bool) (*BackupTaskSummary, error) {
|
||||
|
||||
@@ -5,11 +5,13 @@ import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"backupx/server/internal/apperror"
|
||||
@@ -37,13 +39,19 @@ type NodeCreateInput struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
}
|
||||
|
||||
// NodeService manages the cluster nodes.
|
||||
type NodeService struct {
|
||||
repo repository.NodeRepository
|
||||
// NodeUpdateInput 是编辑节点的输入。
|
||||
type NodeUpdateInput struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
}
|
||||
|
||||
func NewNodeService(repo repository.NodeRepository) *NodeService {
|
||||
return &NodeService{repo: repo}
|
||||
// NodeService manages the cluster nodes.
|
||||
type NodeService struct {
|
||||
repo repository.NodeRepository
|
||||
version string
|
||||
}
|
||||
|
||||
func NewNodeService(repo repository.NodeRepository, version string) *NodeService {
|
||||
return &NodeService{repo: repo, version: version}
|
||||
}
|
||||
|
||||
// EnsureLocalNode creates the default "local" node if it does not exist.
|
||||
@@ -57,6 +65,8 @@ func (s *NodeService) EnsureLocalNode(ctx context.Context) error {
|
||||
existing.LastSeen = time.Now().UTC()
|
||||
hostname, _ := os.Hostname()
|
||||
existing.Hostname = hostname
|
||||
existing.IPAddress = detectLocalIP()
|
||||
existing.AgentVer = s.version
|
||||
existing.OS = runtime.GOOS
|
||||
existing.Arch = runtime.GOARCH
|
||||
return s.repo.Update(ctx, existing)
|
||||
@@ -64,14 +74,16 @@ func (s *NodeService) EnsureLocalNode(ctx context.Context) error {
|
||||
hostname, _ := os.Hostname()
|
||||
token, _ := generateToken()
|
||||
node := &model.Node{
|
||||
Name: "本机 (Local)",
|
||||
Hostname: hostname,
|
||||
Token: token,
|
||||
Status: model.NodeStatusOnline,
|
||||
IsLocal: true,
|
||||
OS: runtime.GOOS,
|
||||
Arch: runtime.GOARCH,
|
||||
LastSeen: time.Now().UTC(),
|
||||
Name: "本机 (Local)",
|
||||
Hostname: hostname,
|
||||
IPAddress: detectLocalIP(),
|
||||
Token: token,
|
||||
Status: model.NodeStatusOnline,
|
||||
IsLocal: true,
|
||||
OS: runtime.GOOS,
|
||||
Arch: runtime.GOARCH,
|
||||
AgentVer: s.version,
|
||||
LastSeen: time.Now().UTC(),
|
||||
}
|
||||
return s.repo.Create(ctx, node)
|
||||
}
|
||||
@@ -199,7 +211,7 @@ func (s *NodeService) ListDirectory(ctx context.Context, nodeID uint, path strin
|
||||
}
|
||||
|
||||
// Heartbeat updates the node status when an agent reports in.
|
||||
func (s *NodeService) Heartbeat(ctx context.Context, token string, hostname string, ip string, agentVer string) error {
|
||||
func (s *NodeService) Heartbeat(ctx context.Context, token string, hostname string, ip string, agentVer string, osName string, archName string) error {
|
||||
node, err := s.repo.FindByToken(ctx, token)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -211,12 +223,36 @@ func (s *NodeService) Heartbeat(ctx context.Context, token string, hostname stri
|
||||
node.Hostname = hostname
|
||||
node.IPAddress = ip
|
||||
node.AgentVer = agentVer
|
||||
node.OS = runtime.GOOS
|
||||
node.Arch = runtime.GOARCH
|
||||
if strings.TrimSpace(osName) != "" {
|
||||
node.OS = osName
|
||||
} else {
|
||||
node.OS = runtime.GOOS
|
||||
}
|
||||
if strings.TrimSpace(archName) != "" {
|
||||
node.Arch = archName
|
||||
} else {
|
||||
node.Arch = runtime.GOARCH
|
||||
}
|
||||
node.LastSeen = time.Now().UTC()
|
||||
return s.repo.Update(ctx, node)
|
||||
}
|
||||
|
||||
// Update 编辑节点名称。
|
||||
func (s *NodeService) Update(ctx context.Context, id uint, input NodeUpdateInput) (*NodeSummary, error) {
|
||||
node, err := s.repo.FindByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if node == nil {
|
||||
return nil, apperror.New(http.StatusNotFound, "NODE_NOT_FOUND", "节点不存在", nil)
|
||||
}
|
||||
node.Name = strings.TrimSpace(input.Name)
|
||||
if err := s.repo.Update(ctx, node); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.Get(ctx, id)
|
||||
}
|
||||
|
||||
// DirEntry represents a file or directory in a node's file system.
|
||||
type DirEntry struct {
|
||||
Name string `json:"name"`
|
||||
@@ -225,6 +261,22 @@ type DirEntry struct {
|
||||
Size int64 `json:"size"`
|
||||
}
|
||||
|
||||
// detectLocalIP 获取本机第一个非回环 IPv4 地址。
|
||||
func detectLocalIP() string {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
for _, addr := range addrs {
|
||||
if ipNet, ok := addr.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
|
||||
if ipNet.IP.To4() != nil {
|
||||
return ipNet.IP.String()
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func generateToken() (string, error) {
|
||||
b := make([]byte, 32)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
|
||||
@@ -5,8 +5,6 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
@@ -135,62 +133,3 @@ func (s *SystemService) GetInfo(_ context.Context) *SystemInfo {
|
||||
return info
|
||||
}
|
||||
|
||||
// UpdateApplyResult 描述自动更新执行结果。
|
||||
type UpdateApplyResult struct {
|
||||
Success bool `json:"success"`
|
||||
Message string `json:"message"`
|
||||
Output string `json:"output,omitempty"`
|
||||
}
|
||||
|
||||
// IsDockerEnvironment 检测当前是否运行在 Docker 容器中。
|
||||
func (s *SystemService) IsDockerEnvironment() bool {
|
||||
if _, err := os.Stat("/.dockerenv"); err == nil {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ApplyDockerUpdate 执行 Docker 自动更新:pull 新镜像 + recreate 容器。
|
||||
// 容器会在 docker compose up -d 后自动重启为新版本。
|
||||
func (s *SystemService) ApplyDockerUpdate(_ context.Context, targetVersion string) *UpdateApplyResult {
|
||||
if !s.IsDockerEnvironment() {
|
||||
return &UpdateApplyResult{Success: false, Message: "当前非 Docker 环境,请手动下载二进制更新"}
|
||||
}
|
||||
|
||||
image := "awuqing/backupx"
|
||||
tag := strings.TrimSpace(targetVersion)
|
||||
if tag == "" {
|
||||
tag = "latest"
|
||||
}
|
||||
pullTarget := image + ":" + tag
|
||||
|
||||
// Step 1: docker pull
|
||||
pullCmd := exec.Command("docker", "pull", pullTarget)
|
||||
pullOut, pullErr := pullCmd.CombinedOutput()
|
||||
if pullErr != nil {
|
||||
return &UpdateApplyResult{Success: false, Message: fmt.Sprintf("docker pull 失败: %v", pullErr), Output: string(pullOut)}
|
||||
}
|
||||
|
||||
// Step 2: docker compose up -d(后台执行,容器会自重启)
|
||||
// 检测 compose 命令
|
||||
composeBin := "docker"
|
||||
composeArgs := []string{"compose", "up", "-d"}
|
||||
if _, err := exec.LookPath("docker-compose"); err == nil {
|
||||
composeBin = "docker-compose"
|
||||
composeArgs = []string{"up", "-d"}
|
||||
}
|
||||
|
||||
// 异步执行,给 API 响应留时间
|
||||
go func() {
|
||||
time.Sleep(1 * time.Second)
|
||||
cmd := exec.Command(composeBin, composeArgs...)
|
||||
cmd.Dir = "/app" // Docker 容器中的工作目录
|
||||
_ = cmd.Run()
|
||||
}()
|
||||
|
||||
return &UpdateApplyResult{
|
||||
Success: true,
|
||||
Message: fmt.Sprintf("已拉取 %s,容器即将自动重启到新版本", pullTarget),
|
||||
Output: string(pullOut),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user