Redis 数据使用备份恢复

起因 :由于之前公司介绍说,Redis只是做缓存,并未涉及贮存部分
于 2020-4-24晚21:50左右 清理缓存导致,业务部分链接失效,最终到Redis头上
最新的Redis备份是 2020-4-24 凌晨2:30 的备份数据,(发现问题已经是26号)

目前的情况是 : Redis 已经存在了 缓存及贮存数据包含2020-4-24 晚21:50 - 2020-4-26 的 数据

方案确定:使用备份在新的Redis实例中恢复数据,使用脚本进行对现有Redis进行插入操作,(切记是插入操作,不是覆盖)。
1、使用备份对新实例导入数据,并查看。
2、按需恢复,贮存数据在Redis DB3 库中贮存,恢复到生产的Redis DB3 上面,* 插入恢复 *
3、实现步奏:shell or Python

推荐使用Python

shell 实现以下存在缺陷不识别特殊字符

如果有兴趣可以尝试下

复制一个库的部分key
#!/bin/bash
src_ip=redis-01
src_port=6379
src_db=4
src_pw='1234'

dest_ip=redis-02
dest_port=6379
dest_db=5
desc_pw='1234'
 
#要遍历的key
k=(test ws we)

for loop in ${
   k[*]}
do
    redis-cli -h $src_ip -p $src_port -a $src_pw -n $src_db --raw dump $loop | perl -pe 'chomp if eof' | redis-cli -h $dest_ip -p $dest_port -a $desc_pw -n $dest_db -x restore $loop 0
    echo "The value is: $loop"
done
复制全部一个库的全部key
#!/bin/bash
src_ip=Redis-01         # 备份 Redis
src_port=6379
src_db=3
src_pw='1234'

dest_ip=Redis-02        # 目标 Redis
dest_port=6379
dest_db=3
desc_pw='1234'

redis-cli -h $src_ip -p $src_port  -a $src_pw  -n $src_db keys "*" | while read key
do
    redis-cli -h $src_ip -p $src_port -a $src_pw  -n $src_db --raw dump $key | perl -pe 'chomp if eof' | redis-cli -h $dest_ip -p $dest_port -a $desc_pw -n $dest_db -x restore $key 0
    echo "migrate key $key"
done
Python 实现

import redis
import time
import sys
import getopt


class RedisCopy:
    """A class for copying keys from one server to another.
    """

    #some key prefix for this script
    mprefix = 'mig:'
    keylistprefix = 'keylist:'
    hkeylistprefix = 'havekeylist:'

    # numbers of keys to copy on each iteration
    limit = 10000

    def __init__(self, source, target, dbs, spass, tpass):
        self.source = source
        self.target = target
        self.dbs = dbs
        self.spass = spass
        self.tpass = tpass

    def save_keylists(self, prefix="*"):
        """Function to save the keys' names of the source redis server into a list for later usage.
        """

        for db in self.dbs:
            db = int(db[0])
            servername = self.source['host'] + ":" + str(
                self.source['port']) + ":" + str(db)
            #get redis handle for server-db
            r = redis.StrictRedis(
                host=self.source['host'], port=self.source['port'], db=db, password=self.spass)

            #returns the number of keys in the current database
            dbsize = r.dbsize()
            #check whether we already have the list, if not get it
            hkl = r.get(self.mprefix + self.hkeylistprefix + servername)
            if hkl is None or int(hkl) != 1:
                print ("Saving the keys in %s to temp keylist...\n" % servername)
                moved = 0
                r.delete(self.mprefix + self.keylistprefix + servername)
                #returns a list of keys matching pattern
                for key in r.keys(prefix):
                    moved += 1
                    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值