16 02月
2530

redis 集群批量删除

#!/bin/bash
redis_comm=redis-cli

#从当前节点查询集群信息, 查询所有master节点 生成数组
redis_list=$($redis_comm -h 192.168.11.191 -p 6379 cluster nodes | grep master |  awk '{print $2}' |  awk -F['@'] '{print $1}')

#用临时文件也行 
#$redis_comm -p 9001 cluster nodes | grep master |  awk '{print $2}' |  awk -F['@'] '{print $1}' > tmp
#i=0
#while read line
#do
#    redis_list[i]=$line
#    i=$(($i+1))
#done < ./tmp


read -p "请输入要删除的key格式: " searchkey
read -p "是否显示查询的所有key(Y/N): " searchFlag

if [ $searchFlag = "Y" ] || [ $searchFlag = "y" ]; then
    for info in ${redis_list[@]}
    do
        echo "开始查询: $info"
        ip=`echo $info | cut -d : -f 1`
        port=`echo $info | cut -d : -f 2`
        $redis_comm -c -h $ip -p $port keys "$searchkey"
    done
fi

read -p "确定删除(Y/N): " runCommand
if [ $runCommand = "Y" ] || [ $runCommand = "y" ]; then
    for info in ${redis_list[@]}
    do
        echo "开始执行: $info"
        ip=`echo $info | cut -d : -f 1`
        port=`echo $info | cut -d : -f 2`
        $redis_comm -c -h $ip -p $port keys "$searchkey" | xargs -i $redis_comm -h $ip -p $port del {}
    done
    echo "完成"
fi