- rsync簡介
rsync是linux系統下的數據鏡像備份工具。使用快速增量備份工具Remote Sync可以遠程同步,支持本地複製,或者與其他SSH、rsync主機同步。
- Inotify簡介
Inotify 是一個 Linux特性,它監控文件系統操作,比如讀取、寫入和創建。Inotify 反應靈敏,用法非常簡單,並且比 cron 任務的繁忙輪詢高效得多。學習如何將 inotify 集成到您的應用程序中,並發現一組可用來進一步自動化系統治理的命令行工具。
主機名稱 | IP | 主要軟件 |
---|---|---|
服務器A(rsync源) | 192.168.100.21 | rsync |
服務器B(rsync客戶機發起端) | 192.168.100.22 | gcc gcc-c++ inotify rsync |
- 配置服務器A
- 修改配置文件
# vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes //禁錮在源目錄
address = 192.168.100.21 //監聽地址
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.yun.cn
read only = no //是否爲只讀
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 //同步時 不再壓縮的文件類型
auth users = backuper //授權賬戶
secrets file = /etc/rsyncd_users.db //存放賬戶信息的數據文件
- 爲備份賬戶創建數據文件
# vim /etc/rsyncd_users.db backuper:abc123 # chmod 600 /etc/rsyncd_users.db # mkdir -p /var/www/html #創建同步文件站點 # chmod 777 /var/www/html/ chmod 600 /etc/rsyncd_users.db
- 啓動服務器
rsync --daemon 啓動 netstat -antp | grep rsync 查看873端口 kill -9 (進程) cd /var/run/ rm -rf rsyncd.pid #刪除pid文件 在啓動服務會自動生成 systemctl stop firewalld.service setenforce 0 cd /var/www/html #寫入文件進行測試 echo "this is test" > test.txt
- rsync命令的基本用法
[-a] 歸檔模式,表示以遞歸方式傳輸文件,並保持所有文件屬性,等於-rlptgoD。 [-z] 對備份的文件在傳輸時進行壓縮處理。 [-H] 保留硬鏈結。 [-D] 保持設備文件信息。 [-A] 保留ACL屬性信息 [-r]遞歸模式,包含目錄及子目錄中的所有文件。 [-l]對於符號鏈接文件仍然複製爲符號鏈接文件。 [-V]顯示同步過程的詳細(verbose) 信息。 [-p]保留文件的權限標記。 [-t]保留文件的時間標記。 [-g]保留文件的屬組標記(僅超級用戶使用) [-o]保留文件的屬主標記(僅超級用戶使用) [--delete] 刪除那些目標位置有而原始位置沒有的文件。 [--checksum] 打開校驗開關,強制對文件傳輸進行校驗。
客戶機B配置
systemctl stop firewalld.service setenforce 0 格式一: rsync -avz [email protected]::wwwroot /opt/ //密碼abc123 cd /opt 可看到test.txt文件 格式二: rsync -avz rsync://[email protected]/wwwroot /opt/ #免交互 vim /etc/server.pass #創建獨立存放密碼文件 abc123 chmod 600 /etc/server.pass rsync -az --delete --password-file=/etc/server.pass [email protected]::wwwroot /opt
rsync+inotify
- 服務器B調整inotify內核參數
- rsync命令的基本用法
chmod 777 /var/www/html #給與可讀可寫可執行
cat /proc/sys/fs/inotify/max_queued_events
16384 #監控事件列
cat /proc/sys/fs/inotify/max_user_instances
128 #最多監控實列數
cat /proc/sys/fs/inotify/max_user_watches
8192 #實例最多監控文件數
vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
sysctl -p 立即生效
服務器B需安裝一些小插件
更新yum源安裝
yum install -y epel-release && yum update
yum install inotify-tools (v3.14-8.el7.×86_64 as of 4-18-2018)
--------------由於我是手工編譯--------------------
yum install gcc gcc-c++ -y
tar zxvf inotify-tools-3.14.tar.gz -C /opt/
cd /opt/inotify-tools-3.14
./configure
make && make install
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/ [email protected]::wwwroot/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then
$RSYNC_CMD
fi
done
# chmod +x /opt/inotify.sh
# .//inotify.sh
# echo '/opt/inotify.sh' >> /etc/rc.local //加入開機自啓動
上述腳本用來檢測本機/var/www/html目標變動情況,一旦有更新觸發rsync 同步操作,上傳備份指服務器A的目錄下
inotifywait 簡單的使用介紹
inotifywait -mrq -e modify,create,move,delete /var/www/html/
詳解:
-m 表示持續監控
-r 表示遞歸整個目錄
-q 簡化輸出信息
inotifywait 可監控: modify 修改、 create 創建、move 移動、delete 刪除