Skip to content

Instantly share code, notes, and snippets.

@rtitle
Last active August 29, 2015 14:10
Show Gist options
  • Save rtitle/fedc5f403ac5718767b2 to your computer and use it in GitHub Desktop.
Save rtitle/fedc5f403ac5718767b2 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Dumps memcached data from one server and restores on another.
# Useful for copying production data to staging.
#
# Generates a ./user_ids file with the users that were copied and an ./error file for errors.
# The user_ids file can be plugged into the JMeter RTS load generator.
#
# Takes a maximum number of entries to copy. If unspecified it copies everything.
#
# Usage:
# ./memcached_dump source dest [max]
# ./memcached_dump.sh "rts-cache01.sliad.dataxu.net:1760" "test-rts-cache01.us01.dataxu.net:1760" 1000
SOURCE=$1
DEST=$2
MAX=$3
if [[ -z $SOURCE || -z $DEST ]]; then
echo 'Usage: ./memcached_dump [source] [dest]'
echo 'Example: ./memcached_dump.sh "rts-cache01.sliad.dataxu.net:1760" "test-rts-cache01.us01.dataxu.net:1760" 1000'
exit 1
fi
echo "Dumping ${SOURCE} to ${DEST}"
if [[ -n $MAX ]]; then
echo "Copying at most ${MAX} entries"
else
echo "Copying all entries!"
fi
DATA_DIR=/tmp/memcached_dump
rm -rf ${DATA_DIR}
mkdir -p ${DATA_DIR}
echo "Dumping..."
export MEMCACHED_SERVERS=${SOURCE}
if [[ -n $MAX ]]; then
dump_cmd="memcdump | head -${MAX}"
else
dump_cmd="memcdump"
fi
eval "${dump_cmd}" | xargs -n1 -i bash -c "memccat {} --file ${DATA_DIR}/{} && echo {}" >./user_ids 2>error
echo "`du -sh ${DATA_DIR}`"
echo "Restoring..."
export MEMCACHED_SERVERS=${DEST}
find ${DATA_DIR} -type f -exec memccp {} \;
echo "Done!"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment