Rsync+inotify搭建使用
生活随笔
收集整理的這篇文章主要介紹了
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/functionsstart() {rsync --daemon &>/dev/nullif [ $? = 0 ];thenaction "startting rsync" /bin/trueelseaction "startting rsync" /bin/falsefi
}stop() {if [ -e /var/run/rsyncd.pid ];thenkill -9 `cat /var/run/rsyncd.pid` &>/dev/nullrm -fr /var/run/rsyncd.pid /var/run/rsync.lockaction "stopping rsync" /bin/trueelseecho "the rsyncd is not running"fi
}status() {if [ -e "/var/run/rsyncd.pid" ];thenecho -e "\033[32m rsyncd is running... \033[0m"elseecho -e "\033[31m rsyncd is stopped \033[0m"fi
} restart() {stopstart
}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_Testsent 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
dorsync -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 加大隊列可容納的事件數量
```
轉載于:https://www.cnblogs.com/so-cool/p/9817736.html
總結
以上是生活随笔為你收集整理的Rsync+inotify搭建使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 引入样式
- 下一篇: Spring Cloud -Zuul