rsync+inotify配置例項
阿新 • • 發佈:2018-11-03
需求說明
部署rsync+inotify同步/lzh目錄至目標伺服器的/tmp/lzh下
環境說明
伺服器型別 | ip地址 | 應用 |
---|---|---|
源伺服器 | 172.16.11.11 | rsync,inotify-tools,指令碼 |
目標伺服器 | 172.16.11.12 | rsync |
①.關閉防火牆
[[email protected] ~]# systemctl stop firewalld.service
②.關閉selinux
[[email protected] ~]# setenforce 0
[[email protected] ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
操作步驟
- 目標伺服器
①.安裝rsync
[[email protected] ~]# yum -y install rsync
②.修改/etc/rsync.dconf配置檔案,建立備份目的資料夾
[[email protected] ~]# cat >> /etc/rsyncd.conf << EOF
> log file = /var/log/rsyncd.log
> pidfile = /var/run/rsyncd.pid
> lock file = /var/run/rsyncd.lock
> secrets file = /etc/rsync.mima
>
> [xixi]
> path = /tmp/lzh
> comment = sync xixi
> uid = root
> gid = root
> port = 873
> ignore errors
> use chroot = no
> read only = no
> list = no
> max connections = 100
> timeout = 100
> auth users = xixi
> EOF
[ [email protected] ~]# mkdir /tmp/lzh
③.建立使用者認證密碼檔案,並保證安全性修改許可權為600
[[email protected] ~]# echo 'xixi:123' > /etc/rsync.mima
[[email protected] ~]# chmod 600 /etc/rsync.mima
[[email protected] ~]# ll /etc/rsync.mima
-rw-------. 1 root root 9 Sep 18 21:49 /etc/rsync.mima
④.重啟服務並設定開機啟動
[ [email protected] ~]# systemctl start rsyncd
[[email protected] ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
[[email protected] ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 5 *:873 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 5 :::873 :::*
- 源伺服器
①.因為個別包本地源沒有,需要配置網路源
[[email protected] ~]# cd /etc/yum.repos.d/
[[email protected] yum.repos.d]# yum -y install wget.x86_64
[[email protected] yum.repos.d]# wget http://mirrors.aliyun.com/repo/Centos-7.repo
[[email protected] yum.repos.d]# sed -i 's/$releasever/7/g' Centos-7.repo
[[email protected] yum.repos.d]# yum clean all
②.安裝epel-release,rsync,inotify-tools
[[email protected] ~]# yum -y install rsync.x86_64
[[email protected] ~]# yum -y install epel-release.noarch
[[email protected] ~]# yum -y install inotify-tools
③.建立密碼檔案並修改許可權,
[[email protected] ~]# echo '123' >> /etc/rsync.mima
[[email protected] ~]# chmod 600 /etc/rsync.mima
[[email protected] ~]# ll /etc/rsync.mima
-rw-------. 1 root root 4 Sep 18 22:01 /etc/rsync.mima
④.建立需要備份到目標伺服器的目錄
[[email protected] ~]# mkdir /lzh/
5.寫自動同步的指令碼,然後啟動指令碼並放至後臺
[[email protected] ~]# mkdir /scripts
[[email protected] ~]# vim /scripts/inotify.sh
host=172.16.11.12
src=/lzh
des=xixi
password=/etc/rsync.mima
user=xixi
inotifywait=/usr/bin/inotifywait
$inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files ; do
rsync -avzP --delete --timeout=100 --password-file=${password} $src [email protected]$host::$des
echo "${files} was rsynced" >> /tmp/rsync.log 2>&1
done
[[email protected] ~]# chmod 600 /scripts/inotify.sh
[[email protected] ~]# nohup bash /scripts/inotify.sh &
測試結果
在源伺服器上建立和修改,檢視日誌以及在目標伺服器上檢視
- 源伺服器
[[email protected] ~]# ls /lzh/
[[email protected] ~]# echo 'xixihaha' > /lzh/hehe
[[email protected] ~]# tail /tmp/rsync.log
20180918 23:24 /lzh/heheDELETE was rsynced
20180918 23:24 /lzh/heheCREATE was rsynced
20180918 23:24 /lzh/heheMODIFY was rsynced
- 目標伺服器
[[email protected] ~]# ls /tmp/lzh/
lzh
[[email protected] ~]# cat /tmp/lzh/lzh/hehe
xixihaha
設定指令碼開機啟動
[[email protected] ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local
[[email protected] ~]# chmod +x /etc/rc.d/rc.local