inotify和rsync實現實時同步

1.搭建rsync服務端
systemctl start rsyncd
systemctl enable rsyncd
systemctl status rsyncd
ss -ntl |grep 873
rpm -ql rsync
2.修改配置文件
vi /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 0
ignore errors
exclude = lost+found/
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
reverse lookup = no
hosts allow = 192.168.8.0/24 #客戶端IP
[backup]
path = /backup/
comment = backup
read only = no
auth users = rsyncuser
secrets file = /etc/rsync.passvim

3.建立rsync備份服務器目錄
mkdir /backupbash

服務器端生成驗證文件
echo "rsyncuser:magedu" > /etc/rsync.pass
chmod 600 /etc/rsync.pass
 服務器端準備目錄
mkdir /backup
服務器端啓動rsync服務
rsync --daemon 可加入/etc/rc.d/rc.local實現開機啓動
systemctl start rsyncd CentOS 7
客戶端配置密碼文件
echo "magedu" > /etc/rsync.pass
chmod 600 /etc/rsync.pass
客戶端
開啓inotify
yum -y install inotify-tools
4.客戶端測試同步數據
rsync -avz --password-file=/etc/rsync.pass /data/backup rsyncuser@rsync服務器IP::backup服務器

5.客戶端建立inotify_rsync.sh腳本
vim inotify_rsync.sh br/>#!/bin/bash
SRC='/data/backup/'
DEST='rsyncuser@rsync服務器IP::backup'
inotifywait -mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %w %f' -e create,delete,moved_to,close_write ${SRC} |while read DATE TIME DIR FILE;
do
FILEPATH=${DIR}${FILE}
rsync -az --delete --password-file=/etc/rsync.pass $SRC $DEST &&
echo "At ${TIME} on ${DATE}, file $FILEPATH was backuped up via rsync" markdown

/var/log/changelist.log
donessh

chmod +x inotify_rsync.sh
開啓後臺監控進程ide

6.sh inotify_rsync.sh &
ps -ef |grep inotify_rsync
注意:兩臺主機必須開啓ssh免密登陸,容許客戶端把數據被分到rsync服務器上.測試

相關文章
相關標籤/搜索