Skip to content

Instantly share code, notes, and snippets.

@ZwodahS
Created March 1, 2016 06:07
Show Gist options
  • Select an option

  • Save ZwodahS/2263e351b8c29181c642 to your computer and use it in GitHub Desktop.

Select an option

Save ZwodahS/2263e351b8c29181c642 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" | head -c -1 | redis-cli -x -h $target_host -p $target_port -n $target_db RESTORE "$key" 0
done
@2743d2
Copy link
Copy Markdown

2743d2 commented Jul 30, 2021

If you get the below error over Mac, try install gnu head and change head to ghead in the above script.

head: illegal byte count -- -1

@yogeshsr It's better to replace head -c -1 with perl for Mac
perl -pe 'chomp if eof'

@DavidGoodwin
Copy link
Copy Markdown

unfortunately doesn't copy any ttl values across; so your new keys may last forever?

@2743d2
Copy link
Copy Markdown

2743d2 commented Jul 26, 2022

Sorry i'm not using ttl keys for now

@jgoldman83
Copy link
Copy Markdown

jgoldman83 commented Aug 25, 2023

`#!/bin/bash

source_host=10.128.0.157
source_port=6393
target_host=prod-experienceservice-elasticache.l15s7f.ng.0001.usw2.cache.amazonaws.com
target_port=6379

function copy_key {
key="$1"
echo "Copying $key"
redis-cli --raw -h $source_host -p $source_port DUMP "$key" | head -c-1 | redis-cli -x -h $target_host -p $target_port RESTORE "$key" 0
if [ $? -ne 0 ]; then
echo "Error copying key $key"
# Handle error as needed
fi
}

export -f copy_key
export source_host
export source_port
export target_host
export target_port

redis-cli -h $source_host -p $source_port keys '*' | xargs -I {} -P 16 bash -c 'copy_key "$@"' _ {}`

This works much better and distributes the load to 16 threads

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