#!/bin/sh
DBFILE=./migratedata2redis.sql
for((nDBIndex=0;nDBIndex<4;nDBIndex++)) do
if [ -f $DBFILE ] then rm -rf $DBFILE fi for((nTBIndex=0;nTBIndex<64;nTBIndex++)) do ( cat <<EOF SELECT concat( '*4\r\n\$4\r\nzadd\r\n', '\$', LENGTH(redis_key), '\r\n', redis_key, '\r\n', '\$', LENGTH(redis_score), '\r\n', redis_score, '\r\n', '\$', LENGTH(redis_value), '\r\n', redis_value, '\r' ) FROM( select FtoID ,FmsgID,FdbIndex,FtableIndex,FmsgTimestamp, concat("offlinemsg_",FtoID) as redis_key, FmsgTimestamp as redis_score, concat(FmsgID,"_", FdbIndex,"_", FtableIndex) as redis_value from db_im_user_msg_$nDBIndex.t_im_user_msgidlist_$nTBIndex ) AS T; EOF ) >> $DBFILE done echo "begin migrate data to redis,use $DBFILE .." mysql db_im_user_msg_$nDBIndex --skip-column-names --raw < $DBFILE | redis-cli --pipe echo "migrate data to redis,end" done 这个脚本是将以多库多表存储在MYSQL的数据迁移到REDIS的一个库里。 但是假如,需求是将这些数据安全UID的规则迁移到REDIS的不同的库里呢,怎么办?
#!/bin/sh
DBFILE=./migratedata2redis.sql
function select_data() { DBINDEX=$1 TBINDEX=$2 MOINDEX=$3 #echo "select_data $DBINDEX $TBINDEX $MOINDEX"
( cat <<EOF SELECT concat( '*4\r\n\$4\r\nzadd\r\n', '\$', LENGTH(redis_key), '\r\n', redis_key, '\r\n', '\$', LENGTH(redis_score), '\r\n', redis_score, '\r\n', '\$', LENGTH(redis_value), '\r\n', redis_value, '\r' ) FROM( select FtoID ,FmsgID,FdbIndex,FtableIndex,FmsgTimestamp, concat("offlinemsg_",FtoID) as redis_key, FmsgTimestamp as redis_score, concat(FmsgID) as redis_value from db_im_user_msg_$DBINDEX.t_im_user_msgidlist_$TBINDEX where FtoID%4=$MOINDEX ) AS T1; SELECT concat( '*3\r\n\$3\r\nset\r\n', '\$', LENGTH(redis_key), '\r\n', redis_key, '\r\n', '\$', LENGTH(redis_value), '\r\n', redis_value, '\r' ) FROM( select FtoID ,FmsgID,FdbIndex,FtableIndex,FmsgTimestamp, concat("msg_",FmsgID) as redis_key, FmsgTimestamp as redis_score, concat(FmsgID,"_", FdbIndex,"_", FtableIndex) as redis_value from db_im_user_msg_$DBINDEX.t_im_user_msgidlist_$TBINDEX where FtoID%4=$MOINDEX ) AS T2; EOF ) >> $DBFILE }
function traverse_tb() { DBINDEX=$1 MOINDEX=$2 #echo "traverse_tb $DBINDEX $MOINDEX"
for((nTBIndex=0;nTBIndex<64;nTBIndex++)) do select_data $DBINDEX $nTBIndex $MOINDEX done }
function traverse_md() { DBINDEX=$1 # echo "traverse_md $DBINDEX"
for((nModNum=0;nModNum<4;nModNum++)) do [ -f $DBFILE ] && rm -rf $DBFILE traverse_tb $DBINDEX $nModNum echo " mysql db_im_user_msg_$DBINDEX --skip-column-names --raw < $DBFILE | redis-cli --pipe -n $nModNum " mysql db_im_user_msg_$DBINDEX --skip-column-names --raw < $DBFILE | redis-cli --pipe -n $nModNum # echo "migrate data to redis,end" done }
function traverse_db() { for((nDBIndex=0;nDBIndex<4;nDBIndex++)) do echo -e "\n-------traverse_db $nDBIndex--------------" traverse_md $nDBIndex done }
function main() { traverse_db }
main
初步思路是:先把数据全量迁移到一个库,然后keys * 到指定的记录,再一个一个的move一遍。
|