歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux管理 >> Linux問題解決 >> Linux和windows系統文件的實時同步

Linux和windows系統文件的實時同步

日期:2017/3/6 10:16:56   编辑:Linux問題解決

使用軟件Rsync和Inotify,實現多服務器,Linux和windows系統文件的實時同步
兩台Linux 主機之間文件實時同步
主機A: 192.168.254.201
主機B: 192.168.254.202
實驗目的,實現在主機B上做修改,主機A上做到實時同步
1.1 主機A上的配置
1.1.1建立需要同步文件夾
Mkdir /opt/rsync
1.1.2安裝Rsync
Emerge rsync
我用的Gentoo,默認已經安裝,配置文件在/etc/rsyncd.conf
1.1.3修改配置文件
Nano –w /etc/rsyncd.conf
# Globel setting
uid = root
gid = root
use chroot =no
max connectiongs = 10
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsync.og

# Folder Setting,Permit add more folder
[images]
path = /opt/image/
read only = no
auth users = rsyncuser
secrets file = /etc/rsyncd.secrets
1.1.4 寫驗證文件
Nano –w /etc/rsyncd.secrets

rsyncuser:Password01

Chown -R 600 /etc/rsyncd.secrets
1.1.5 啟動服務
Rsync --daemon --config=/etc/rsyncd.conf
Echo –en ‘rsync –daemon –config=/etc/rsyncd.conf’ >> /etc/rc.local

1.2 主機B上面的配置
1.2.1 建立相同的目錄
Mkdir /opt/rsync
1.2.2安裝rsync和inotify
Emerge rsync
Emerge inotify-tools
1.2.3 建立訪問密碼
Echo –en ‘Password01’ > /etc/rsyncd.secrets
Chmod 600 /etc/rsyncd.secrets
1.2.4配置同步腳本
nano -w /root/InotifyRsync.sh
#!/bin/bash
host_1=192.168.254.201
src=/opt/image/
dst=images
user=valeRsync
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e

modify,delete$
do
rsync -avzP --delete --password-file=/etc/rsyncd.secrets $src $
echo "${file} was rsynced"
done
exit 0
1.2.5 測試運行
echo -ne ‘/root/InotifyRsync.sh &’ >> /etc/rc.local
Sh /root/InotifyRsync.sh
另外開一個終端在/opt/rsync下做測試,檢查A 主機是否有同步。
1.2.6 inotify支持的屬性

IN_ACCESS,即文件被訪問

IN_MODIFY,文件被 write
IN_ATTRIB,文件屬性被修改,如 chmod、chown、touch 等
IN_CLOSE_WRITE,可寫文件被 close
IN_CLOSE_NOWRITE,不可寫文件被 close
IN_OPEN,文件被 open
IN_MOVED_FROM,文件被移走,如 mv
IN_MOVED_TO,文件被移來,如 mv、cp
IN_CREATE,創建新文件
IN_DELETE,文件被刪除,如 rm
IN_DELETE_SELF,自刪除,即一個可執行文件在執行時刪除自己
IN_MOVE_SELF,自移動,即一個可執行文件在執行時移動自己
IN_UNMOUNT,宿主文件系統被 umount
IN_CLOSE,文件被關閉,等同於(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
IN_MOVE,文件被移動,等同於(IN_MOVED_FROM | IN_MOVED_TO

Copyright © Linux教程網 All Rights Reserved