Skip to content

Commit

Permalink
Merge pull request #292 from xuewenG/backup-commit-id
Browse files Browse the repository at this point in the history
检查是否备份数据库文件增加 commitId 条件
  • Loading branch information
xuewenG authored Feb 20, 2025
2 parents 95f0ee4 + 5e802cb commit b406744
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions backend/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ func backupDatabase(log zerolog.Logger, cfg *vo.AppConfig) {
version = "unknown"
}

if version == cfg.Version {
log.Debug().Msgf("当前版本和上次启动时的版本一致, 所以无需备份数据库, version: %s", version)
commitId, ok = config["commitId"].(string)
if !ok {
commitId = "unknown"
}

if version == cfg.Version && commitId == cfg.CommitId {
log.Debug().Msgf("当前版本和上次启动时的版本一致, 所以无需备份数据库, version: %s, commitId: %s", version, commitId)
return
}

Expand All @@ -58,7 +63,7 @@ func backupDatabase(log zerolog.Logger, cfg *vo.AppConfig) {

now := time.Now()
datetime := now.Format("2006-01-02-15-04-05")
destinationFileName := fmt.Sprintf("backup-%s-%s.sqlite3", datetime, version)
destinationFileName := fmt.Sprintf("backup-%s-%s-%s.sqlite3", datetime, version, commitId)

destinationFile, err := os.Create(path.Join(path.Dir(cfg.DB), destinationFileName))
if err != nil {
Expand All @@ -76,6 +81,7 @@ func backupDatabase(log zerolog.Logger, cfg *vo.AppConfig) {
log.Info().Msgf("数据库备份完成")

config["version"] = cfg.Version
config["commitId"] = cfg.CommitId
content, err := json.Marshal(config)
if err != nil {
log.Error().Msgf("序列化新配置失败, err: %v", err)
Expand Down

0 comments on commit b406744

Please sign in to comment.