centos 配置rsync+inotify數據實時同步

何爲rsync?

定義:

rsync是一個開源的快速備份工具,能夠在不一樣主機之間鏡像同步整個目錄樹,支持增量備份,保持連接和權限,很是適用於異地備份html

何爲源端和發起端?

在遠程同步過程當中,負責發起rsync同步操做的客戶機稱爲++發起端++,而負責響應來及客戶機的rsync同步操做的服務器爲++同步源++。在同步過程當中,同步源負責提供文檔的原始位置,而發起端對該位置具備讀寫權限。nginx

rsync命令的基本用法:
備份的基本格式:「rsync [選項] 原始位置 目標位置」

-v, --verbose 詳細模式輸出 -q, --quiet 精簡輸出模式 -c, --checksum 打開校驗開關,強制對文件傳輸進行校驗 -a, --archive 歸檔模式,表示以遞歸方式傳輸文件,並保持全部文件屬性,等於-rlptgoD -r, --recursive 對子目錄以遞歸模式處理 -R, --relative 使用相對路徑信息 -b, --backup 建立備份,也就是對於目的已經存在有一樣的文件名時,將老的文件從新命名爲~filename。可使用--suffix選項來指定不一樣的備份文件前綴 --backup-dir 將備份文件(如~filename)存放在在目錄下 -suffix=SUFFIX 定義備份文件前綴 -u, --update 僅僅進行更新,也就是跳過全部已經存在於DST,而且文件時間晚於要備份的文件,不覆蓋更新的文件 -l, --links 保留軟鏈結 -L, --copy-links 想對待常規文件同樣處理軟鏈結 --copy-unsafe-links 僅僅拷貝指向SRC路徑目錄樹之外的鏈結 --safe-links 忽略指向SRC路徑目錄樹之外的連接 -H, --hard-links 保留硬鏈結 -p, --perms 保持文件權限 -o, --owner 保持文件屬主信息 -g, --group 保持文件屬組信息 -D, --devices 保持設備文件信息 -t, --times 保持文件時間信息 -S, --sparse 對稀疏文件進行特殊處理以節省DST的空間 -n, --dry-run現實哪些文件將被傳輸 -w, --whole-file 拷貝文件,不進行增量檢測 -x, --one-file-system 不要跨越文件系統邊界 -B, --block-size=SIZE 檢驗算法使用的塊尺寸,默認是700字節 -e, --rsh=command 指定使用rsh、ssh方式進行數據同步 --rsync-path=PATH 指定遠程服務器上的rsync命令所在路徑信息 -C, --cvs-exclude 使用和CVS同樣的方法自動忽略文件,用來排除那些不但願傳輸的文件 --existing 僅僅更新那些已經存在於DST的文件,而不備份那些新建立的文件 --delete 刪除那些DST中SRC沒有的文件 --delete-excluded 一樣刪除接收端那些被該選項指定排除的文件 --delete-after 傳輸結束之後再刪除 --ignore-errors 及時出現IO錯誤也進行刪除 --max-delete=NUM 最多刪除NUM個文件 --partial 保留那些因故沒有徹底傳輸的文件,以是加快隨後的再次傳輸 --force 強制刪除目錄,即便不爲空 --numeric-ids 不將數字的用戶和組id匹配爲用戶名和組名 --timeout=time ip超時時間,單位爲秒 -I, --ignore-times 不跳過那些有一樣的時間和長度的文件 --size-only 當決定是否要備份文件時,僅僅察看文件大小而不考慮文件時間 --modify-window=NUM 決定文件是否時間相同時使用的時間戳窗口,默認爲0 -T --temp-dir=DIR 在DIR中建立臨時文件 --compare-dest=DIR 一樣比較DIR中的文件來決定是否須要備份 -P 等同於 --partial --progress 顯示備份過程 -z, --compress 對備份的文件在傳輸時進行壓縮處理 --exclude=PATTERN 指定排除不須要傳輸的文件模式 --include=PATTERN 指定不排除而須要傳輸的文件模式 --exclude-from=FILE 排除FILE中指定模式的文件 --include-from=FILE 不排除FILE指定模式匹配的文件 --version 打印版本信息 --address 綁定到特定的地址 --config=FILE 指定其餘的配置文件,不使用默認的rsyncd.conf文件 --port=PORT 指定其餘的rsync服務端口 --blocking-io 對遠程shell使用阻塞IO -stats 給出某些文件的傳輸狀態 --progress 在傳輸時現實傳輸過程 --log-format=formAT 指定日誌文件格式 --password-file=FILE 從FILE中獲得密碼 --bwlimit=KBPS 限制I/O帶寬,KBytes per second -h, --help 顯示幫助信息

如今向你們演示rsync+inotify實時同步的配置

準備條件:centos 7.4版本 同步源端ip:192.168.100.129 發起端ip: 192.168.100.130 [root@localhost ~]# rpm -q rsync //系統默認已安裝 rsync-3.0.9-18.el7.x86_64

1、配置原服務器

一、修改/etc/rsyncd.conf配置文件
[root@localhost ~]# vim /etc/rsyncd.conf uid = nobody gid = nobody use chroot = yes //禁錮在源目錄 address = 192.168.100.129 //監聽本地端口 port 873 //監聽端口 log file = /var/log/rsyncd.log //日誌文件位置 pid file = /var/run/rsyncd.pid //進程id存放位置 hosts allow = 192.168.100.0/24 //容許訪問的地址段 [wwwroot] //共享模塊名稱 path = /var/www/html //源目錄路徑 comment = www.kgc.cn //描述信息 read only = yes //只讀,同步時需改成「no」 dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 auth users = backuper //受權帳戶 secrets file = /etc/rsyncd_users.db //存放帳戶信息的數據文件
二、爲備份帳戶建立數據文件,對須要進行同步備份的帳戶進行認證,避免信息泄露
[root@localhost ~]# vim /etc/rsyncd_users.db backuper:abc123 //添加密碼,這樣發起端發起同步須要此密碼認證 [root@localhost ~]# chmod 600 /etc/rsyncd_users.db //賦予讀寫權限
三、安裝httpd服務,後面須要在/var/www/html裏添加文件測試
[root@localhost ~]# yum install httpd -y [root@localhost ~]# ls -ld /var/www/html/ drwxr-xr-x. 2 root root 6 6月 27 21:49 /var/www/html/ [root@localhost ~]# cd /var/www/ [root@localhost www]# ll 總用量 0 drwxr-xr-x. 2 root root 6 6月 27 21:49 cgi-bin drwxr-xr-x. 2 root root 6 6月 27 21:49 html [root@localhost www]# chmod 777 html //賦予最大權限,不然會同步失敗 [root@localhost www]# ll 總用量 0 drwxr-xr-x. 2 root root 6 6月 27 21:49 cgi-bin drwxrwxrwx. 2 root root 6 6月 27 21:49 html
四、開啓同步源服務
[root@localhost www]# rsync --daemon //啓動rsync服務 [root@localhost www]# netstat -antp |grep rsync tcp 0 0 192.168.100.129:++873++ 0.0.0.0:* LISTEN 4254/rsync //默認端口:873 重啓操做:先kill進程,再到/var/run/rsyncd.pid中刪除pid文件,再啓動服務完成重啓,不然會失敗 [root@localhost html]# systemctl stop firewalld.service [root@localhost html]# setenforce 0 //關閉防火牆和網絡加強×××
五、在rsync同步源端進行備份操做
[root@localhost www]# cd html/ [root@localhost html]# touch 111.txt [root@localhost html]# rsync -avz backuper@192.168.100.129::wwwroot /opt/ Password: receiving incremental file list ./ 111.txt [root@localhost html]# cd /opt [root@localhost opt]# ls 111.txt //顯示備份成功 

2、配置發起端

一、調整inotify內核參數
vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384 //監控事件隊列 fs.inotify.max_user_instances = 1024 //最多監控實例數 fs.inotify.max_user_watches = 1048576 //最多監控文件數
[root@localhost html]# systemctl stop firewalld.service [root@localhost html]# setenforce 0 //關閉防火牆和網絡加強×××
二、安裝inotify-tools,版本爲3.14,須要可找我
[root@localhost abc]# mount.cifs //192.168.100.1/mem /abc [root@localhost abc]# tar zxvf inotify-tools-3.14.tar.gz -C /opt/ [root@localhost opt]# cd /opt/inotify-tools-3.14/ [root@localhost inotify-tools-3.14]# ./configure [root@localhost inotify-tools-3.14]# make && make install
三、發起端安裝httpd服務
[root@localhost ~]# yum install httpd -y [root@localhost ~]# ls -ld /var/www/html/ drwxr-xr-x. 2 root root 6 6月 27 21:49 /var/www/html/ [root@localhost www]# chmod 777 html //賦予最大權限,不然會同步失敗 [root@localhost www]# ll 總用量 0 drwxr-xr-x. 2 root root 6 6月 27 21:49 cgi-bin drwxrwxrwx. 2 root root 6 6月 27 21:49 html
四、設置同步驗證
vim /etc/server.pass abc123 //和源端的驗證密碼需一致 [root@localhost ~]# chmod 600 /etc/server.pass
五、測試同步,到源端修改配置文件
[root@localhost html]# vim /etc/rsyncd.conf read only =yes //這裏將yes改成no,賦予寫的權限 [root@localhost html]# netstat -antp |grep rsync tcp 0 0 192.168.100.129:873 0.0.0.0:* LISTEN 4254/rsync [root@localhost html]# kill -9 4254 [root@localhost run]# cd /var/run [root@localhost run]# rm -rf rsyncd.pid [root@localhost run]# rsync --daemon [root@localhost run]# netstat -antp |grep rsync tcp 0 0 192.168.100.129:873 0.0.0.0:* 注:修改配置文件後,源端需重啓:先kill進程,再到/var/run/rsyncd.pid中刪除pid文件
六、在發起端開啓監控,前提是同步源已經開啓服務
[root@localhost ~]# inotifywait -mrq -e modify,create,move,delete /var/www/html/ (-e:指定監控事件;-m:持續監控;-r;遞歸整個目錄;-q:簡化輸出信息

算法

七、在發起端編寫觸發式腳本同步
[root@localhost html]# vim /opt/inotify.sh #!/bin/bash INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/" RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ backuper@192.168.100.129::wwwroot/" //同步源的ip $INOTIFY_CMD | while read DIRECTORY EVENT FILE do if [ $(pgrep rsync | wc -l) -le 0 ] ; then $RSYNC_CMD fi done [root@localhost html]#chmod +x /opt/inotify.sh //賦予執行權限
八、在發起端/var/www/html/目錄下添加文件測試
發起端:

[root@localhost html]# touch 1.txt abc.txt [root@localhost html]# ls 1.txt abc.txt [root@localhost html]# rm -rf 1.txt 源端查看: [root@localhost html]# ls 1.txt abc.txt [root@localhost html]# ls abc.txt //同步成功
相關文章
相關標籤/搜索