恢复方法:停止目标redis,把rdb和aof拷贝到${BASE_DIR}/var,修改权限:chown redis:redis -R /opt/redis/var/* ,启动即可
该备份和恢复方案已经过实测。
脚本内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
#!/bin/bash ################################### # # function 适用于有持久数据的同机多redis备份 # # touch /root/sh/backup_redis.sh; chmod 700 /root/sh/backup_redis.sh # # cron: # 37 5 * * * root /root/sh/backup_redis.sh 6379 default >> /root/sh/backup_redis.log 2>&1 # # # date author note # 2018/04/17 mail@zhaoyanan.cn 创建备份,备份校验,异地上传,告警通知,帮助说明,多redis进程备份等功能 # 2018/08/16 mail@zhaoyanan.cn 为适应监管要求,管理大量备份包,基于静态数据备份脚本,整合运维cmdb改编,主要增强管理备份名称和提升易部署性 ################################### export LANG=C #export LC_ALL=C export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin export datetime=$(date +%Y%m%d–%H%M) ###### 配置开始 ###### export parameter1=$1 export parameter2=$2 # 本机IP,如果获取不准,可改为手动填写 export local_primary_ip=`ip addr show dev eth0 | grep “inet” | awk –F ‘ ‘ ‘{print $2}’ | cut –d‘/’ –f 1` #export local_primary_ip=手动填写ip #export project_name=”redis_$1″ # 使用cmdb获取项目名称,这里的手动配置已弃用 export backup_dir=/opt/data_bak/app_bak export script_dir=/root/sh export work_dir=${script_dir} export tmp_dir=/tmp #export retain_days=8 # 使用cmdb获取本地保留天数,这里的手动配置已弃用 # redis自定义安装的目录 export BASE_DIR=/opt/redis # 服务器信息 export HOST=“127.0.0.1” export PORT=“$1” export REDIS_DB=0 # 如果无密码,置空即可。或者直接从本地配置中获取密码 # PASSWD=”pass” export PASSWD=`grep –v “^#” ${BASE_DIR}/redis.conf | sed ‘/^$/d’ | grep requirepass | awk ‘{ print $2; }’` export REDISCLI=“${BASE_DIR}/bin/redis-cli” if [ ! –f “$REDISCLI” ];then REDISCLI=“${BASE_DIR}/src/redis-cli” fi ### cmdb查询开始 ### # 使用cmdb控制备份包名信息,有助于提升备份多个方面的自动化,其中包括统一管理保留时间,半自动化恢复校验,跨地域多重备份等,收获效率,减少遗漏,在众多任务堆积时,备份工作的优先级通常是最低的,所以备份自动化的意义深远。 curl —connect–timeout 7 –sSd “sql=select project,subproject,en_name,ip,retain_days,mail from server where status=0 and lanip=\”${local_primary_ip}\”” http://cmdb_address/ > ${tmp_dir}/cmdbdata_tmp.txt # 如果没有运维cmdb或cmdb暂时不可用,可以放弃查询,手动给cmdb_开头的5个变量赋值 # 例如:cat “projectname subprojectname 1.2.3.4 30 mail@zhaoyanan.cn” > ${tmp_dir}/cmdbdata_tmp.txt i=`grep “[0-9]” –c ${tmp_dir}/cmdbdata_tmp.txt` if (( “$i” == 1 ));then /bin/cp –ap ${tmp_dir}/cmdbdata_tmp.txt ${tmp_dir}/cmdbdata.txt echo “从cmdb成功获取到数据,开始应用” else echo “从cmdb获取到的数据有问题,仍使用老数据” fi # 注意: ${tmp_dir}/cmdbdata.txt的数据由cmdb中自动获取,一般情况下不应该人为去修改,除非cmdb暂时不好用了 while read line do export cmdb_project_value=`echo $line | awk ‘{ print $1; }’` export cmdb_subproject_value=`echo $line | awk ‘{ print $2; }’` export cmdb_en_name_value=`echo $line | awk ‘{ print $3; }’` export cmdb_ip_value=`echo $line | awk ‘{ print $4; }’` export cmdb_retain_days=`echo $line | awk ‘{ print $5; }’` export cmdb_mail=`echo $line | awk ‘{ print $6; }’` done < ${tmp_dir}/cmdbdata.txt export project_name=${cmdb_project_value}_${cmdb_subproject_value}_${cmdb_en_name_value}_${cmdb_ip_value}_${parameter1} export retain_days=$cmdb_retain_days ### cmdb查询结束 ### # 跨机备份,FTP信息 export ftpip=‘备份异地IP’ export ftpport=’21’ export ftpuser=‘ftpuser’ export ftppw=‘pass’ export ftpsw=“yes” # To turn on or off the FTP upload if [ “$cmdb_ip_value” == “” ];then echo “ip不存在,灾备可能会上传Null目录,或灾备父根目录” elif [ “$cmdb_en_name_value” == “” ] || [ “$cmdb_en_name_value” == “Null” ] || [ “$cmdb_ip_value” == “Null” ];then export ftpdir=${cmdb_project_value}/${cmdb_ip_value} else export ftpdir=${cmdb_project_value}/${cmdb_en_name_value}_${cmdb_ip_value} fi # 发件方式,没有装mutt的就填mailto_simple,装了mutt是的可以填mailto_advanced,后者支持中文,被判断为垃圾邮件的概率也较小 # 值为no,就是关闭发邮件 #export mailto=mailto_simple #export mailto=mailto_advanced export mailto=no # 邮件配置,默认发件人 export mailfromadd=“sender@zhaoyanan.cn” # 邮件配置,默认收件人 export mailtoadd=“$cmdb_mail” ###### 配置结束 ###### ############# PROGRAM ############## # 记录开始时间 start_time=$(date +%s) ### 函数 ### # 帮助信息 function help_msg() { echo –e “\033[41merror!!! 小心点,不要慌,现在出错了,请仔细查看下面的帮助信息:\033[0m” cat <<EOF redis数据备份和恢复 HELP: \$1: parameters eg: 端口 \$2: parameters eg: default # 备份 exec eg: sh $0 6379 default # 对端口为6379的redis数据dump 恢复方法:停止目标redis,把rdb和aof拷贝到${BASE_DIR}/var,修改权限:chown redis:redis –R /opt/redis/var/* ,启动即可 该备份和恢复方案已经过实测 EOF echo –e “\033[41merror!!! 小心点,不要慌,现在出错了,请仔细查看上面的帮助信息:\033[0m” } function redis_backup() { echo “客户端版本:” ${BASE_DIR}/bin/redis–cli —version echo “服务端版本:” ${BASE_DIR}/bin/redis–server —version # 判断是否有密码 if [ “$PASSWD” != “” ];then pw_parameter=“-a” else pw_parameter=“” fi REDISCONN=“$REDISCLI -h $HOST -p $PORT $pw_parameter $PASSWD” # 判断$1是否是数字 if grep ‘^[[:digit:]]*$’ <<< “$1” > /dev/null; then true else help_msg exit echo “`date +”%Y–%m–%d %H:%M:%S“` error, exit.” fi # 清理老备份 cd /tmp cd $backup_dir find –maxdepth 1 –name “*.rdb.gz” –mtime +$retain_days –exec rm –f {} \; find –maxdepth 1 –name “*.aof.gz” –mtime +$retain_days –exec rm –f {} \; cd $work_dir # 备份 $REDISCONN —rdb ${backup_dir}/${project_name}_redis_dump_${datetime}.rdb $REDISCONN BGREWRITEAOF sync;sleep 1s;sync if [ “$parameter1” == “6379” ];then cp –ap ${BASE_DIR}/var/appendonly.aof ${backup_dir}/${project_name}_redis_appendonly_${datetime}.aof else cp –ap ${BASE_DIR}/var/appendonly_${parameter1}.aof ${backup_dir}/${project_name}_redis_appendonly_${datetime}.aof fi redis_backup_check cat /dev/null > ${tmp_dir}/${project_name}_backup_list.txt echo “${project_name}_redis_dump_${datetime}.rdb.gz” >> ${tmp_dir}/${project_name}_backup_list.txt echo “${project_name}_redis_appendonly_${datetime}.aof.gz” >> ${tmp_dir}/${project_name}_backup_list.txt } function redis_backup_check() { # 校验 ${BASE_DIR}/bin/redis–check–rdb ${backup_dir}/${project_name}_redis_dump_${datetime}.rdb > ${tmp_dir}/${project_name}_redis_dump_rdb.log 2>&1 ${BASE_DIR}/bin/redis–check–aof ${backup_dir}/${project_name}_redis_appendonly_${datetime}.aof > ${tmp_dir}/${project_name}_redis_appendonly.log 2>&1 rdb_check_result=`cat ${tmp_dir}/${project_name}_redis_dump_rdb.log | egrep “Checksum OK|RDB looks OK” | wc –l` aof_check_result=`cat ${tmp_dir}/${project_name}_redis_appendonly.log | grep “AOF is valid” | wc –l` if [ “$rdb_check_result” != “2” ] || [ “$aof_check_result” != “1” ];then echo “” echo “rdb检查结果:” cat ${tmp_dir}/${project_name}_redis_dump_rdb.log echo “” echo “aof检查结果:” cat ${tmp_dir}/${project_name}_redis_appendonly.log mail_send else # 备份成功的才压缩 gzip ${backup_dir}/${project_name}_redis_dump_${datetime}.rdb gzip ${backup_dir}/${project_name}_redis_appendonly_${datetime}.aof fi } # 简单发件函数 function mailto_simple() { # mail /usr/sbin/sendmail –t <<EOF From: $mailfromadd To: $mailtoadd Cc: $mailccadd Subject: [${project_name}] $sub $msg EOF } # 标准发件函数 function mailto_advanced() { # mail echo “$msg” | /opt/mutt/bin/mutt \ –e ‘set content_type=”text/html”‘ \ –s “[${project_name}] $sub” \ –e ‘my_hdr from:’“$mailfromadd” \ –c “$mailccadd” \ “$mailtoadd” — } function mail_send() { if [ “$mailto” == “mailto_simple” ];then sub=“${project_name} Redis Backup failed, Please check the bak log” msg=“Such as title.” mailto_simple elif [ “$mailto” == “mailto_advanced” ];then # 该功能暂未写 mailto_advanced elif [ “$mailto” == “no” ];then echo “no mailto.” else echo “error, no mailto.” fi } function up_bak() { cd $backup_dir if [ “$ftpsw” == ‘yes’ ]; then for filename in `cat ${tmp_dir}/${project_name}_backup_list.txt` do ftp –v –n –i <<END open $ftpip $ftpport user $ftpuser $ftppw bin cd $ftpdir put ${filename} close bye END done else echo “`date +”%Y–%m–%d %H:%M:%S“` 没有开启异地灾备。” fi cd $work_dir } # 开始工作 cd $work_dir if [ “$parameter1” == “” ] || [ “$parameter1” == “help” ]; then help_msg exit echo “`date +”%Y–%m–%d %H:%M:%S“` error, exit.” elif [ “$parameter2” == “default” ]; then redis_backup up_bak else help_msg exit echo “`date +”%Y–%m–%d %H:%M:%S“` error, exit.” fi echo “” stop_time=$(date +%s) echo “本次脚本运行了$((${stop_time}-${start_time}))秒。” |
发表回复