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
|
我的博客已迁移到xdoujiang.com请去那边和我交流
一、基础环境说明
1、版本
cat
/etc/debian_version
5.0.1
2、vsftpd和redis-server安装
1)下载redis-server的deb包
axel -n 10 http:
//mirror
.wh-stuttgart.net
/apt-mirror/packages
.dotdeb.org
/dists/wheezy/redis/binary-amd64/redis-server_2
.6.14-1~dotdeb.1_amd64.deb
2)安装redis-server
dpkg -i redis-server_2.6.14-1~dotdeb.1_amd64.deb
3)安装vsftpd
apt-get -y
install
vsftpd
3、redis部分配置信息
cat
/etc/redis/redis
.conf|
egrep
-
v
"^#|^$"
|
egrep
"port|dbfilename|dir"
port 6379
dbfilename dump.rdb
dir
/var/lib/redis
4、redis命令参考
http:
//redisdoc
.com/
5、ip
1)本机ip
10.1.10.133
2)
ftp
的ip
10.1.10.133
二、shell脚本
#!/bin/bash
#--------------------------------------------------
#Created:2015-04-29
#Author:jimmygong
#Mail:jimmygong@taomee.com
#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/
-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
shell的内置命令命令
exec
exec
> ${logfile} 2>&1
|
本文转自 xdoujiang 51CTO博客,原文链接:http://blog.51cto.com/7938217/1641646,如需转载请自行联系原作者