1. 程式人生 > >inotify+rsync安裝配置

inotify+rsync安裝配置

服務器 while ble div In -s light ati spinner

環境 系統 IP地址
主服務器 CentOS7.4 192.168.1.1
備份服務器 CentOS7.4 192.168.1.2

一、備份服務器

安裝rsync(備)

wget https://rsync.samba.org/ftp/rsync/src/rsync-3.1.3.tar.gz
tar -xf rsync-3.1.3.tar.gz
./configure --prefix=/usr/local/rsync 
make && make install 

配置rsyncd.conf

cat <<EOF> /etc/rsyncd.conf 
pid file = /var/run/rsync.pid  
log file = /var/log/rsync.log 
lock file=/var/run/rsync.lock  
secrets file = /etc/rsync.pw  
motd file = /etc/rsyncd.motd  
transfer logging = yes  
log format = %t %a %m %f %b  
syslog facility = local3  
[data]  
path = /data/test/
comment = data
exclude = 
port = 873
uid = root
gid = root
timeout = 600
max connections = 200
use chroot = no
read only = no
list = no
hosts allow = 192.168.1.1
EOF

可以設置多個目錄

#增加test1目錄  
[test1]  
path = /data/test1  
list = yes  
ignore errors  
comment = ucweb-file system  
secrets file = /etc/rsync.pw  
exclude = blank.png ; spinner.gif ; downsimple.png ; rails.png ; WEB-INF/  

建立密碼認證文件

cat <<EOF> /etc/rsync.pw  
root:123456  
EOF

配置rsyncd.motd文件,開始傳送的時候會顯示

cat <<EOF> /etc/rsyncd.motd  
###############################  
#                             #  
#        start rsync          #  
#                             #  
###############################  
EOF

啟動rsync

/usr/local/rsync/bin/rsync --daemon --config=/etc/rsyncd.conf  

開機啟動rsync

echo ‘/usr/local/rsync/bin/rsync --daemon --config=/etc/rsyncd.conf‘>>/etc/rc.d/rc.localer 

二、主服務器

建立密碼認證文件

cat <<EOF> /etc/rsync.pw  
123456
EOF

測試開始

/usr/local/rsync/bin/rsync -avH --port=873 --progress --delete /data/test/ [email protected]::data --password-file=/etc/rsync.pw

查看192.168.1.2上是否有同步

安裝inotify-tools

yum install inotify-tools -y

新建inotify.sh文件同步

#!/bin/sh  
# get the current path  
CURPATH=`pwd`  
/usr/bin/inotifywait -mr --timefmt ‘%d/%m/%y %H:%M‘ --format ‘%T %w %f‘ -e close_write /data | while read date time dir file; do  
       FILECHANGE=${dir}${file}  
       # convert absolute path to relative  
       FILECHANGEREL=`echo "$FILECHANGE" | sed ‘s_‘$CURPATH‘/__‘`  

       /usr/bin/rsync -avH --port=873 --progress --delete /data/test/ [email protected]::data --password-file=/etc/rsync.pw 
       echo "At ${time} on ${date}, file $FILECHANGE was backed up via rsync"  
done  


註意:

如果僅僅是備份的話,不要帶delete參數

參考鏈接

https://blog.csdn.net/liuzheng0915/article/details/52003942

inotify+rsync安裝配置