Skip to content

Instantly share code, notes, and snippets.

@jdp
Created August 22, 2013 22:24
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jdp/6313561 to your computer and use it in GitHub Desktop.
Save jdp/6313561 to your computer and use it in GitHub Desktop.
Move keys matching pattern from one database to another
#!/bin/sh
#
# Usage: ./redis-movekeys.sh [-h host] [-p port] [-n src] [-m dest] pattern
#
# Move keys matching pattern from the src Redis database to the
# dest Redis database.
set -e
HOST="localhost"
PORT="6379"
SRCDB="0"
DESTDB="0"
while getopts "h:p:n:m:" opt; do
case $opt in
h) HOST=$OPTARG;;
p) PORT=$OPTARG;;
n) SRCDB=$OPTARG;;
m) DESTDB=$OPTARG;;
\?) echo "invalid option: -$OPTARG" >&2; exit 1;;
esac
done
shift $(( $OPTIND -1 ))
PATTERN="$@"
if [ -z "$PATTERN" ]; then
echo "pattern required" >&2
exit 2
fi
redis-cli -h "$HOST" -p "$PORT" -n "$SRCDB" --raw keys "$PATTERN" |
xargs -I{} redis-cli -h "$HOST" -p "$PORT" -n "$SRCDB" move {} "$DESTDB"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment