redis 脚本扫描

1.创建脚本

vi scan_get_all_keys.sh

#!/bin/bash

redisbase='/usr/local/bin'

##定义使用说明
usage(){
echo -e "\033[31m脚本功能:\033[0m"
echo -e " 用于扫描redis实例中所有的key"
echo -e " "
echo -e "\033[31m示例如下:\033[0m"
echo -e " sh scan_get_all_keys.sh redis_node_6577 6577 0 1000 'match *MP001_1'"
echo -e " "
echo -e "\033[31m注意事项:\033[0m"
echo -e " 1. 只能用于redis 2.8及以上版本,因为只有这些版本支持scan命令"
echo -e " 2. 第三个参数表示每次scan的key个数,不要超过300"
}


##参数判断
if [ "$1"x = x ] || ([ -n "$1" ] && [ "$2"x = x ] )
then
usage
exit
fi

i=0

host=$1
port=$2
db=$3
num=$4
#matchpattern为需要模糊匹配的表达式参数
matchpattern=$5

 

tmp_file=${host}_${port}_tmp.txt
result_file=${host}_${port}.txt
rm -rf ${result_file} >> /dev/null
touch ${result_file} >> /dev/null

if [ 1 ]
then
${redisbase}/redis-cli -h $host -p $port -n ${db} scan ${i} ${matchpattern} count $num > ${tmp_file}
i=`head -1 ${tmp_file}`
counts_tmp=`cat ${tmp_file}|wc -l`
counts=$(( $counts_tmp - 1 ))
tail -${counts} ${tmp_file} >> ${result_file}
fi
while [ $i -ne 0 ]
do
${redisbase}/redis-cli -h $host -p $port -n ${db} scan ${i} ${matchpattern} count $num > ${tmp_file}
i=`head -1 ${tmp_file}`
counts_tmp=`cat ${tmp_file}|wc -l`
counts=$(( $counts_tmp - 1 ))
tail -${counts} ${tmp_file} >> ${result_file}
done
rm -rf $tmp_file
sed '/^$/d' ${result_file} > final_${result_file}

2.执行脚本

---放在空目录下执行

sudo sh scan_get_all_keys.sh 10.1.1.1 6379 8 300 'match plus-gat:REQ_IP_LIMITED_*'

redis IP:10.1.1.1
端口:6379
库序列号:8
keys 个数:300
like 扫描:'match like 那部分*'

 

转载于:https://www.cnblogs.com/ss-33/p/10973087.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值