diff --git a/mvc/controllers/backup.go b/mvc/controllers/backup.go index 2c56485..8418d26 100644 --- a/mvc/controllers/backup.go +++ b/mvc/controllers/backup.go @@ -14,7 +14,7 @@ func Backup(tmplname string, group *gin.RouterGroup, s *models.Site) { group.POST(tmplname, func(c *gin.Context) { go func() { - if err := s.Bot.Backup(config.Cfg.DBPath, s.LogEntries, s.Cache); err != nil { + if err := s.Bot.BackupAll(config.Cfg.DBPath, s.LogEntries, s.Cache); err != nil { log.Println("s.Bot.Backup() error: ", err) } }() diff --git a/mvc/models/context.go b/mvc/models/context.go index d556a61..63cfe14 100644 --- a/mvc/models/context.go +++ b/mvc/models/context.go @@ -42,7 +42,7 @@ func NewSiteCtx() *Site { // Запуск параллельных процессов func (s *Site) RunTickers() { go tools.Ticker(config.Cfg.TgTickerTime*time.Hour, func() { - if err := s.Bot.Backup(config.Cfg.DBPath, s.LogEntries, s.Cache); err != nil { + if err := s.Bot.BackupAll(config.Cfg.DBPath, s.LogEntries, s.Cache); err != nil { log.Println("s.Bot.Backup() error: ", err) } }) diff --git a/mvc/models/telegram.go b/mvc/models/telegram.go index 181eeef..65365c3 100644 --- a/mvc/models/telegram.go +++ b/mvc/models/telegram.go @@ -99,25 +99,17 @@ func (b *TGBot) SendFile(filePath, caption string) error { } // Telegram backup. Takes database path, query log, cache -func (b *TGBot) Backup(dbpath string, log []string, cache *candycache.Cache) error { - // Send db - if err := b.SendFile(dbpath, "📦 Бэкап базы данных"); err != nil { +func (b *TGBot) BackupAll(dbpath string, log []string, cache *candycache.Cache) error { + + if err := b.DatabaseBackup(dbpath); err != nil { return err } - // Create and send log file - if err := tools.StringSliceToFile(log, "log.txt"); err != nil { - return err - } - if err := b.SendFile("log.txt", "📃 Лог запросов за последний час"); err != nil { + if err := b.LogBackup(log); err != nil { return err } - // Create and send cache dump - if err := WriteCacheDumpToFile(cache, "cache.json"); err != nil { - return err - } - if err := b.SendFile("cache.json", "🗄 Дамп кэша"); err != nil { + if err := b.CacheBackup(cache); err != nil { return err } @@ -125,8 +117,34 @@ func (b *TGBot) Backup(dbpath string, log []string, cache *candycache.Cache) err for i := range log { log[i] = "" } - os.Remove("log.txt") - os.Remove("cache.json") return nil } + +func (b *TGBot) DatabaseBackup(dbpath string) error { + return b.SendFile(dbpath, "📦 Бэкап базы данных") +} + +func (b *TGBot) LogBackup(log []string) error { + // Create and send log file + if err := tools.StringSliceToFile(log, "log.txt"); err != nil { + return err + } + if err := b.SendFile("log.txt", "📃 Лог запросов за последний час"); err != nil { + return err + } + // Cleanup log file after sending + return os.Remove("log.txt") +} + +func (b *TGBot) CacheBackup(cache *candycache.Cache) error { + // Create and send cache dump + if err := WriteCacheDumpToFile(cache, "cache.json"); err != nil { + return err + } + if err := b.SendFile("cache.json", "🗄 Дамп кэша"); err != nil { + return err + } + // Cleanup cache file after sending + return os.Remove("cache.json") +}