#!/bin/bash
# FileName: rsyncd.sh
# Author: Shuhui

AUTH_USER='www'                                             # 认证用户
AUTH_PASS='1c790a002393c3e022d50a917a3d26fafedf842'         # 认证密码
AUTH_FILE='/etc/rsyncd.auth'                                # 认证文件

PORT=873                                                    # 监听端口
RSYNC_DIR='/data/www/product'                               # 同步目录
MOD_NAME='data'                                             # 同步模块名称

ALLOW_IP='192.168.20.0/24'                                  # 允许同步的IP

if [[ ${UID} -ne '0' ]] 
then
	echo 'Error: Please use root role run this script.'
	exit 67
fi

yum -y install rsync

[[ -e /etc/rsyncd.conf ]] && \mv /etc/rsyncd.conf /etc/rsyncd.conf.default
cat > /etc/rsyncd.conf <<EOF
# filename:/etc/rsyncd.conf
 
#address = 127.0.0.1
port = ${PORT}

uid = ${AUTH_USER}
gid = ${AUTH_USER}

use chroot = yes
max connections = 10
strict modes = yes

pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log

[${MOD_NAME}]
path = ${RSYNC_DIR}
comment = Rsync Directory
ignore errors
read only = no
write only = no
hosts allow = ${ALLOW_IP}
hosts deny = *
list = false

# Use Auth
auth users = ${AUTH_USER}
secrets file = ${AUTH_FILE}
EOF

echo "${AUTH_USER}:${AUTH_PASS}" | tee ${AUTH_FILE}
chmod 600 ${AUTH_FILE}

systemctl restart rsyncd.service
systemctl enable rsyncd.service

echo "Usage: rsync -vzrtopg --progress --delete  --password srcDir ${AUTH_USER}@$(hostname -I | awk '{print $1}')::${MOD_NAME}"
