mirror of
https://github.com/usememos/memos.git
synced 2024-12-19 00:51:30 +03:00
parent
52fdf8bccd
commit
6814915c88
@ -52,10 +52,13 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store
|
|||||||
Profile: profile,
|
Profile: profile,
|
||||||
|
|
||||||
// Asynchronous runners.
|
// Asynchronous runners.
|
||||||
backupRunner: backup.NewBackupRunner(store),
|
|
||||||
telegramBot: telegram.NewBotWithHandler(integration.NewTelegramHandler(store)),
|
telegramBot: telegram.NewBotWithHandler(integration.NewTelegramHandler(store)),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if profile.Driver == "sqlite" {
|
||||||
|
s.backupRunner = backup.NewBackupRunner(store)
|
||||||
|
}
|
||||||
|
|
||||||
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
|
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
|
||||||
Format: `{"time":"${time_rfc3339}","latency":"${latency_human}",` +
|
Format: `{"time":"${time_rfc3339}","latency":"${latency_human}",` +
|
||||||
`"method":"${method}","uri":"${uri}",` +
|
`"method":"${method}","uri":"${uri}",` +
|
||||||
@ -112,7 +115,10 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store
|
|||||||
func (s *Server) Start(ctx context.Context) error {
|
func (s *Server) Start(ctx context.Context) error {
|
||||||
go versionchecker.NewVersionChecker(s.Store, s.Profile).Start(ctx)
|
go versionchecker.NewVersionChecker(s.Store, s.Profile).Start(ctx)
|
||||||
go s.telegramBot.Start(ctx)
|
go s.telegramBot.Start(ctx)
|
||||||
|
|
||||||
|
if s.backupRunner != nil {
|
||||||
go s.backupRunner.Run(ctx)
|
go s.backupRunner.Run(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
metric.Enqueue("server start")
|
metric.Enqueue("server start")
|
||||||
return s.e.Start(fmt.Sprintf("%s:%d", s.Profile.Addr, s.Profile.Port))
|
return s.e.Start(fmt.Sprintf("%s:%d", s.Profile.Addr, s.Profile.Port))
|
||||||
|
@ -3,6 +3,7 @@ package backup
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -24,6 +25,8 @@ func NewBackupRunner(store *store.Store) *BackupRunner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const MaxBackupFiles = 5
|
||||||
|
|
||||||
func (r *BackupRunner) Run(ctx context.Context) {
|
func (r *BackupRunner) Run(ctx context.Context) {
|
||||||
intervalStr := r.Store.GetSystemSettingValueWithDefault(ctx, apiv1.SystemSettingAutoBackupIntervalName.String(), "")
|
intervalStr := r.Store.GetSystemSettingValueWithDefault(ctx, apiv1.SystemSettingAutoBackupIntervalName.String(), "")
|
||||||
if intervalStr == "" {
|
if intervalStr == "" {
|
||||||
@ -46,20 +49,46 @@ func (r *BackupRunner) Run(ctx context.Context) {
|
|||||||
ticker := time.NewTicker(time.Duration(interval) * time.Second)
|
ticker := time.NewTicker(time.Duration(interval) * time.Second)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
var t time.Time
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
log.Info("stop auto backup graceful.")
|
log.Info("stop auto backup graceful.")
|
||||||
return
|
return
|
||||||
case t = <-ticker.C:
|
case <-ticker.C:
|
||||||
|
}
|
||||||
|
|
||||||
|
filename := r.Store.Profile.DSN + ".bak"
|
||||||
|
|
||||||
|
if err := rotateFiles(filename, MaxBackupFiles); err != nil {
|
||||||
|
log.Error("fail to rotate backup files", zap.Error(err))
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
filename := r.Store.Profile.DSN + t.Format("-20060102-150405.bak")
|
|
||||||
log.Info(fmt.Sprintf("create backup to %s", filename))
|
log.Info(fmt.Sprintf("create backup to %s", filename))
|
||||||
err := r.Store.BackupTo(ctx, filename)
|
if err := r.Store.BackupTo(ctx, filename); err != nil {
|
||||||
if err != nil {
|
|
||||||
log.Error("fail to create backup", zap.Error(err))
|
log.Error("fail to create backup", zap.Error(err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func rotateFiles(filename string, cnt int) error {
|
||||||
|
// Generate suffix slices of history files like "",".1",".2",".3"...
|
||||||
|
ss := make([]string, cnt-1)
|
||||||
|
for i := 1; i < len(ss); i++ {
|
||||||
|
ss[i] = fmt.Sprintf(".%d", i)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iterate through the suffix slices and rename the files
|
||||||
|
for i := len(ss) - 1; i >= 0; i-- {
|
||||||
|
from := filename + ss[i]
|
||||||
|
to := filename + "." + strconv.Itoa(i+1)
|
||||||
|
|
||||||
|
log.Info("rotate file", zap.String("from", from), zap.String("to", to))
|
||||||
|
err := os.Rename(from, to)
|
||||||
|
if err != nil && !os.IsNotExist(err) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user