Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( NoAdminsDefined = errors.Business("No admins defined", "SY:001") NoRootssDefined = errors.Business("No roots defined", "SY:002") BackupIsEmpty = errors.Business("Backup is empty", "SY:003") )
View Source
var BackupCmd = &cli.Command{ Name: "backup", Usage: "do system backup", Flags: []cli.Flag{ &cli.BoolFlag{ Name: "notify", Usage: "send information to roots", Required: true, }, &cli.BoolFlag{ Name: "force", Usage: "force notify even if got error", }, }, Action: func(cmd *cli.Context) error { if !cmd.Bool("notify") { return ErrOnlyNotifyTrue } force := cmd.Bool("force") cfg := *config.Ctx(cmd.Context) roots := cfg.Telegram.Roots if len(roots) == 0 { return NoAdminsDefined } logger := zerolog.Ctx(cmd.Context). With(). Str("context", "system"). Logger() bot, err := telegram.NewBot(cfg) if err != nil { return err } notifyOnError := func(err error) { msg := fmt.Sprintf( "*%s*\n\n*System Notify:*\n`%s`\n\n*Backup Error:*\n`%s`\n\n*System time:*\n`%s`", support.AddSlashes(cmd.Args().First()), config.Hostname(), err.Error(), time.Now().Format(time.RFC3339), ) for _, id := range roots { _, _ = bot.Send(&telebot.User{ ID: id, }, msg, telebot.ModeMarkdownV2) } } db, err := database.Open(cfg.Store.Path) if err != nil { if force { notifyOnError(err) } return err } defer func() { _ = db.Close() }() logger.Debug().Msg("Generating backup...") file, destroy, err := system.Backup(cmd.Context, db) if err != nil { if force { notifyOnError(err) } return err } defer destroy() logger.Info().Msgf("Temp backup file created: %s", file.Name()) st, err := file.Stat() if err != nil { if force { notifyOnError(err) } return err } if st.Size() == 0 { notifyOnError(BackupIsEmpty) return BackupIsEmpty } caption := fmt.Sprintf( "*%s*\n\n*System Notify:*\n`%s`\n\n*Peristera Backup:*\n`%s`", support.AddSlashes(cmd.Args().First()), config.Hostname(), time.Now().Format(time.RFC3339), ) document := telegram.Document(file, caption) for _, id := range roots { document.Caption = caption _, err = bot.Send(&telebot.User{ ID: id, }, document, telebot.ModeMarkdownV2) if err != nil { logger.Warn().Err(err).Int64("userId", id).Msg("Fail to sent backup file") } } logger.Info().Msgf("System backup sent do telegram %v", roots) return nil }, }
View Source
var ErrOnlyNotifyTrue = errors.Business("only notify true is supported", "SY:003")
View Source
var InfoCmd = &cli.Command{ Name: "info", Usage: "load system info", Flags: []cli.Flag{ &cli.BoolFlag{ Name: "notify", Usage: "send information to admins", DefaultText: "false", }, }, Action: func(cmd *cli.Context) error { cfg := *config.Ctx(cmd.Context) data, err := system.New() if err != nil { return err } if cmd.Bool("notify") { admins := cfg.Telegram.Admins logger := zerolog.Ctx(cmd.Context). With(). Str("context", "system"). Logger() if len(admins) == 0 { return NoAdminsDefined } bot, err := telegram.NewBot(cfg) if err != nil { return err } msg := data.MarkdownV2(cmd.Args().First()) for _, id := range admins { _, err = bot.Send(&telebot.User{ ID: id, }, msg, telebot.ModeMarkdownV2) if err != nil { logger.Warn().Err(err).Int64("userId", id).Msg("Fail to sent info") } } logger.Info().Msgf("System info sent do telegram %v", admins) return nil } return data.Println() }, }
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.