Skip to content

Commit

Permalink
Only take a backup of the redis/keydb master (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 authored Nov 28, 2023
1 parent 4dc86ef commit 4bff2ef
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion cmd/internal/database/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path"
"strings"
"time"

"github.com/redis/go-redis/v9"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}

0 comments on commit 4bff2ef

Please sign in to comment.