---------------------------------------------------------

rsync小结


1,客户端配置

配置文件:/etc/rsync_client/rsyncd.conf

uid=root

gid=root

use chroot = no

max connections = 5

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

log file = /var/log/rsyncd.log

Timeout = 300

Log format = %t %a %m %f %b


密码文件:权限必须600

/etc/rsync_client/rsyncd.pass

password123


2个文件存在即可,作为客户端,rsync服务无需启动。


2,服务端配置

配置文件:/etc/rsync_serv/rsyncd.conf

uid=root

gid=root

use chroot = no

max connections = 5

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

log file = /var/log/rsyncd.log

Timeout = 300

Log format = %t %a %m %f %b


[nginx]#模块

path=/usr/local/nginx#路径

ignore errors

read only = no#需要写入权限

list = no

auth users = user123#连接认证使用的帐号

secrets file = /etc/rsync_serv/rsyncd.pass#连接认证使用的密码文件,权限必须600

hosts allow = 192.168.1.103#允许连接的客户端

hosts deny = 0.0.0.0/0#拒绝连接的IP


密码文件:/etc/rsync_serv/rsyncd.pass

user123:password123#密码信息需与客户端配置的密码一致


作为服务端,需要启动服务:

rsync --daemon --config=/etc/rsync_serv/rsyncd.conf


3,同步nginx目录的下的web文件,在客户端上执行

rsync -va --progress --password-file=/etc/rsync_client/rsyncd.pass html/* [email protected]::nginx/html/

如果是同步目录,需要在目录后面加上/

rsync -va --progress --password-file=/etc/rsync_client/rsyncd.pass conf/ [email protected]::nginx/conf


---------------------------------------------------------

实时同步脚本


#!/bin/sh

#

DESTHOST=192.168.1.108

SRCDIR=/usr/local/nginx/conf/


# --format '%T %w %f %e' 定义的变量即为while后面引用的变量

# %T 时间格式

# %w 路径

# %f 文件

# %e 事件

inotifywait -mr --timefmt '%Y-%m-%d_%H:%M:%S' --format '%T %w %f %e' -e close_write,modify,delete,create,attrib $SRCDIR |  while read DATE DIR FILE EVENTS

# while后面申明的变量即对应上面格式化后的变量

do

#echo "date: $DATE" >> /var/log/rsync-action.log

#echo "time: $TIME" >> /var/log/rsync-action.log

#echo "dir: $DIR" >> /var/log/rsync-action.log

#echo "file: $FILE" >> /var/log/rsync-action.log

#echo "--------" >> /var/log/rsync-action.log

  FILE2=${DIR}${FILE}

  EVENT=`echo ${EVENTS} | awk '{print $1}'`        #可以在此针对不同的事件做不同的处理

  rsync -vaq --password-file=/etc/rsync_client/rsyncd.pass $SRCDIR [email protected]::nginx/conf

  echo "At ${DATE} on ${DESTHOST}, file $FILE2 was ${EVENT} via 103 rsync. " >> /var/log/rsync-action.log

  #将变更信息邮件通知到管理员或者查询后台

  #可关联邮件系统或者日志系统进行分析

  # ...

done



---------------------------------------------------------

PS:需要单独安装inotify-tools工具