Skip to content

Instantly share code, notes, and snippets.

@marz619
Last active January 7, 2023 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marz619/b076cd2a4f1d905bcceb to your computer and use it in GitHub Desktop.
Save marz619/b076cd2a4f1d905bcceb to your computer and use it in GitHub Desktop.
Rename Redis Keys
#!/bin/bash
# args
host=$1
shift
prefix=$1
shift
# from=$1
# shift
# to=$1
# shift
echo "using host: $host"
# echo "replacing '$from' --> '$to'"
keys=( $(redis-cli -h $host KEYS "$prefix*") )
# echo $keys
for key in "${keys[@]}"
do
renamed=`echo $key | sed -e "s/^"\(.*\)"$/\1/' -e 's/$from/$to/g"`
echo $key $renamed
redis-cli -h $host RENAME $key $renamed
done
@VictorVolpe
Copy link

Working code

#!/bin/bash

prefix=$1
replace=$2
redis='redis-cli -s /var/run/redis/redis.sock'

keys=( $($redis KEYS "$prefix*") )

for key in "${keys[@]}"
do
    renamed=`echo $key | sed -e "s/$prefix/$replace/"`
    echo $key $renamed
    $redis RENAME $key $renamed
done

Example: bash redis_rename_keys.sh SANDBOX PRODUCTION

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