-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #291 from xuewenG/backup-database
自动备份数据库
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path" | ||
"time" | ||
|
||
"github.com/glebarez/sqlite" | ||
fs_util "github.com/kingwrcy/moments/util" | ||
"github.com/kingwrcy/moments/vo" | ||
"github.com/rs/zerolog" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func backupDatabase(log zerolog.Logger, cfg *vo.AppConfig) { | ||
if !fs_util.Exists(cfg.DB) { | ||
log.Debug().Msgf("原数据库不存在, 所以无需备份数据库") | ||
return | ||
} | ||
|
||
db, err := gorm.Open(sqlite.Open(cfg.DB)) | ||
if err != nil { | ||
log.Fatal().Msgf("打开原数据库出错, err: %v", err) | ||
return | ||
} | ||
|
||
var sysConfig string | ||
db.Raw("SELECT content FROM SysConfig").Scan(&sysConfig) | ||
|
||
var config map[string]interface{} | ||
err = json.Unmarshal([]byte(sysConfig), &config) | ||
if err != nil { | ||
log.Fatal().Msgf("反序列化原配置出错, err: %v", err) | ||
return | ||
} | ||
|
||
version, ok := config["version"].(string) | ||
if !ok { | ||
version = "unknown" | ||
} | ||
|
||
if version == cfg.Version { | ||
log.Debug().Msgf("当前版本和上次启动时的版本一致, 所以无需备份数据库, version: %s", version) | ||
return | ||
} | ||
|
||
log.Info().Msgf("开始备份数据库") | ||
|
||
sourceFile, err := os.Open(cfg.DB) | ||
if err != nil { | ||
log.Error().Msgf("打开原数据库文件出错, err: %v", err) | ||
return | ||
} | ||
defer sourceFile.Close() | ||
|
||
now := time.Now() | ||
datetime := now.Format("2006-01-02-15-04-05") | ||
destinationFileName := fmt.Sprintf("backup-%s-%s.sqlite3", datetime, version) | ||
|
||
destinationFile, err := os.Create(path.Join(path.Dir(cfg.DB), destinationFileName)) | ||
if err != nil { | ||
log.Error().Msgf("创建备份数据库文件失败, err: %v", err) | ||
return | ||
} | ||
defer destinationFile.Close() | ||
|
||
_, err = io.Copy(destinationFile, sourceFile) | ||
if err != nil { | ||
log.Error().Msgf("复制数据库文件失败, err: %v", err) | ||
return | ||
} | ||
|
||
log.Info().Msgf("数据库备份完成") | ||
|
||
config["version"] = cfg.Version | ||
content, err := json.Marshal(config) | ||
if err != nil { | ||
log.Error().Msgf("序列化新配置失败, err: %v", err) | ||
} | ||
|
||
db.Exec("UPDATE SysConfig SET content = ?", content) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters