同步文件,相当于cp,可以用在一个机器中或是不同机器间。
非操作方
不同机器间同步时,非操作方配置/etc/rsyncd.conf文件,更新配置文件不需要重启进程。
配置示例:本记ip:10.1.1.1,允许来自10.1.1.*、10.1.2.3和10.1.3.3的机器pull或push文件,非操作方路径在/path/to/store/file中。
[push_demo]
path = /path/to/push/file
hosts allow=10.1.1.2/255.255.255.0 10.1.2.3 10.1.3.3
read only = no
[pull_demo]
path = /path/to/pull/file
hosts allow=10.1.1.3
read only = no
启动服务进程
rsync --daemon
当接收到同名文件时,前一次收到的文件会按照发送时指定到后缀重命名,当前收到到文件名称不变。当再次收到同名文件后,上上次收到的文件被删除,上次收到的文件加后缀重命名,当前收到的文件名称不变。以后同上。
操作方
push
rsync /path/to/file non-operate_ip::example
rsync -avz --suffix=.old --backup non-operate_ip::example
同步本地/data1/path目录中,上一个小时的日志,日志格式business.log.2018-11-16-17。
push到非操作方文件名:operate_ip.business.log.2018-11-16-17
遇到同名文件时,前一文件加后缀.old
pre_hour=`date -d '-1 hours' '+%Y-%m-%d-%H'`
ip=`/usr/sbin/ifconfig eth0|grep 'inet'|grep '10.'|awk '{print $2}'`
cd /data1/path
find ./ -name "business.log.$pre_hour" |xargs -I {} rsync -avz --suffix=.old --backup {} 10.3.1.1::push_demo/$ip.business.log.$pre_hour
pull
本级ip:10.1.1.3,把非操作方/path/to/pull/file中以.txt结尾到文件同步到操作方当前路径
rsync 10.1.1.1::pull_demo/path/to/pull/file/*.txt ./