diff --git a/cmd/internal/database/redis/redis.go b/cmd/internal/database/redis/redis.go index a7e8e6d..2d57b14 100644 --- a/cmd/internal/database/redis/redis.go +++ b/cmd/internal/database/redis/redis.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path" + "strings" "time" "github.com/redis/go-redis/v9" @@ -53,6 +54,15 @@ func New(log *zap.SugaredLogger, datadir string, addr string, password *string) // Backup takes a dump of redis with the redis client. func (db *Redis) Backup(ctx context.Context) error { + isMaster, err := db.isMaster(ctx) + if err != nil { + return err + } + if !isMaster { + db.log.Info("this database is not master, not taking a backup") + return nil + } + if err := os.RemoveAll(constants.BackupDir); err != nil { return fmt.Errorf("could not clean backup directory: %w", err) } @@ -62,7 +72,7 @@ func (db *Redis) Backup(ctx context.Context) error { } start := time.Now() - _, err := db.client.Save(ctx).Result() + _, err = db.client.Save(ctx).Result() if err != nil { return fmt.Errorf("could not create a dump: %w", err) } @@ -147,3 +157,15 @@ func (db *Redis) Recover(ctx context.Context) error { func (db *Redis) Upgrade(_ context.Context) error { return nil } + +func (db *Redis) isMaster(ctx context.Context) (bool, error) { + info, err := db.client.Info(ctx, "replication").Result() + if err != nil { + return false, fmt.Errorf("unable to get database info %w", err) + } + if strings.Contains(info, "role:master") { + db.log.Info("this is database master") + return true, nil + } + return false, nil +}