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	加大隊列可容納的事件數量
```
相關文章
相關標籤/搜索