redis-rdb-cli

repository·master·Indexed 19 days ago

https://github.com/leonchen83/redis-rdb-cli

A comprehensive toolset for managing Redis RDB files and instances. It provides capabilities for parsing, filtering, splitting, and merging RDB files, as well as analyzing memory usage and key counts. The suite includes tools for one-time data migration (rmt), continuous synchronization (rst), remote RDB backup (rdt), and RDB/AOF extraction (rcut). It supports Redis Clusters, SSL, ACL, and provides a monitoring integration with Grafana via rmonitor.

Tokens
32.6K
Snippets
87
Records
126
Agent score
66%

What's inside redis-rdb-cli

  1. Sync Redis instances with `rst` and `rmt`

    master

    The tool provides two primary ways to move data between Redis instances.

    rst (Continuous Sync)

    rst migrates an initial RDB snapshot and then continuously syncs incremental data changes. It runs until manually stopped.

    • Use case: Real-time synchronization or keeping a replica updated.
    • Limitation: Only supports filtering by db index.
    # Sync two instances
    $ rst -s redis://127.0.0.1:6379 -m redis://127.0.0.1:6380 -r
    
    # Sync a single instance to a Redis Cluster
    $ rst -s redis://127.0.0.1:6379 -m redis://127.0.0.1:30001 -r -d 0

    rmt (One-time Migration)

    rmt performs a BGSAVE on the source to generate a snapshot, migrates that snapshot to the target, and then terminates.

    • Use case: One-time data migration.
    # Migrate RDB file to a remote Redis instance
    $ rmt -s /path/to/dump.rdb -m redis://192.168.1.105:6379 -r
    
    # Migrate RDB to a remote Redis Cluster using nodes.conf
    $ rmt -s /path/to/dump.rdb -c ./nodes-30001.conf -r
    $ rst -s redis://127.0.0.1:6379 -m redis://127.0.0.1:6380 -r
  2. Sync data between Redis instances with rst and rmt

    master

    There are two primary commands for data synchronization:

    rmt (Migration)

    Used for one-time migrations. It triggers a BGSAVE on the source Redis, migrates the resulting RDB snapshot to the target, and then terminates.

    • Sync RDB to remote Redis: $ rmt -s <rdb_path> -m <redis_uri> -r
    • Sync RDB to a Redis Cluster: $ rmt -s <rdb_path> -c <nodes_conf_path> -r

    rst (Continuous Sync)

    Used for continuous synchronization. It migrates the RDB snapshot and then continues to migrate incremental data. It does not terminate automatically; use CTRL+C to stop.

    • Sync between two Redis instances: $ rst -s <source_uri> -m <target_uri> -r
    • Sync single Redis to a Cluster: $ rst -s <source_uri> -m <target_uri> -r -d <db_index>

    Note: rst only supports filtering by db (-d).

    # Sync data between two Redis instances
    $ rst -s redis://127.0.0.1:6379 -m redis://127.0.0.1:6380 -r
    
    # Migrate an RDB file to a remote Redis instance
    $ rmt -s /path/to/dump.rdb -m redis://192.168.1.105:6379 -r
  3. Understand limitations of `rst` cluster migration

    master

    When migrating data to a Redis Cluster using rst, be aware of the following constraints:

    Cluster Stability Requirement

    The tool uses the cluster's nodes.conf file and does not handle MOVED or ASK redirections. The cluster MUST be in a stable state:

    • No slots should be in a migrating or importing state.
    • No failovers (replica promotion) should occur during the process.

    Unsupported Commands

    The following commands are not supported during cluster migration:

    • PUBLISH, SWAPDB, MOVE, FLUSHALL, FLUSHDB, MULTI, EXEC, SCRIPT FLUSH, SCRIPT LOAD, EVAL, EVALSHA.

    Slot-Specific Command Constraints

    The following commands are ONLY supported if all keys in the command belong to the same hash slot (e.g., using hash tags like del {user}:1 {user}:2):

    • RPOPLPUSH, SDIFFSTORE, SINTERSTORE, SMOVE, ZINTERSTORE, ZUNIONSTORE, DEL, UNLINK, RENAME, RENAMENX, PFMERGE, PFCOUNT, MSETNX, BRPOPLPUSH, BITOP, MSET, COPY, BLMOVE, LMOVE, ZDIFFSTORE, GEOSEARCHSTORE, MSETEX.
  4. Configure Redis 6 SSL and ACL

    master

    Redis 6 SSL

    To use SSL/TLS, use the rediss:// URI scheme. If the source and target use the same keystore, configure the following in your config:

    • source_keystore_path / target_keystore_path
    • source_keystore_pass / target_keystore_pass

    Redis 6 ACL

    To use ACLs, include credentials in the URI: redis://user:pass@host:port

    Requirement: The user must have +@all permissions to handle synchronization commands.

    # Use SSL with rst
    $ rst -s rediss://127.0.0.1:6379 -m rediss://127.0.0.1:30001 -r -d 0
    
    # Use ACL with rst
    $ rst -s redis://user:pass@127.0.0.1:6379 -m redis://user:pass@127.0.0.1:6380 -r -d 0
  5. Understand limitations when migrating to a Redis Cluster

    master

    When using the rst command to migrate data to a Redis Cluster via a nodes.conf file, the following constraints apply:

    1. Cluster Stability: The cluster must be in a stable state. It must not have slots in migrating or importing states, and no master/replica failovers should occur during synchronization.
    2. Command Support:
      • Unsupported Commands: PUBLISH, SWAPDB, MOVE, FLUSHALL, FLUSHDB, MULTI, EXEC, SCRIPT FLUSH, SCRIPT LOAD, EVAL, EVALSHA.
      • Limited Support: Commands like RPOPLPUSH, SDIFFSTORE, SINTERSTORE, SMOVE, ZINTERSTORE, ZUNIONSTORE, DEL, UNLINK, RENAME, RENAMENX, PFMERGE, PFCOUNT, MSETNX, BRPOPLPUSH, BITOP, MSET, COPY, BLMOVE, LMOVE, ZDIFFSTORE, GEOSEARCHSTORE, MSETEX are only supported if the keys involved reside in the same slot (e.g., del {user}:1 {user}:2).
  6. Analyze RDB files (Count keys and Memory usage)

    master

    Use rct to perform offline analysis on RDB files.

    Count Keys

    Outputs the key count to a CSV file.

    $ rct -f count -s /path/to/dump.rdb -o /path/to/dump.csv

    Find Top 50 Largest Keys

    Analyzes memory usage and finds the largest keys.

    $ rct -f mem -s /path/to/dump.rdb -o /path/to/dump.mem -l 50
  7. Monitor Redis with rmonitor and Grafana Dashboard

    master

    You can monitor Redis servers (Standalone, Cluster, or Sentinel) using rmonitor and a Grafana dashboard.

    Setup Steps:

    1. Configure Metric Gateway: In redis-rdb-cli.conf, change metric_gateway from none to influxdb.
    2. Start Dashboard: Navigate to the dashboard directory and run docker-compose up -d.
    3. Run Monitor:
      • Standalone: rmonitor -s <uri> -n standalone
      • Cluster: rmonitor -s <uri> -n cluster
      • Sentinel: rmonitor -s <sentinel_uri> -n sentinel
    4. View Results: Open http://localhost:3000/d/monitor/monitor in your browser and log in with admin/admin.

    Note: Since version v0.1.9, rct -f mem results can also be displayed in Grafana.

    # Start the monitoring dashboard
    $ cd /path/to/redis-rdb-cli/dashboard
    $ docker-compose up -d
    
    # Monitor a standalone Redis instance
    $ rmonitor -s redis://127.0.0.1:6379 -n standalone
  8. Install redis-rdb-cli

    master

    You can install redis-rdb-cli via binary download, Homebrew (MacOS), or Docker.

    Binary Installation

    1. Download the latest release zip.
    2. Unzip the file.
    3. Run the binary using ./redis-rdb-cli/bin/rct.

    MacOS Homebrew

    Use the custom tap provided by the author:

    brew tap leonchen83/redis-rdb-cli
    brew install redis-rdb-cli

    Docker

    Run the tool directly in a container:

    docker run -it --rm redisrdbcli/redis-rdb-cli:latest
    # MacOS homebrew installation
    $ brew tap leonchen83/redis-rdb-cli
    $ brew install redis-rdb-cli
    $ rct -h
  9. Implement a custom FormatterService

    master

    You can create custom formatters to change how Redis data is outputted using the rct command.

    Implementation Steps

    1. Extend AbstractFormatterService: Implement the format() method and override specific event methods like applyString to define how different data types are processed.
    2. Register via SPI: Create a file named com.moilioncircle.redis.rdb.cli.api.format.FormatterService in src/main/resources/META-INF/services/ containing your class name.
    3. Deploy: Build the JAR with dependencies and copy it to the redis-rdb-cli/lib directory.
    4. Run: Use the rct command.

    Example Usage

    $ rct -f test -s redis://127.0.0.1:6379 -o ./out.csv -t string -d 0 -e json
    public class YourFormatterService extends AbstractFormatterService {
        @Override
        public String format() {
            return "test";
        }
    
        @Override
        public Event applyString(Replicator replicator, RedisInputStream in, int version, byte[] key, int type, ContextKeyValuePair context) throws IOException {
            byte[] val = new DefaultRdbValueVisitor(replicator).applyString(in, version);
            getEscaper().encode(key, getOutputStream());
            getEscaper().encode(val, getOutputStream());
            getOutputStream().write('\n');
            return context;
        }
    }
  10. Configure Redis 6 with SSL and ACL

    master

    Redis 6 with SSL

    1. Generate a PKCS12 keystore using OpenSSL from your Redis certs.
    2. In redis-rdb-cli.conf, set:
      • source_keystore_path and target_keystore_path to your .p12 file.
      • source_keystore_pass and target_keystore_pass.
    3. Use the rediss:// protocol in commands.
      $ rst -s rediss://127.0.0.1:6379 -m rediss://127.0.0.1:30001 -r -d 0

    Redis 6 with ACL

    Use the URI format to include credentials:

    $ rst -s redis://user:pass@127.0.0.1:6379 -m redis://user:pass@127.0.0.1:6380 -r -d 0

    Note: The user must have +@all permissions.

  11. Convert RDB files to other formats

    master

    Use the rct command to transform RDB files into different formats.

    Convert RDB to AOF (Dump format)

    Useful for mass insertion into Redis.

    $ rct -f dump -s /path/to/dump.rdb -o /path/to/dump.aof -r
    # Then pipe to redis-cli:
    $ cat /path/to/dump.aof | /redis/src/redis-cli -p 6379 --pipe

    Convert RDB to JSON

    $ rct -f json -s /path/to/dump.rdb -o /path/to/dump.json

    Convert RDB to RESP

    $ rct -f resp -s /path/to/dump.rdb -o /path/to/appendonly.aof
    $ rct -f dump -s /path/to/dump.rdb -o /path/to/dump.aof
  12. Migrate RDB to remote Redis Cluster

    master

    You can migrate an RDB file to a Redis Cluster in two ways:

    1. Using nodes.conf: Provide the cluster configuration file.
      $ rmt -s /path/to/dump.rdb -c ./nodes-30001.conf -r
    2. Direct Node Connection: Connect directly to one of the cluster nodes.
      $ rmt -s /path/to/dump.rdb -m redis://127.0.0.1:30001 -r
    $ rmt -s /path/to/dump.rdb -c ./nodes-30001.conf -r