redis实战:redis自动备份与备份管理
使用redis开启多个服务
在实际的部署中redis需要开启多个端口来向外提供服务,不仅要设置不同的启动参数,还需要我们及时做好备份的相关操作,这就需要借助shell或者python等脚本来进行管理配置。接下来我们以一个实际的例子来介绍工作中对于redis的相关操作。
需求:
1.我们需要自动开启多个redis服务;
2.需要定时对redis进行备份;
3.考虑到磁盘的容量,我们需要将备份的规模限制在一个范围,以防磁盘容量不足;
4.对于不同的redis服务,备份文件也需要分目录管理;
在/data目录下创建redis服务所在目录以及redis备份目录:
[root@server1 data]# pwd
/data
[root@server1 data]# ls
redis-backup redis-file
在redis-file中我们开启了4个redis服务,他们绑定了不同的端口,并且指定了不同的目录、备份文件、pid文件:
[root@server1 redis-file]# ls
allredisrun.sh redis_test1 redis_test2 redis_test3 redis_test4
[root@server1 redis-file]# cd redis_test1
[root@server1 redis_test1]# ls
redis.conf redis-server redis_test1.pid redis_test1.rdb run_redis_test1.sh
我们以redis_test1服务为例,可以查看其启动脚本
[root@server1 redis_test1]# cat run_redis_test1.sh
#!/bin/bash
rootdir=/data/redis-file/redis_test1
${rootdir}/redis-server ${rootdir}/redis.conf --port 6381 --dbfilename redis_test1.rdb --save 900 1000 -save 300 10000 --pidfile redis_test1.pid &
其他的redis服务与redis_test1类似,所以我们设置一个总的启动脚本:
[root@server1 redis-file]# cat allredisrun.sh
#!/bin/bash
redis_root_dir="/data/redis-file"
bash="/bin/bash"
for i in `seq 1 4`
do
cd ${redis_root_dir}/redis_test${i}
${bash} ${redis_root_dir}/redis_test${i}/run_redis_test${i}.sh
done
执行