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

作者: ropon 分类: 分布式集群 发布时间: 2018-04-18 21:37

#!/bin/bash

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://luopengtest.gotoip2.com/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”

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
}

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
}

if [ ! $1 ];then
Echo “msg” “缺少必要参数”
Echo “msg” “安装请使用Create,如sh $(basename $0) Install或sh $(basename $0) -I”
Echo “msg” “卸载请使用Uninstall,如sh $(basename $0) Uninstall或sh $(basename $0) -U”
exit
else
flag=$1
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” “卸载完成”
}

Main()
{
([ “$flag” == “Install” ] || [ “$flag” == “-I” ]) && Install
([ “$flag” == “Uninstall” ] || [ “$flag” == “-U” ]) && Uninstall
}
Main

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注