Redis Database Dump Tool
The `redis-dump` package provides a utility for exporting data from a Redis database, offering output in two primary formats: a series of Redis commands suitable for direct re-import via `redis-cli`, or a structured JSON representation for programmatic processing. The current stable version, `0.1.10`, was last published in March 2014, indicating that the project is no longer actively maintained. Its core functionality enables users to backup, migrate, or inspect Redis data, differentiating itself by supporting both a command-line interface and a programmatic API for Node.js applications. Due to its age, it predates modern JavaScript module systems and may not be compatible with newer Redis features or Node.js versions, and has an effectively non-existent release cadence.
Common errors
-
"redis-dump" is not recognized as an internal or external command, operable program or batch file.
cause The `redis-dump` CLI tool is not found in your system's PATH. This typically occurs if the package was installed locally (`npm install redis-dump`) but not globally (`npm install -g redis-dump`), or if `node_modules/.bin` is not in your PATH.fixInstall the package globally using `npm install -g redis-dump`. Alternatively, execute it using `npx redis-dump [OPTIONS]` or by referencing its local binary path: `$(npm bin)/redis-dump [OPTIONS]`. -
Error: connect ECONNREFUSED 127.0.0.1:6379
cause The `redis-dump` tool failed to establish a connection to the Redis server. This usually means the Redis server is not running, is configured to listen on a different host or port, or is blocked by a firewall.fixEnsure your Redis server is running and accessible from the machine where `redis-dump` is executed. Verify the host, port, and password by explicitly passing them to `redis-dump` using the `-h`, `-p`, and `-a` arguments. -
SyntaxError: Unexpected token 'export' or 'import' in redis-dump.js
cause You are attempting to use `redis-dump`, which is a CommonJS module from 2014, with ES module `import` syntax in a modern Node.js environment or build setup that does not correctly transpile or handle CommonJS imports.fixUse CommonJS `require()` syntax to load the module: `const program = require('redis-dump');`. Direct programmatic use of its dumping functionality is limited, as the module exports the Commander.js program object rather than a dump function.
Warnings
- breaking The `redis-dump` package is effectively abandoned, with its last publication dating back to March 2014. It is unlikely to be compatible with newer Node.js versions, modern Redis client libraries, or recent Redis server features and data structures, potentially leading to instability or data loss.
- gotcha Despite its description, programmatic usage of `redis-dump` in Node.js is primarily achieved by spawning its command-line interface as a child process (e.g., using `child_process.spawn`). The `require('redis-dump')` statement directly exposes the Commander.js CLI parser object, not a direct function for dumping Redis data.
- gotcha The package defaults to connecting to a Redis server on `127.0.0.1:6379` without authentication. If your Redis instance is configured differently, requires a password, or is not running on the default host/port, you must explicitly provide the correct hostname, port, and authentication parameters via the command line arguments (`-h`, `-p`, `-a`).
- gotcha When generating Redis commands, the output includes `DEL` commands before `RPUSH`, `SADD`, `ZADD`, `HMSET`, etc. This behavior ensures a clean slate upon re-import but will unconditionally delete and then recreate keys with matching names, potentially overwriting existing data if not handled carefully.
Install
-
npm install redis-dump -
yarn add redis-dump -
pnpm add redis-dump
Imports
- program
import program from 'redis-dump';
const program = require('redis-dump');
Quickstart
const redis = require('redis');
const { spawn } = require('child_process');
async function runDump() {
const client = redis.createClient({
url: process.env.REDIS_URL || `redis://${process.env.REDIS_HOST || '127.0.0.1'}:${process.env.REDIS_PORT || '6379'}`,
password: process.env.REDIS_PASSWORD || ''
});
client.on('error', (err) => {
console.error('Redis Client Error', err);
});
try {
await client.connect();
// Populate some dummy data for demonstration
await client.del('mydb:numberlist', 'mydb:numberset', 'mydb:numvisits');
await client.rPush('mydb:numberlist', 'one', 'two', 'three');
await client.sAdd('mydb:numberset', 'one', 'two', 'three');
await client.set('mydb:numvisits', '34');
console.log('Dummy data populated in Redis.');
// Define common connection arguments for redis-dump CLI
const commonDumpArgs = [
'-h', process.env.REDIS_HOST || '127.0.0.1',
'-p', process.env.REDIS_PORT || '6379',
'-a', process.env.REDIS_PASSWORD || '',
'-f', 'mydb:*'
];
// Dump as JSON
console.log('\n--- Dumping as JSON ---');
const jsonDumpProcess = spawn('redis-dump', [...commonDumpArgs, '--json', '--pretty']);
let jsonOutput = '';
jsonDumpProcess.stdout.on('data', (data) => { jsonOutput += data.toString(); });
jsonDumpProcess.stderr.on('data', (data) => { console.error(`stderr (JSON dump): ${data}`); });
await new Promise((resolve, reject) => {
jsonDumpProcess.on('close', (code) => {
if (code === 0) { console.log('JSON Dump Output:\n', jsonOutput); resolve(); }
else { reject(new Error(`redis-dump JSON process exited with code ${code}`)); }
});
});
// Dump as Redis commands
console.log('\n--- Dumping as Redis Commands ---');
const commandDumpProcess = spawn('redis-dump', commonDumpArgs);
let commandOutput = '';
commandDumpProcess.stdout.on('data', (data) => { commandOutput += data.toString(); });
commandDumpProcess.stderr.on('data', (data) => { console.error(`stderr (Command dump): ${data}`); });
await new Promise((resolve, reject) => {
commandDumpProcess.on('close', (code) => {
if (code === 0) { console.log('Command Dump Output:\n', commandOutput); resolve(); }
else { reject(new Error(`redis-dump Command process exited with code ${code}`)); }
});
});
} catch (error) {
console.error('An error occurred:', error);
} finally {
await client.disconnect();
console.log('Redis client disconnected.');
}
}
runDump();