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 1fiset - o nounsetlocalip= ' 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>& 1echosucc () { 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} quitEOF}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 quiteof}exportlogbgsavebackupftpputfiledeleteftpputlogexit 0================== =========== description =============================================bash redis.sh Execution Results ll /opt/redisbackup/ 20150429/-rw-r--r-- 1 root root 148 2015-04-29 23:20 10.1.10.133. dump.rdb.tar.gzll /opt/redisbackuplog/-rw-r--r-- 1 root root 53 2015-04-29 23:20 20150429.10.1.10.133.logcat /opt/redisbackuplog/20150429.10.1.10.133.log bgsave:oktargz:o KFtpServer:OkFtpFile:OkDelete:Okftp over there LL&NBSP;/OPT/BACKUP/11/20150429/10.1.10.133/TOTAL&NBSP;4-RW------- 1 ftpup ftpup 148 2015-04-29 23:20 10.1.10.133.dump.rdb.tar.gzll /opt/backup/11/20150429/ log/ cat /opt/backup/11/log/20150429.10.1.10.133.log bgsave:oktargz:okftpserver:o Kftpfile:okdelete:okredis-cli -h 10.1.10.133 -p 6379redis 10.1.10.133:6379> Bgsavebackground saving started asynchronously (asynchronously) saves data from the current database to disk in the background. The bgsave command returns &NBSP;OK&NBSP immediately after execution, and then Redis fork a new child process, the original Redis process (parent process) to continue processing the client request , and the child process is responsible for saving the data to disk and then exiting. http://redisdoc.com/ #redis命令参考因为需要测试脚本是否能正常备份 need to install Redis-server and Ftp redis-server ( centos6.6) redis.x86_64 2.4.10-1.el6 on Epel source @epelredis-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.DEBPROFTPD (debian5.0.1) installation apt-get -y install proftpdcat /etc/redis/redis.conf|egrep -v "^#|^$" |egrep "port| Dbfilename|dir "Port 6379dbfilename dump.rdbdir /var/lib/redisshell built-in command command execexec > ${logfile} 2>&1cat /etc/debian_version 5.0.1
This article is from the "7928217" blog, please be sure to keep this source http://7938217.blog.51cto.com/7928217/1641475
Use shell scripts to back up Redis and upload to the FTP server