Redis导出key脚本

#!/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


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值