由于历史原因,需要将一批key做判断后进行调整,规则如下:
1、如果源端的成绩大于目标端的,那么将源和目标的对应hash的字段相加,更新到目标端。
2、如果源端的成绩小于目标端的,那么将这些key记录下来,提供业务进行手动检查处理。
3、如果源端成绩和目标端相等,那么记录这些key,不做任何其他处理。
测试数据
源 redis-cli -p 9000 -n 8 hmset test_100 abc 0.1 xyz 1533 redis-cli -p 9000 -n 8 hmset test_200 abc 0.1 xyz 1000 redis-cli -p 9000 -n 8 hmset test_300 abc 0.8 xyz 77822 redis-cli -p 9000 -n 8 hmset test_400 abc 699.70000000000001028 xyz 7128337 redis-cli -p 9000 -n 8 hmset test_500 abc 888.70000000000001028 xyz 8828337 redis-cli -p 9000 -n 8 hmset test_600 abc 888.70000000000001028 xyz 8828337 目标 redis-cli -p 7000 -n 8 hmset test_100 abc 0.8 xyz 22367 redis-cli -p 7000 -n 8 hmset test_200 abc 0.4 xyz 4000 redis-cli -p 7000 -n 8 hmset test_300 abc 0.2 xyz 16612 redis-cli -p 7000 -n 8 hmset test_400 abc 0.7 xyz 7000 redis-cli -p 7000 -n 8 hmset test_500 abc 888.70000000000001028 xyz 8828337 redis-cli -p 7000 -n 8 hmset test_600 abc 888.70000000000001028 xyz 8828337
PS:相关的IP和字段已经脱敏。
source_ip="192.168.1.1" source_port=$1 source_db=1 dest_ip="192.168.1.2" dest_port=6379 dest_db=1 for i in `cat /home/xiaodongl/check/${source_port}/change_${source_port}.txt` do source_value=($(redis-cli -h $source_ip -p $source_port -n $source_db hmget $i abc xyz|xargs -n2)) dest_value=($(redis-cli -h $dest_ip -p $dest_port -n $dest_db hmget $i abc xyz)) source_abc=${source_value[0]} source_xyz=${source_value[1]} dest_abc=${dest_value[0]} dest_xyz=${dest_value[1]} if [[ -z $dest_abc ]];then echo "$dest_ip:$dest_port not have the key $i" >> dest_no_keys.txt else #源比目标大处理 check_ret1=$(echo "$source_abc>$dest_abc"|bc) if [[ $check_ret1 -eq 1 ]];then new_abc=$(echo "$source_abc+$dest_abc"|bc) new_xyz=$(echo "$source_xyz+$dest_xyz"|bc) echo -e "key $i 源端:source_abc:$source_abc 目标端:dest_abc:$dest_abc" | tee -a change_${source_port}.log echo -e "key $i 源端:source_xyz:$source_xyz 目标端:dest_xyz:$dest_xyz" | tee -a change_${source_port}.log echo -e "key $i 待修改后:abc:$new_abc" | tee -a change_${source_port}.log echo -e "key $i 待修改后:xyz:$new_xyz" | tee -a change_${source_port}.log redis-cli -h $dest_ip -p $dest_port -n $dest_db hmset $i abc $new_abc xyz $new_xyz check_new_value=($(redis-cli -h $dest_ip -p $dest_port -n $dest_db hmget $i abc xyz | xargs -n2)) current_abc=${check_new_value[0]} current_xyz=${check_new_value[1]} echo -e "key $i 修改后确认:abc:$current_abc" | tee -a change_${source_port}.log echo -e "key $i 修改后确认:xyz:$current_xyz" | tee -a change_${source_port}.log echo | tee -a change_${source_port}.log fi #源比目标小处理 check_ret2=$(echo "$source_abc<$dest_abc"|bc) if [[ $check_ret2 -eq 1 ]];then echo "key $i 源端比目标端小未处理,需确认." | tee -a check_${source_port}.log echo "--------key:$i--------------" | tee -a check_${source_port}.log echo "源端:abc:$source_abc" | tee -a check_${source_port}.log echo "目标端:abc:$dest_abc" | tee -a check_${source_port}.log echo "源端:xyz:$source_xyz" | tee -a check_${source_port}.log echo "目标端:xyz:$dest_xyz" | tee -a check_${source_port}.log echo | tee -a check_${source_port}.log continue fi #源和目标相等 check_ret3=$(echo "$source_abc==$dest_abc"|bc) if [[ $check_ret3 -eq 1 ]];then echo "key $i 源端和目标端相等,未处理." | tee -a equal_${source_port}.log echo "--------key:$i--------------" | tee -a equal_${source_port}.log echo "源端:abc:$source_abc" | tee -a equal_${source_port}.log echo "目标端:abc:$dest_abc" | tee -a equal_${source_port}.log echo "源端:xyz:$source_xyz" | tee -a equal_${source_port}.log echo "目标端:xyz:$dest_xyz" | tee -a equal_${source_port}.log echo | tee -a equal_${source_port}.log continue fi fi done