Rsync+inotify搭建使用

## Rsync搭建
### 1.1 环境准备
```
Rsync-Server  192.168.1.174
Client-Rsync  192.168.1.173
服务启动用户都是root,客户端的用户也是root
[root@Rsync-Server file]# systemctl stop firewalld
[root@Rsync-Server file]# getenforce 
Permissive
```
### 1.1 检查是否安装rsync
```
[root@Rsync-Server ~]# rpm -qa|grep rsync
#如果没有安装Rsync
[root@Rsync-Server ~]# yum install -y rsync
```

### 1.2 服务端配置Rsync
```
[root@Rsync-Server ~]# cat /etc/rsyncd.conf 
uid = root
gid = root
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log  
[file]
path = /data/file/
ignore errors
read only = false
list = false
hosts allow = 192.168.1.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_file
secrets file = /etc/rsync.password
[root@Rsync-Server ~]# cat /etc/rsync.password 
rsync_file:123456
[root@Rsync-Server ~]# ll /etc/rsync.password
-rw-------. 1 root root 18 Oct 19 11:37 /etc/rsync.password
```

### 1.3 Rsync启动脚本
```
[root@Rsync-Server file]# cat /etc/init.d/rsyncd 
#!/bin/bash
. /etc/init.d/functions

start() {
  rsync --daemon &>/dev/null
  if [ $? = 0 ];then
     action "startting rsync" /bin/true
  else
     action "startting rsync" /bin/false
  fi
}

stop() {
  if [ -e /var/run/rsyncd.pid ];then
     kill -9 `cat /var/run/rsyncd.pid` &>/dev/null
     rm -fr /var/run/rsyncd.pid /var/run/rsync.lock
     action "stopping rsync" /bin/true
  else
     echo "the rsyncd is not running"
  fi
}

status() {
  if [ -e "/var/run/rsyncd.pid" ];then
      echo -e "\033[32m rsyncd is running... \033[0m"
  else
      echo -e "\033[31m rsyncd is stopped \033[0m"
   fi
}  

restart() {
    stop
    start
}

case $1 in 
      start)
           start
      ;;
      stop)
           stop
      ;;
      status)
           status
      ;;
      restart)
           restart
      ;;
      *)
      echo "USAG: $0 {start|stop|status|restart}"
esac
"Centos7用systemctl管理Rsync"
[root@Rsync-Server ~]# cat /usr/lib/systemd/system/rsyncd.service 
[Unit]
Description=fast remote file copy program daemon
ConditionPathExists=/etc/rsyncd.conf

[Service]
EnvironmentFile=/etc/sysconfig/rsyncd
Type=forking
PIDFile=/var/run/rsyncd.pid
ExecStart=/etc/init.d/rsyncd start
ExecReload=/etc/init.d/rsyncd restart
ExecStop=/etc/init.d/rsyncd stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
```

### 1.4 Rsync客户端
```
[root@Client-Rsync ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@Client-Rsync ~]# yum install -y inotify-tools rsync 
[root@Client-Rsync ~]# cat /etc/rsync.password 
123456
#测试一下rsync推送是否有问题
[root@Client-Rsync ~]# touch /data/file/ceshi_Test
[root@Client-Rsync ~]# rsync -avz /data/file/ rsync_file@192.168.1.174::file --password-file=/etc/rsync.password 
sending incremental file list
./
ceshi_Test

sent 172 bytes  received 46 bytes  436.00 bytes/sec
total size is 0  speedup is 0.00
没有报错证明rsync推送成功,参数说明:
-v    详细模式输出,给出传输进度等信息
-z    压缩传输  --compress-level=NUM 指定压缩级别 1-9,9是最大压缩级别
-a    以归档方式传输,保留文件属性
        -r    递归传输
        -t    保持文件时间信息
        -o    保持文件属主信息
        -p    保持文件权限
        -g    保持文件属组信息
        -P    显示同步过程及进度等信息
        -D    保持设备文件信息
        -l    保持软链接
        这些参数加起来等于 –a
--exclude=PATTERN    指定排除不需要传输的文件
--exclude-from=FILE  排除FILE中记录的文件
--delete   保证两边数据完全一样,如果源里没有该文件,就在目标目录删除
```

### 1.5 配合inotify-tools
```
[root@Client-Rsync ~]# cat inotify.sh 
#!/bin/bash
inotifywait  -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create,close_write,modify,move,attrib /data/file/ \
|while read file
do
    rsync -avz /data/file/ rsync_file@192.168.1.174::file --password-file=/etc/rsync.password 
done
inotifywait参数详解:
-m      是保持一直监听
-r      是递归查看目录
-q      是打印出事件
-e      modify,delete,create,attrib 是指"监听 创建 移动 删除 写入权限"
#启动测试
[root@Client-Rsync ~]# nohup sh inotify.sh  & 
```

### 优化
```
如果实际并发较大,可以适当的把inotify简单优化下:
ls -l /proc/sys/fs/inotify/
echo "50000000" > /proc/sys/fs/inotify/max_user_watches	加大单进程最大的文件监视数量
echo "50000000" > /proc/sys/fs/inotify/max_queued_events	加大队列可容纳的事件数量
```

  

posted @ 2018-10-19 17:33  阿进,fighting  阅读(312)  评论(0编辑  收藏  举报