Skip to content

Commit 1a427c4

Browse files
committed
fix: 确保每次备份都使用独立的时间戳
1 parent 0268f46 commit 1a427c4

File tree

4 files changed

+9
-9
lines changed

4 files changed

+9
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
--name vaultwarden-backup \
3636
-v /path/to/vaultwarden/data:/data \
3737
-v /path/to/backups:/backups \
38-
-e PASSWORD="your-strong-password" \
38+
-e PASSWORD=your-password \
3939
-e RETENTION_DAYS=30 \
4040
--restart unless-stopped \
4141
ghcr.io/xg4/vaultwarden-backup:latest

internal/app/backup.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@ import (
1717

1818
// App 封装了备份应用的状态和依赖
1919
type App struct {
20-
cfg *config.Config
20+
cfg *config.Config
21+
Timestamp string
2122
}
2223

2324
// New 创建一个新的 App 实例
24-
func New(cfg *config.Config) *App {
25-
return &App{cfg: cfg}
25+
func New(cfg *config.Config, startTime time.Time) *App {
26+
return &App{cfg: cfg, Timestamp: startTime.Format("20060102_150405")}
2627
}
2728

2829
// Run 执行完整的备份和清理流程
2930
func (a *App) Run() error {
30-
slog.Info("开始备份流程", "timestamp", a.cfg.Timestamp)
31+
slog.Info("开始备份流程", "timestamp", a.Timestamp)
3132

3233
// 1. 运行前检查
3334
if err := utils.ValidateDirectories(a.cfg); err != nil {
@@ -218,7 +219,7 @@ func (a *App) createArchive() error {
218219
return fmt.Errorf("备份目录为空")
219220
}
220221

221-
archiveFile := filepath.Join(a.cfg.BackupDir, fmt.Sprintf("%s_%s.tar.gz", a.cfg.Filename, a.cfg.Timestamp))
222+
archiveFile := filepath.Join(a.cfg.BackupDir, fmt.Sprintf("%s_%s.tar.gz", a.cfg.Filename, a.Timestamp))
222223
slog.Info("创建加密压缩包", "file", archiveFile)
223224

224225
if err := archive.EncryptedBackup(a.cfg.BackupTmpDir, a.cfg.Password, archiveFile); err != nil {
@@ -245,6 +246,7 @@ func (a *App) cleanupOldBackups() error {
245246
if err != nil {
246247
return fmt.Errorf("查找旧备份失败: %w", err)
247248
}
249+
slog.Info("找到备份文件", "count", len(files), "pattern", globPattern)
248250

249251
cutoffTime := time.Now().AddDate(0, 0, -a.cfg.RetentionDays)
250252
count := 0

internal/config/config.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ type Config struct {
1515
BackupTmpDir string
1616
DataDir string
1717
Filename string
18-
Timestamp string
1918
RetentionDays int
2019
Password string
2120
MaxConcurrency int
@@ -58,7 +57,6 @@ func Load() (*Config, error) {
5857
BackupTmpDir: backupTmpDir,
5958
DataDir: getEnv("DATA_DIR", "/data"),
6059
Filename: getEnv("FILENAME", "vault"),
61-
Timestamp: time.Now().Format("20060102_150405"),
6260
RetentionDays: retentionDays,
6361
Password: password,
6462
MaxConcurrency: maxConcurrency,

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func bootstrap(cfg *config.Config) {
1616
slog.Info("==================== 备份开始 ====================")
1717

1818
// 创建并运行备份应用
19-
backupApp := app.New(cfg)
19+
backupApp := app.New(cfg, startTime)
2020
if err := backupApp.Run(); err != nil {
2121
slog.Error(fmt.Sprintf("备份过程中发生错误: %v", err))
2222
slog.Error("==================== 备份失败 ====================")

0 commit comments

Comments
 (0)