#!/bin/bash
# 查询没有设置过期时间的key;
# 需在redis-cli 目录执行,或者修改脚本中的路径;
# checknottl.sh,默认每scan(ttl) 1000 个key即休眠0.1秒。
# Note:
# shell 脚本无法维护长连接,所以每次ttl都将创建连接,性能有一定影响。
# 删除命令: cat redis.txt | redis-cli -h 127.0.0.1 -a password - p 6379 --pipe
db_ip=127.0.0.1 # Redis ip
db_port=6379 # Redis 端口
password=123456 # Redis 密码
cursor=0 # 第一次游标
cnt=1000 # 每次迭代的数量
new_cursor=0 # 下一次游标
scan_num=0 # 已scan的key数量
./redis-cli -h $db_ip -p $db_port -a $password scan $cursor match "*" count $cnt > scan_tmp_result
new_cursor=`sed -n '1p' scan_tmp_result` # 获取下一次游标
sed -n '2,$p' scan_tmp_result > scan_result # 获取 keys
cat scan_result |while read line # 循环遍历所有 keys
# 2>/dev/null,将标准错误丢弃,Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
do # $scan_num +=1;
#判断不为空
if [!z $line]
then
echo $line >> no_ttlkey.log # 追加到指定文件
fi
done
echo 'scan_num: '$scan_num
while [ $cursor -ne $new_cursor ] # 若 游标 不为 0 ,则证明没有迭代完所有的 key,继续执行
do
./redis-cli -h $db_ip -p $db_port -a $password scan $new_cursor match "*" count $cnt 2>/dev/null> scan_tmp_result
new_cursor=`sed -n '1p' scan_tmp_result`
sed -n '2,$p' scan_tmp_result > scan_result
cat scan_result |while read line
do
$scan_num +=1;
#echo 'del '$line >> no_ttlkey.log
if [!z $line]
then
echo $line >> no_ttlkey.log # 追加到指定文件
fi
#if [ $scan_num % 1000 == 0 ];then
# sleep 0.5
#fi
done
sleep 0.1
echo 'scan_num: '$scan_num
done
rm -f scan_tmp_result
rm -f scan_result
Redis导出key脚本
最新推荐文章于 2024-05-17 10:51:25 发布