Last active
February 8, 2023 08:33
-
-
Save gythialy/8db35ca73fbe59562c0203cfd7633ee5 to your computer and use it in GitHub Desktop.
Comfort tool for migrating redis keys among instances. Supports KEYS syntax matching, authentication and TTL
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/bash | |
###### | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED | |
# TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF | |
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
# DEALINGS IN THE SOFTWARE. | |
###### | |
###### | |
### Migrates the keys responding to the pattern specified on the command line, using DUMP/RESTORE, supports authentication differently from MIGRATE | |
###### | |
KEYS_MATCHER=$1 | |
SOURCE_HOST=redis | |
SOURCE_PASSWORD=NHiWmWTeVz36egfz0X8zd | |
SOURCE_PORT=6379 | |
SOURCE_SCHEMA=5 | |
TARGET_HOST=redis | |
TARGET_PASSWORD=NHiWmWTeVz36egfz0X8zd | |
TARGET_PORT=6379 | |
TARGET_SCHEMA=3 | |
LOG_FILE="redis-migrate.log" | |
if [[ -z "$KEYS_MATCHER" ]]; then | |
echo -e "Please provide a KEYS matcher, like *~cache" | |
exit 1 | |
fi | |
echo "*** Migrating keys matching $KEYS_MATCHER" | |
redis-cli --no-auth-warning -h $SOURCE_HOST -a $SOURCE_PASSWORD -p $SOURCE_PORT -n $SOURCE_SCHEMA keys "$KEYS_MATCHER" | while read key; do | |
# Preparing TTL | |
key_ttl=`redis-cli --no-auth-warning -h $SOURCE_HOST -a $SOURCE_PASSWORD -p $SOURCE_PORT ttl "$key"` | |
if [[ $key_ttl -lt 1 ]]; then | |
key_ttl=0 | |
else | |
# TTL must be in milliseconds when specifying it | |
key_ttl+=000 | |
fi | |
echo "Dump/Restore \"$key\", ttl $key_ttl" &>> $LOG_FILE | |
redis-cli --no-auth-warning --raw -h $SOURCE_HOST -p $SOURCE_PORT -n $SOURCE_SCHEMA -a $SOURCE_PASSWORD DUMP "$key" | head -c -1 | redis-cli -x --no-auth-warning -h $TARGET_HOST -p $TARGET_PORT -n $TARGET_SCHEMA -a $TARGET_PASSWORD RESTORE "$key" $key_ttl &>> $LOG_FILE | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment