Write a shell script to clear all data of Redis Cluster at one time

Redis cluster clear all data script

flush-redis-cluster.shThe data cleaning of the Redis cluster can be completed through the following script file and the file name

#!/bin/sh
# 注意[]内部,两侧要各有一个空格, -n表示字符串长度>0,且$1要用双引号括起来
# 下面表示当第一个参数不为空时,获取下来,作为ip,否则ip取默认值192.168.0.127
if [ -n "$1" ];then
  ip=$1
else
  ip="192.168.0.127"
fi

if [ -n "$2" ];then
  port=$2
else
  port="6379"
fi
# 若不输入参数的话,默认连接 192.168.0.127:6379来获取redis集群信息
NODES=`/usr/share/redis-3.2.11/src/redis-cli -c -h $ip -p $port cluster nodes | cut -f2 -d' '`

IFS="
"

for node in $NODES; do
        echo Flushing node $node...
        /usr/share/redis-3.2.11/src/redis-cli -h ${node%:*} -p ${
    
    node##*:} FLUSHALL
done

This script was found on the Internet and slightly modified according to your own situation. Take this opportunity to learn about Linux Shell. The following is an explanation of the script

The first line #!/bin/shis a statement, it said it would use the /bin/shshell to run the script

The second line defines a variable NODES. The value of the variable is the execution result of the following command. In Shell, use $()or `` to do command substitution. The backtick is to run a redis-cli client, which -cmeans connecting in cluster mode. $1It is the first parameter passed in when running the script, and $2the second parameter passed in. After the connection is established, the cluster nodescluster information can be obtained by running the command. The approximate format is as follows

127.0.0.1:6379> cluster nodes
99e243ecc5e2283641940b9ada8df9f86d051378 192.168.0.132:6379 master - 0 1594877798080 6 connected 9102-10922
9b993b8b1110ef3d258b73ee0a34dbc96de65636 192.168.0.127:6379 master - 0 1594877802088 1 connected 0-1819
bcbc20c59d2788c355e55684a58b8b1cd37ed748 192.168.0.136:6379 master - 0 1594877803090 9 connected 14564-16383
88158f7ff5067b759dde8ec05722455c6a27b320 192.168.0.129:6379 master - 0 1594877804093 3 connected 3641-5460
ecb9c87ef49ee302845891ec2a6dfc6850f4cec3 192.168.0.134:6379 master - 0 1594877801086 7 connected 10923-12742
f56b825f569e6b0af2a01e5b00d3abb035dcf027 192.168.0.131:6379 master - 0 1594877799080 5 connected 7282-9101
d48e1a17d19057375df6c910c7d662074f0d636c 192.168.0.128:6379 myself,master - 0 0 2 connected 1820-3640
7ccd60a0f0d6a1c56d89778806ab5f7d8e9665e9 192.168.0.135:6379 master - 0 1594877803090 8 connected 12743-14563
60f780efe859010230d820841c0ca819ce2dd8d7 192.168.0.130:6379 master - 0 1594877800083 4 connected 5461-7281

Then use |the command to pass the obtained information to the next command, and use the cutcommand to cut the string, which is -dused to set the separator used during ' 'cutting. The space above is used. After cutting, a string array will be obtained, which -f2means that the array is taken. The second element, you can get the ip address and port number of the cluster

So the variable NODES is a string, which contains all the node ip and port numbers of the redis cluster, \r\nseparated by newlines , and its contents are as follows

"192.168.0.132:6379
192.168.0.127:6379
192.168.0.136:6379
192.168.0.129:6379
192.168.0.134:6379
192.168.0.131:6379
192.168.0.128:6379
192.168.0.135:6379
192.168.0.130:6379"

Subsequent

IFS="
"

IFS is an important concept of Shell scripts. It is used to set the separator of the Shell. The above means that the separator of the Shell is set to a line break.\r\n

Then the variable NODES is traversed through the for loop. When traversing, IFS will be used as the separator, that is, the ip address and port number of a certain redis node are retrieved each time. For example, in the first round of traversal, the value of node is192.168.0.132:6379

In one cycle, use the redis-cli client to connect to a specific redis node, and execute the flushall command to clear all data on the node

Note ${node%:*}Used to cut the string, the meaning is to delete the first colon from right to left of the node variable :, and the string to the right, and the final result is the ip address of the redis node

${node##*:}Used to cut the string, the meaning is to delete the last colon of the node variable from left to right :, and the string on the left, and the final result is the port number of the redis node

About $and #and%

$() #用于做命令替换,等同于 ``
# `` 几乎可以在所有的unix shell中使用
# $() 并不是所有shell都支持
fileInfo=$(ls -l)
fileInfo=`ls -l`

${
    
    } #用于做变量替换,比如
echo ${fileInfo}
# ${fileInfo} 和 $fileInfo 其实没有不一样,但是加了{}会比较精确的界定变量名称的范围

$$ #获取当前进程的PID
$! #获取最后运行的进程的PID
$? #获取上一个命令的返回值
$* # 获取所有参数列表
$@ # 同上
# 在Shell脚本中
$0 #获取Shell脚本本身的文件名
$1 #获取Shell的第一个参数
$2 #同上
$# #获取输入参数的个数
# 在 Shell 脚本中使用 $ 引用变量
# 可以用 ${} 结合#% 来对变量进行裁剪和替换, 如下
file=/dir1/dir2/yogurt.file.txt # 注意定义变量时, 变量名和=之间不能有空格
echo ${
    
    file#*/} #移除从左往右的第一个/及其左边的子串
echo ${
    
    file##*/} #移除从左往右的最后一个/及其左边的子串
echo ${file%.*} #移除从右往左的第一个.及其右边的子串
echo ${file%%.*} #移除从右往左的最后一个.及其右边的子串

# 记忆方法:
# 键盘上,#在$的左边,表示移除左侧的字符串,%在$的右边,表示移除右侧的字符串
# 用#进行移除时,使用通配符*/ 表示要移除的是左侧任意字符串加一个/
# 用%进行移除时,使用通配符/* 表示要移除的是左边加一个/,以及右侧任意字符串
# 一个#或一个%表示非贪婪匹配,即找到的第一个位置
# 两个#或两个%表示贪婪匹配,会尽可能匹配的多,也即找到的最后的位置
# #从左侧开始匹配, %从右侧开始匹配

Redis cluster fuzzy delete key script

#!/bin/sh

if [ -n "$1" ];then
  ip=$1
else
  ip="192.168.0.127"
fi

if [ -n "$2" ];then
  port=$2
else
  port="6379"
fi

redis_cli=/usr/share/redis-3.2.11/src/redis-cli

NODES=`$redis_cli -c -h $ip -p $port CLUSTER NODES | cut -f2 -d' '`


IFS="
"

for node in $NODES;do
    echo Flushing igola-vi cache in node $node...
    connect="$redis_cli -h ${node%:*} -p ${node##*:}"
    command="$connect keys 'igolavi*' | xargs -i $connect del {} >/dev/null"
    eval $command
done

The above script can delete the key prefixed with igolavi in ​​the Redis cluster

Guess you like

Origin blog.csdn.net/vcj1009784814/article/details/107383286