Skip to content

Instantly share code, notes, and snippets.

@2743d2
Forked from ZwodahS/copy_redis_key.sh
Last active September 13, 2023 03:19
Show Gist options
  • Save 2743d2/989b2cbb36ce9a163a2ad79cb9b87b64 to your computer and use it in GitHub Desktop.
Save 2743d2/989b2cbb36ce9a163a2ad79cb9b87b64 to your computer and use it in GitHub Desktop.
copy a redis db key to another place (use MIGRATE COPY for v3.0<= redis)
#!/bin/bash
# source http://stackoverflow.com/questions/23222616/copy-all-keys-from-one-db-to-another-in-redis
#set connection data accordingly
source_host=localhost
source_port=6379
source_db=1
target_host=localhost
target_port=6379
target_db=2
#copy all keys without preserving ttl!
redis-cli -h $source_host -p $source_port -n $source_db keys \* | while read key;
do
echo "Copying $key"
redis-cli --raw -h $source_host -p $source_port -n $source_db DUMP "$key" | perl -pe 'chomp if eof' | redis-cli -x -h $target_host -p $target_port -n $target_db RESTORE "$key" 0
done
@2743d2
Copy link
Author

2743d2 commented Sep 13, 2023

Relace head -c -1 to
perl -pe 'chomp if eof'
which not work for mac with GNU head

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment