![QQ截图20171221160427.png](http://www.xwblog.club/usr/uploads/2017/12/591930742.png) 服务端 #this is rsyncdaemon script #author xw 2017.12.21 #!/bin/bash mkdir /backup/ filedir=/backup if rpm -qa |grep rsync |wc -l > /dev/null;then yum install -y rsync fi echo $? ##检查rsync安装包是否存在 sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config service iptables stop setenforce 0 chkconfig iptables off ##安装准备 useradd rsync -s /sbin/nologin -M chown rsync.rsync $filedir cat > /etc/rsyncd.conf</etc/rsync.password chmod 600 /etc/rsync.password ##权限必须为600,否则日志会出现报错 rsync --daemon ##此处使用daemon方式启动,当然也可以用脚本实现,脚本附下文 grep rsync /etc/rc.local|| echo '/usr/bin/rsync --daemon' >> /etc/rc.local echo $? 客户端 #this is rsyncdaemon clinet script #author xw 2017.12.21 #!/bin/bash mkdir /backup/ filedir=/backup if rpm -qa |grep rsync |wc -l > /dev/null;then yum install -y rsync fi echo $? ##检查rsync安装包是否存在 sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config service iptables stop setenforce 0 chkconfig iptables off ##安装准备 echo “123456” >etc/rsync.password chmod 600 /etc/rsync.password cd /backup && touch zzzz{1..500} ##进入backup目录创建文件 echo "*/5 * * * * root ./rsyncclient.sh /dev/null 2>&1">/etc/crontab /etc/rc.d/init.d/crond restart ##定时任务建好 echo "rsync -avz /$filedir/ rsync_backup@192.168.187.129::backup --password-file=/etc/rsync.password">rsyncclient.sh chmod +x rsyncclient.sh ./rsyncclient.sh echo $? 服务端 rsync 启动脚本 ##author xw 2017.12.21 #!/bin/bash #chkconfig: 2345 20 80 #description: create by vincen . /etc/init.d/functions function usage(){ echo $"usage:$0 {start|stop|restart}" exit 1 } function start(){ rsync --daemon sleep 1 if [ `netstat -lntup|grep rsync|wc -l` -ge 1 ];then action "rsyncd is started." /bin/true else action "rsyncd is started." /bin/false fi } function stop(){ killall rsync sleep 1 if [ `netstat -lntup|grep rsync|wc -l` -eq 0 ];then action "rsyncd is stopped." /bin/true else action "rsync is started." /bin/false fi } function main(){ if [ $# -ne 1 ];then usage fi if [ "$1" == "start" ];then start elif [ "$1" == "stop" ];then stop elif [ "$1" == "restart" ];then stop sleep 1 start else usage fi } main $*