一键搭建Sersync+Rsync相互实时同步(双机热备)

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
#!/bin/bash

rsname=Rsync+Sersync
rsver=1.0
sip=172.16.8.23
dip=172.16.8.24
run_user=rsync
base_dir=/usr/local
rsync_dir=/home/wwwroot/test22
port=873
auth_users=ropon
passwd=test123
sersync_version=2.5.4
mirrorLink=http://panel.ropon.top/rsync/
sersync_install_dir=/usr/local/sersync
confpath=$sersync_install_dir/confxml.xml
rsync_cmd="rsync -artuz -R --delete $auth_users@$dip::rsync $rsync_dir --password-file=/etc/rsyncd.pass"
sersync_cmd="/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml"
flag=$1

Echo()
{
case $1 in
success) flag="\033[1;32m"
;;
failure) flag="\033[1;31m"
;;
warning) flag="\033[1;33m"
;;
msg) flag="\033[1;34m"
;;
*) flag="\033[1;34m"
;;
esac
if [[ $LANG =~ [Uu][Tt][Ff] ]]
then
echo -e "${flag}${2}\033[0m"
else
echo -e "${flag}${2}\033[0m" iconv -f utf-8 -t gbk
fi
}

header()
{
printf "
###################################################################
# $rsname version $rsver Author: Ropon <idiyrom.com> #
# For more information please visit https://idiyrom.com/83.html #
#-----------------------------------------------------------------#
# Copyright @2017-2018 idiyrom.com. All rights reserved. #
###################################################################

"
}

showhelp()
{
header
Echo "msg" "Usage: $rsname [OPTIONS]"
echo
Echo "msg" "OPTIONS:"
Echo "msg" "-h --help : Show help of $rsname"
Echo "msg" "-u --update : update Check for $rsname"
Echo "msg" "-i --install : install $rsname version $rsver to This System"
Echo "msg" "-U --uninstall : Uninstall $rsname from This System"
echo
}

Download_src() {
[ -s "${src_url##*/}" ] && Echo "msg" "[${src_url##*/}] found" { wget --tries=6 -c --no-check-certificate $src_url; sleep 1; } #判断下载是否已存在
if [ ! -e "${src_url##*/}" ]; then
Echo "msg" "${src_url##*/} download failed, Please contact the author!"
kill -9 $$
fi
}

Install()
{
pushd ${base_dir}/src
id -u $run_user >/dev/null 2>&1
[ $? -ne 0 ] && useradd -M -s /sbin/nologin $run_user
yum install rsync -y
cat > /etc/rsyncd.conf <<EOF
uid=$run_user
gid=$run_user
use chroot = no
port=$port
#address=$sip
max connections=2000
timeout=200
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
log format=%t %a %m %f %b

[rsync]
path=$rsync_dir
commemt=rsync
list=yes
read only=no
#write only=no
auth users=$auth_users
secrets file=/etc/rsyncd.secret
ignore errors=yes
hosts allow=$dip
hosts deny=
EOF

cat > /etc/rsyncd.secret <<EOF
$auth_users:$passwd
EOF

cat > /etc/rsyncd.pass <<EOF
$passwd
EOF

chmod 600 /etc/rsyncd.secret /etc/rsyncd.pass
[ ! -d "$rsync_dir" ] && mkdir -p $rsync_dir
chown $run_user.$run_user -R $rsync_dir
systemctl enable rsyncd
systemctl start rsyncd
[ $? -eq 0 ] && Echo "success" "rsyncd服务安装成功,且已正常启动。"

[ ! -f sersync-${sersync_version}.tar.gz ] && src_url=${mirrorLink}/sersync-${sersync_version}.tar.gz && Download_src
[ ! -d "$sersync_install_dir" ] && mkdir -p $sersync_install_dir
tar -xzf sersync-${sersync_version}.tar.gz -C $sersync_install_dir
sed -i "s@/ropon@$rsync_dir@g" $confpath
sed -i "s@pengge@$auth_users@g" $confpath
sed -i "s@192.168.19.19@$dip@g" $confpath
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml

cat >> /etc/rc.d/rc.local <<EOF
$rsync_cmd
EOF

cat >> /etc/rc.d/rc.local <<EOF
$sersync_cmd
EOF

[ $? -eq 0 ] && Echo "success" "sersync服务安装成功,且已正常启动。"
rm -rf sersync-${sersync_version}.tar.gz
popd
}

Uninstall()
{
userdel $run_user
systemctl disable rsyncd
yum remove -y rsync
rm -rf /etc/rsyncd.conf*
rm -rf /etc/rsyncd.secret /etc/rsyncd.pass /etc/rsync_exclude.lst
pkill sersync2
rm -rf $sersync_install_dir
sed -i '/^\/usr\/local\/sersync.*/d' /etc/rc.d/rc.local
sed -i '/^rsync.*/d' /etc/rc.d/rc.local
Echo "success" "卸载完成"
}

Update()
{
Echo "msg" "暂未开通"
}

Main()
{
case $flag in
'-h' '--help' '?' )
showhelp
exit
;;
'--install' '-i' )
Install
exit
;;
'--uninstall' '-U' )
Uninstall
exit
;;
'--update' '-u' )
Update
exit
;;
* )
showhelp
exit
;;
esac
}
Main