Created
December 15, 2015 23:23
-
-
Save tinogomes/813362924b2299b023fb to your computer and use it in GitHub Desktop.
Shell script to copy redis keys between redis servers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
function usage() { | |
echo "Usage: $0 <source_redis> <destination_redis> <key_pattern>" | |
echo " <source_redis> - Source server hostname" | |
echo " <destination_redis> - Destination server nostname" | |
echo " <key_pattern> - Keys to copy (Like Redis KEYS pattern)" | |
exit 0 | |
} | |
function main() { | |
redis_cmd="redis-cli -h $source_redis" | |
redis_keys=$($redis_cmd keys $keys | cut -d ' ' -f2) | |
for key in $redis_keys; do | |
echo "------------------" | |
echo "Reading from $source_redis Redis: $key" | |
doc=$($redis_cmd get $key) | |
echo $doc | |
echo "Writing at $destination_redis Redis" | |
redis-cli -h $destination_redis set $key '$doc' || exit 1 | |
done | |
} | |
if [ "$#" -ne 3 ]; then | |
echo '*** Wrong number of arguments ***\n' | |
usage | |
fi | |
if [ "$1" == "-h" ] || [ "$1" == "--help" ] | |
then | |
usage | |
fi | |
source_redis=$1 | |
destination_redis=$2 | |
keys=$3 | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment