1.安装参考:https://www.jb51.net/article/146744.htm
2.安装ruby2.3.3
1.gpg2 --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3
2.curl -L get.rvm.io | bash -s stable
3.rvm生效
source /usr/local/rvm/scripts/rvm
4.安装一个版本
rvm install 2.3.3
5.设置默认ruby
rvm use 2.3.3 --default
6.ruby --version
7.安装redis-dump
gem install redis-dump -V
8.dump出数据
redis-dump >test.json
参考: https://blog.51cto.com/wujianwei/2105124
3.导入数据
$ < db_full.json redis-load
# 带密码的导入
$ < db_full.json redis-load -u :[email protected]:6379
参考:https://www.jianshu.com/p/b3447dc58a51
------------------------------------------------------------------------------------------------
1.主要文件
[root@k8s redis]# tree
.
├── dump_redisdata.sh
├── load_redisdata.sh
├── test.json
└── vars_config.conf
2.变量文件
[root@host163 opt]# cat /mnt/vars_config.conf
WORK_DIR="/mnt/"
DATENOW=`date +%Y%m%d`
OUTPUT_DIR=/data/backup/redis/redis"$DATENOW"
DATETIME=`date +%H%M`
LOAD_FILE=''
REDISDUMP_CMD="/usr/local/rvm/gems/ruby-2.3.3/bin/redis-dump"
HOST_PORT="192.168.1.163:6379"
ALERT_URL="http://192.168.1.144:9093/api/v1/alerts"SUCCESSALERT='[
{
"labels": {
"local_time": "bjsj",
"alertname": "SUCCESSALERTnameaaaaaa",
"instance": "192.168.1.144",
"level": "info..."
},
"annotations": {
"description": "fffredis backup SUCCESS--descriptionsuccess",
"summary": "fffredis backup DONE..."
},
"startsAt": "rfcstartat",
"endsAt": "rfcendat"
}
]'FAILALERT='[
{
"labels": {
"local_time": "bjsj",
"alertname": "fffFAILALERTname111",
"instance": "192.168.1.144",
"level": "CRITICAL.aafff.."
},
"annotations": {
"description": "redis backup FAIL--descriptionsuccess",
"summary": "redis backup FAIL..."
},
"startsAt": "rfcstartat",
"endsAt": "rfcendat"
}
]'
3.备份脚本
[root@host163 opt]# cat /mnt/backup_redis.sh|egrep -v "^$"
#!/bin/bash
#use ruby 2.3.3
source /usr/local/rvm/scripts/rvm
/usr/local/rvm/bin/rvm use 2.3.3 --default
source /mnt/vars_config.conf
#check dir and mkdir new dir if dir not exist
if [ ! -d "$OUTPUT_DIR" ]
then
mkdir -p "$OUTPUT_DIR"
fi
function sent_alert() {
#get time that 8 hours ahead now
date -d'+ 480min' --rfc-3339=seconds |sed 's/ /T/g' >bjsj_startat.txt
date -d'+ 481min' --rfc-3339=seconds |sed 's/ /T/g' >bjsj_endat.txt
#replace the space with T
sed -i "s#rfcstartat#`cat bjsj_startat.txt`#" jsondata.txt
sed -i "s#rfcendat#`cat bjsj_endat.txt`#" jsondata.txt
curl -XPOST -d"`cat jsondata.txt`" $ALERT_URL
}
#begin dump redis data
"$REDISDUMP_CMD" -u $HOST_PORT >"$OUTPUT_DIR"/redis$DATETIME-json
if [[ $? -ne 0 ]];then
echo "$FAILALERT" > jsondata.txt
sent_alert $FAILALERT
exit 1
fi
echo "$SUCCESSALERT" > jsondata.txt
sent_alert $SUCCESSALERT
#remove the old data created 6days ago
/bin/find $BACKUP_DIR -mtime +10 -type d -exec rm -rf {} \;
执行脚本:
bash backup_redis.sh
用一个例子来演示会更加清晰