cat redis.sh #!/bin/bash #-------------------------------------------------- #Created:2015-04-29 #Author:jimmygong #Mail:[email protected] #Function:backup redis && ftp #Version:1.0 #-------------------------------------------------- if [[ `id -u` -ne 0 ]] then echo "sctipts need root" exit 1 fi set -o nounset localip=`ifconfig eth0|awk ‘/inet addr:/‘|awk -F: ‘{print $2}‘|awk -F" " ‘{print $1}‘` currdate=`date +%Y%m%d` redisdir="/var/lib/redis" redisfile="dump.rdb" redisport="6379" rediscmd="/usr/bin/redis-cli" redisbackupdir="/opt/redisbackup/${currdate}" redisbackuplog="/opt/redisbackuplog/" targz="${localip}.${redisfile}.tar.gz" ftphost="10.1.10.133" ftpport="21" ftpuser="ftpup" ftppass="ftpup" [[ -d ${redisbackupdir} ]] || mkdir -p ${redisbackupdir} [[ -e /usr/bin/nmap ]] || apt-get -y install nmap --force-yes > /dev/null 2>&1 echosucc () { echo "Ok" } echofail () { echo "Fail" exit 1 } function echoresult () { if [[ $? == ‘0‘ ]] then echosucc else echofail fi } function exportlog () { [[ -d ${redisbackuplog} ]] || mkdir -p ${redisbackuplog} logfile=${redisbackuplog}/${currdate}.${localip}.log exec > ${logfile} 2>&1 } function bgsave () { echo -n "Bgsave:" ${rediscmd} -h ${localip} -p ${redisport} bgsave > /dev/null 2>&1 echoresult } function backup () { echo -n "Targz:" cd ${redisdir} && cp ${redisdir}/${redisfile} ${redisbackupdir} cd ${redisbackupdir} && tar czf ${targz} ${redisfile} && rm -f ${redisbackupdir}/${redisfile} echoresult } function ftpcheck () { echo -n "FtpServer:" nmaphost=`nmap ${ftphost} -p ${ftpport}|grep "open"|wc -l` [[ ${nmaphost} -eq 1 ]] && echo "Ok" } function ftpcmd () { file=$1 ftp -in<<EOF open ${ftphost} user ${ftpuser} ${ftppass} cd 11 mkdir ${currdate} cd ${currdate} mkdir ${localip} cd ${localip} mput ${file} quit EOF } function ftpputfile () { ftpcheck echo -n "FtpFile:" cd ${redisbackupdir} for i in `ls` do ftpcmd $i done echoresult } function delete () { echo -n "Delete:" cd ${redisbackupdir} && find -name "*.tar.gz" -mtime +30|xargs -i {} rm -f echoresult } function ftpputlog () { cd ${redisbackuplog} ftp -in<<EOF open ${ftphost} user ${ftpuser} ${ftppass} cd 11 mkdir log cd log mput ${currdate}*.log quit EOF } exportlog bgsave backup ftpputfile delete ftpputlog exit 0 =============================说明============================================= bash redis.sh 执行结果 ll /opt/redisbackup/20150429/ -rw-r--r-- 1 root root 148 2015-04-29 23:20 10.1.10.133.dump.rdb.tar.gz ll /opt/redisbackuplog/ -rw-r--r-- 1 root root 53 2015-04-29 23:20 20150429.10.1.10.133.log cat /opt/redisbackuplog/20150429.10.1.10.133.log Bgsave:Ok Targz:Ok FtpServer:Ok FtpFile:Ok Delete:Ok ftp那边 ll /opt/backup/11/20150429/10.1.10.133/ total 4 -rw------- 1 ftpup ftpup 148 2015-04-29 23:20 10.1.10.133.dump.rdb.tar.gz ll /opt/backup/11/ 20150429/ log/ cat /opt/backup/11/log/20150429.10.1.10.133.log Bgsave:Ok Targz:Ok FtpServer:Ok FtpFile:Ok Delete:Ok redis-cli -h 10.1.10.133 -p 6379 redis 10.1.10.133:6379>bgsave Background saving started 在后台异步(Asynchronously)保存当前数据库的数据到磁盘。 BGSAVE 命令执行之后立即返回 OK ,然后 Redis fork 出一个新子进程, 原来的 Redis 进程(父进程)继续处理客户端请求,而子进程则负责将数据保存到磁盘,然后退出。 http://redisdoc.com/ #redis命令参考 因为需要测试脚本是否能正常备份 需要安装redis-server和ftp redis-server(centos6.6)epel源上就有 redis.x86_64 2.4.10-1.el6 @epel redis-server(debian5.0.1) wget mirror.wh-stuttgart.net/apt-mirror/packages.dotdeb.org/dists/wheezy/redis/binary-amd64/redis-server_2.6.14-1~dotdeb.1_amd64.deb proftpd(debian5.0.1) 安装apt-get -y install proftpd cat /etc/redis/redis.conf|egrep -v "^#|^$" |egrep "port|dbfilename|dir" port 6379 dbfilename dump.rdb dir /var/lib/redis shell的内置命令命令exec exec > ${logfile} 2>&1 cat /etc/debian_version 5.0.1
时间: 2024-10-29 23:48:21