cd /usr/local/
wget https://rsync.samba.org/ftp/rsync/src/rsync-3.1.1.tar.gz
tar -zxvf rsync-3.1.1.tar.gz
cd rsync-3.1.1
./configure --prefix=/usr/local/rsync && make && make install
安装好后
cd /usr/local/rsync
然后在当前目录下创建log、pid、conf、password 分别存放日志、程序进程号、配置文件、密码
mkdir log -p
mkdir pid -p
mkdir conf -p
mkdir password -p
创建配置文件
cd conf
nano -w rsyncd.conf
写入
use chroot = nomax
strict modes = false
connections = 10
pid file = /usr/local/rsync/pid/rsyncd.pid
log file = /usr/local/rsync/log/rsyncd.log
[www]
uid = root
gid = root
path = /usr/local/rsync/sync/
comment = wwwbackup
list = yes
ignore errors
read only = false
write only = false
hosts allow = *
#auth users = root
#secrets file = /usr/local/rsync/password/server.pass
安装参考:https://yq.aliyun.com/articles/486255
步骤一、不用密码上传文件到其它服务器参考
https://jingyan.baidu.com/article/e73e26c083555b24adb6a7e8.html
现在我们来配置ssh使得通过ssh执行rsync时不再索要密码。在本地服务器上使用ssh-keygen生成公钥和秘钥,如下命令:
$ ssh-keygen
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
注意:当它要求您输入passphrase按下enter键,不需要给任何密码
然后使用ssh-copy-id命令将公钥拷贝至远程主机
ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.200.10
注:执行以上操作时会要求你输入远程主机帐户和密码,然后就会自动将公钥拷贝至远程目录。
如果提示错误信息:/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
说明已经存在一个key在该主机上,删除它,再重新执行拷贝上去
删除:
ssh-keygen -f "/root/.ssh/known_hosts" -R 192.168.200.10
上传:
/usr/local/rsync/sync# /usr/local/rsync/bin/rsync -avz -e ssh /usr/local/rsync/sync/cc.txt root@服务器IP:/usr/local/rsync/sync/
下载同步
/usr/local/rsync/sync# /usr/local/rsync/bin/rsync -vzrtopg /usr/local/rsync/sync/cc.txt root@服务器IP:/usr/local/rsync/sync/
使用 inotifywait 脚本进行监控,先安装inotifywait
nano -w up.sh
然后写入下面
#!/bin/bash
/usr/local/inotify/bin/inotifywait -mrq --format '%w%f' -e close_write /opt/lampp/htdocs/ckpv/ | while read file
do
/usr/local/rsync/bin/rsync -avz -e ssh $file root@17.18.17.113:$file
/usr/local/rsync/bin/rsync -avz -e ssh $file root@17.18.17.114:$file
/usr/local/rsync/bin/rsync -avz -e ssh $file root@17.18.17.117:$file
/usr/local/rsync/bin/rsync -avz -e ssh $file root@17.18.17.119:$file
done
注:即用监控/opt/lampp/htdocs/ckpv/下文件改动,则同步到对应服务器相同文件路径下
运行脚本
./up.sh
改动一下监控的服务器,就会自动同步了,注意,要在步骤一中给对应服务设置密钥