- go.mod: 将非法版本号 go 1.26.4 改为 go 1.26 - engine: 压缩失败时删除残留的半截 tar.gz 文件 - config: 支持仅按天数保留(count 和 days 均未设时才用默认 count=7) - engine: docker compose stop 失败时也尝试重启,避免容器停摆 - list: 仅显示真正的备份归档文件(新增导出 IsBackupArchive) - systemd: 移除 PrivateTmp,暂存目录改到安装目录内,避免私有 /tmp 被大目录撑爆
83 lines
1.9 KiB
Go
83 lines
1.9 KiB
Go
package backup
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
"git.misaka.ren/M1saka/docker_backup/internal/backup"
|
|
"git.misaka.ren/M1saka/docker_backup/internal/config"
|
|
)
|
|
|
|
var listCmd = &cobra.Command{
|
|
Use: "list",
|
|
Short: "List configured projects and their backups",
|
|
RunE: runList,
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(listCmd)
|
|
}
|
|
|
|
func runList(cmd *cobra.Command, args []string) error {
|
|
cfg, err := config.Load(cfgFile)
|
|
if err != nil {
|
|
return fmt.Errorf("load config: %w", err)
|
|
}
|
|
|
|
fmt.Printf("Config: %s\n\n", cfgFile)
|
|
|
|
for _, proj := range cfg.Projects {
|
|
fmt.Printf(" [%s]\n", proj.Name)
|
|
fmt.Printf(" path: %s\n", proj.Path)
|
|
fmt.Printf(" compose_file: %s\n", proj.ComposeFile)
|
|
if proj.Cron != "" {
|
|
fmt.Printf(" cron: %s\n", proj.Cron)
|
|
}
|
|
if len(proj.Exclude) > 0 {
|
|
fmt.Printf(" exclude: %s\n", strings.Join(proj.Exclude, ", "))
|
|
}
|
|
fmt.Printf(" retention: %d copies", proj.Retention.Count)
|
|
if proj.Retention.Days > 0 {
|
|
fmt.Printf(", %d days", proj.Retention.Days)
|
|
}
|
|
fmt.Println()
|
|
|
|
// List existing backups.
|
|
backupDir := filepath.Join(cfg.Global.BackupDir, proj.Name)
|
|
entries, err := os.ReadDir(backupDir)
|
|
if err != nil {
|
|
fmt.Printf(" backups: none (dir not found)\n\n")
|
|
continue
|
|
}
|
|
|
|
var backups []string
|
|
for _, e := range entries {
|
|
if !e.IsDir() && backup.IsBackupArchive(proj.Name, e.Name()) {
|
|
backups = append(backups, e.Name())
|
|
}
|
|
}
|
|
sort.Sort(sort.Reverse(sort.StringSlice(backups)))
|
|
|
|
if len(backups) == 0 {
|
|
fmt.Printf(" backups: none\n\n")
|
|
continue
|
|
}
|
|
|
|
fmt.Printf(" backups:\n")
|
|
for _, b := range backups {
|
|
info, _ := os.Stat(filepath.Join(backupDir, b))
|
|
size := ""
|
|
if info != nil {
|
|
size = backup.FormatSize(info.Size())
|
|
}
|
|
fmt.Printf(" %s %s\n", b, size)
|
|
}
|
|
fmt.Println()
|
|
}
|
|
|
|
return nil
|
|
} |