Skip to content

Commit

Permalink
Update DB WAL before backup #1300
Browse files Browse the repository at this point in the history
  • Loading branch information
alireza0 committed Dec 8, 2023
1 parent c980a06 commit 6411fac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
9 changes: 9 additions & 0 deletions database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,12 @@ func IsSQLiteDB(file io.ReaderAt) (bool, error) {
}
return bytes.Equal(buf, signature), nil
}

func Checkpoint() error {
// Update WAL
err := db.Exec("PRAGMA wal_checkpoint;").Error
if err != nil {
return err
}
return nil
}
5 changes: 5 additions & 0 deletions web/service/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,11 @@ func (s *ServerService) GetConfigJson() (interface{}, error) {
}

func (s *ServerService) GetDb() ([]byte, error) {
// Update by manually trigger a checkpoint operation
err := database.Checkpoint()
if err != nil {
return nil, err
}
// Open the file for reading
file, err := os.Open(config.GetDBPath())
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions web/service/tgbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"time"
"x-ui/config"
"x-ui/database"
"x-ui/database/model"
"x-ui/logger"
"x-ui/util/common"
Expand Down Expand Up @@ -1417,6 +1418,12 @@ func (t *Tgbot) sendBackup(chatId int64) {
output := t.I18nBot("tgbot.messages.backupTime", "Time=="+time.Now().Format("2006-01-02 15:04:05"))
t.SendMsgToTgbot(chatId, output)

// Update by manually trigger a checkpoint operation
err := database.Checkpoint()
if err != nil {
logger.Warning("Error in trigger a checkpoint operation: ", err)
}

file, err := os.Open(config.GetDBPath())
if err != nil {
logger.Warning("Error in opening db file for backup: ", err)
Expand Down

0 comments on commit 6411fac

Please sign in to comment.