一个导出redis有序集合sorted-sets的shell脚本

  1. 通过keys匹配需要导出的有序集合名称,这些集合命名格式为:*_010_09/Dec/2015

  2. 依次通过zscan导出有序集合中的数据,并分别保存

#/bin/sh
zset_pattern="*_010_14/Dec/2015"
_date=`echo "$zset_pattern"|awk -F"_" '{print $3}'|awk -F"/" '{print $3"_"$2"_"$1}'`
keys=`redis-cli keys $zset_pattern`

for k in $keys;do
        out=`echo "$k"|awk -F"_" '{print $1"_"$2}'`"_$_date.txt"

        cursor=0
        batches=0
        while [ 1==1 ];do
                reply=`redis-cli zscan $k $cursor`
                cursor=`echo "$reply" | head -n 1`
                pairs=`echo "$reply"|tail -n +2`
                [ $cursor -eq 0 ] && [ $batches -gt 0 ] && break
                batches=`expr $batches + 1`
                skip_key=0
                #for ms in $pairs;do
                echo "$pairs"|while read ms
                do
                        #skip empty members
                        [ "$ms" == "" ] && skip_key=1 && continue
                        [ $skip_key -eq 1 ] && skip_key=0 && continue
                        if [ "$m" == "" ];then
                                m=$ms && continue
                        fi
                        if [ "$s" == "" ];then
                                s=$ms
                                echo "$m $s" >> $out
                                m="" && s=""
                        fi
                done
        done
done

你可能感兴趣的:(redis,shell,redis-cli,Export,sorted-set,zscan)