這是我參與更文挑戰的第 16 天,活動詳情查看:更文挑戰html
一名致力於在技術道路上的終身學習者、實踐者、分享者,一位忙起來又偶爾偷懶的原創博主,一個偶爾無聊又偶爾幽默的少年。linux
歡迎各位掘友們微信搜索「傑哥的IT之旅」關注!算法
Rsync 是一個開源的快速備份工具
,是Linux
和UNIX
操做系統默認安裝的組件之一,可在不一樣主機間鏡像同步整個目錄,並支持增量備份文件傳輸,保持連接和權限
,採用優化的同步算法,傳輸前執行壓縮,適用於異地備份,鏡像服務器等應用
。bash
Rsync 官網地址:rsync.samba.org/服務器
在遠程同步任務中,負責發起 rsync 同步操做的客戶端稱爲發起端
,負責響應來自客戶端的 rsync 同步操做的服務器稱爲同步源
。微信
同步過程當中,同步源負責提供文檔的原始位置,發起端應對該位置具備讀取權限。markdown
關閉iptables 防火牆
和selinux
。併發
# service iptables stop
# setenforce 0
複製代碼
將rsync 源服務器
和rsync 客戶端
的/etc/hosts配置文件
解析成同樣的,以下配置。ssh
# tail -2 /etc/hosts
192.168.31.108 linuxserver.com
192.168.31.226 linuxclient.com
複製代碼
配置/etc/rsyncd.conf
文件
# vi /etc/rsyncd.conf
### This is rsyncd.conf
### By 微信公衆號:傑哥的IT之旅(ID:Jake_Internet)
uid = rsync // 使用的用戶
gid = rsync // 使用的用戶
use chroot = no // 是否使用根目錄鎖 若是"use chroot"指定爲true,那麼 rsync 在傳輸文件之前首先 chroot 到 path 參數所指定的目錄下。這樣作的緣由是實現額外的安全防禦,可是缺點是須要以 roots 權限,而且不能備份指向外部的符號鏈接所指向的目錄文件。默認狀況下 chroot 值爲 true。
max connections = 2000 // 指定該模塊的最大併發鏈接數量以保護服務器,超過限制的鏈接請求將被告知隨後再試,默認值是 0,也就是沒有限制。
timeout = 600 // 經過該選項能夠覆蓋客戶指定的IP超時時間。經過該選項能夠確保rsync服務器不會永遠等待一個崩潰的客戶端。超時單位爲秒鐘,0表示沒有超時定義,這也是默認值。對於匿名rsync服務器來講,一個理想的數字是600。
pid file = /var/run/rsyncd.pid // 存放進程 ID 的文件位置
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log // 日誌文件位置
ignore errors
read only = false // 是否爲只讀
list = false
hosts allow = 192.168.31.0/24 // 容許訪問的客戶端 IP 地址範圍
hosts deny = 0.0.0.0/32 // 拒絕訪問的客戶端 IP 地址範圍
auth users = rsync_backup // 受權帳戶
secrets file = /etc/rsync.password // 存放帳戶信息的數據文件
#################################
[backup]
path = /backup
複製代碼
啓動rsync
服務
這時,在啓動 rsync 服務時,會出現如上報錯。
# rsync --daemon
failed to create pid file /var/run/rsyncd.pid: File exists
複製代碼
解決方案:需刪除/var/run/rsyncd.pid
文件後,並重啓rsync
服務
# rm -rf /var/run/rsyncd.pid // 刪除 rsyncd.pid 文件
# rsync --daemon // 再從新啓動 Rsync 服務
複製代碼
建立/etc/rsync.password
文件
# echo 「rsync_backup:123456」 >/etc/rsync.password
複製代碼
查看/etc/rsync.password
文件,是否成功,並調整文件權限。
# cat /etc/rsync.password
rsync_backup:123.com
# chmod 600 /etc/rsync.password
複製代碼
監聽服務端口。
# pgrep rsync
5081
5125
# netstat -lntup|grep rsync
tcp 0 0 192.168.31.108:873 0.0.0.0:* LISTEN 5081/rsync
tcp 0 0 :::873 :::* LISTEN 5125/rsync
複製代碼
建立rsync
須要使用對的真實用戶
# useradd -s /sbin/nologin -M rsync
# id rsync
uid=500(rsync) gid=500(rsync) 組=500(rsync)
複製代碼
建立rsync
推送須要的目錄
# mkdir /backup
# chown -R rsync.rsync /backup/
複製代碼
rsync 命令的基本用法
大多數的備份程序要求指定原始位置、目標位置,rsync 命令其實跟 cp 命令類似。
好比:將文件/etc/fstab
、目錄/boot/grub
同步備份到/opt
目錄下。
-r
:選項表示遞歸整個目錄、-l
:選項表示備份連接文件。
# rsync /etc/fstab /opt
# rsync -rl /etc/fstab /boot/grub /opt
複製代碼
rsync 命令格式
基本格式:rsync [選項] 原始位置 目標位置
rsync 命令經常使用備份選項
-r
:遞歸模式,包含目錄及子目錄中的全部文件。
-l
:對於符號連接文件仍復製爲符號連接文件。
-v
:顯示同步過程的詳細信息。
-a
:歸檔模式,保留文件的權限、屬性等信息,組合選項等於-rlptgoD
。
-z
:在傳輸文件時進行壓縮。
-p
:保留文件的權限標記。
-t
:保留文件的時間標記。
-g
:保留文件的屬組標記。(僅超級用戶使用
)
-o
:保留文件的屬主標記。(僅超級用戶使用
)
-H
:保留硬鏈接文件。
-A
:保留 ACL 屬性信息。
-D
:保留設備文件及其餘特殊文件。
-- delete
:刪除目標位置有而原始位置沒有的文件。
--checksum
:根據校驗和(而不是文件大小、修改時間)來決定是否跳過文件。
配置源的表示方法
執行遠程同步任務時,rsync 命令需指定同步源服務器中的資源位置。
兩種表示方法
用戶名@主機地址::共享模塊名
(兩個冒號分隔形式)
rsync://用戶名@主機地址/共享模塊名
(URL 地址形式)
使用真實用戶測試rsync
服務
若是ssh端口
修改了,需加上端口。
命令格式
:rsync -參數 文件 -e ‘ssh -p 端口號’ 用戶名@ip:目錄路徑
# rsync -avz abc '-e ssh -p 22' root@linuxserver.com:/tmp // 實操命令格式
root@linuxserver.com's password: //輸入 linuxserver 設備的 root 密碼
sending incremental file list
abc
sent 64 bytes received 31 bytes 27.14 bytes/sec
total size is 0 speedup is 0.00
# ll /tmp // 查看同步結果
總用量 52
-rw-r--r--. 1 root root 0 12月 30 07:25 abc
複製代碼
在實際生產環境中的備份工做,一般都是重複性的,那麼咱們能夠設置個定時任務來完成備份操做。好比:天天凌晨04:00對服務器的tmp
共享模塊同步,並下載到本地/mnt/jacktian
目錄下,經過crond
服務來完成。
這種同步過程當中,咱們不想輸入密碼,那麼需建立一個密碼文件,保存rsync_backup
用戶的密碼。
# cat /etc/rsync.password
rsync_backup:123.com
# chmod 600 /etc/rsync.password
# ll /etc/rsync.password
-rw-------. 1 root root 21 12月 30 08:04 /etc/rsync.password
複製代碼
在執行 rsync 同步時使用選項--password-file=/etc/rsync.password
指定。
# crontab -e
00 04 * * * /usr/bin/rsync -az --delete --password-file=/etc/rsync.password@rsync_backup@192.168.31.108::tmp /mnt/jacktian
# service crond restart
# chkconfig crond on
複製代碼
Linux 從 2.6.13 版內核開始提供了 inotify 通知接口,用來監控文件系統的各類變化狀況
,如文件的存取,刪除,移動,修改等。利用這個機制,可實現文件異動警告,增量備份,針對目錄或文件的變化及時作出響應
。
rsync+inotify 能夠實現觸發式備份
,只要原始位置的文檔發生變化,則當即啓動增量備份,不然處於靜默等待狀態,避免了按固定週期備份時存在的延遲性,週期過密等問題。
inotify 主要作本機監控,在觸發式備份應用中更適合上行同步。
inotify 機制提供了三個調控參數
max_queued_events
:表示監控事件隊列
max_user_instances
:最多監控實例數
max_user_watches
:每一個實例最多監控文件數
# 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
複製代碼
調整 inotify 內核參數
當要監控的目錄,文件數量較多或者變化頻繁時,建議加大這三個參數的值。
# vi /etc/sysctl.conf
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
# sysctl -p
複製代碼
安裝 inotify-tools
安裝 inotify-tools 主要提供inotifywait,inotifywatch 等工具,用來監控,彙總改動狀況
。
# tar zxf inotify-tools-3.14.tar.gz
# cd inotify-tools-3.14
# ./configure && make && make install
複製代碼
以監控網站目錄/var/www/html
爲例,執行inotifywait
命令後,在另一個終端中改動/var/www/html
目錄下的內容。
-e
:指定要監控的事件
-m
:表示持續監控
-r
:表示遞歸整個目錄
-q
:簡化輸出信息
# mkdir /var/www/html -p
# inotifywait -mrq -e modify,create,move,delete /var/www/html/
複製代碼
另外一個終端:在/var/www/html/
目錄下添加文件、移動文件、跟蹤屏幕輸出結果
。
# mkdir /var/www/html/a
# mkdir /var/www/html/b
# mv /var/www/html/b /var/www/html/bb
# rm -rf /var/www/html/a
複製代碼
inotifywait 可監控 modify(修改),create(建立),move(移動),delete(刪除),attrib(屬性更改)等各類事件
,一有變更當即輸出結果。可用於收集系統變更狀況,並在運行結束後輸出彙總的變化狀況。
# inotifywait -mrq -e modify,create,move,delete /var/www/html/
fy,create,move,delete /var/www/html/
/var/www/html/ CREATE,ISDIR a
/var/www/html/ CREATE,ISDIR b
/var/www/html/ MOVED_FROM,ISDIR b
/var/www/html/ MOVED_TO,ISDIR bb
/var/www/html/ DELETE,ISDIR a
複製代碼
編寫觸發式同步腳本
inotifywait
輸出的監控結果中,每行記錄中包括目錄,事件,文件,可識別變更狀況,只要檢測到有變更時執行rsync
上行同步操做。
注意:當更新頻繁時,避免併發執行
rsync
備份,若rsync
進程已經存在,則忽略本次同步,或者根據rsync
進程數量來決定是否同步
腳本用來測試本機/var/www/html
目錄的變更,一旦有更新,將觸發rsync
同步操做,並上傳至服務器192.168.31.108
的/var/www/html
目錄下。
觸發式上行同步驗證過程
一、使用rsync
工具執行一次上行同步;
二、在本機運行bak_transfer.sh
腳本;
三、在本機的/var/www/html
目錄下,執行建立,刪除,修改等操做;
四、查看服務器中的/var/www/html
目錄中的變化;
# vi bak_transfer.sh
# This is bak_transfer.sh
# 微信公衆號:傑哥的IT之旅(ID:Jake_Internet)
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,move,attrib,delete /var/www/html/"
RSYNC_CMD="rsync -azH --delete /var/www/html/ rsync_backup@192.168.31.108:/var/www/html"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ]
then
$RSYNC_CMD
fi
done
# chmod +x bak_transfer.sh
# ./bak_transfer.sh
# echo "/bin/bash /root/bak_transfer.sh" >> /etc/rc.local
複製代碼
報錯
:failed to create pid file /var/run/rsyncd.pid: File exists
解決
:rm /var/run/rsyncd.pid
啓動服務:rsync --daemon --config=/etc/rsyncd.conf
報錯
:rsync: failed to connect to IP 地址: Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5]
解決
:啓動服務:rsync --daemon --config=/etc/rsyncd.conf
報錯
:rsync: failed to connect to IP 地址: Connection timed out (110)
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5]
解決
:多是 iptables 防火牆開啓致使沒法通訊,能夠設置規則放行873端口
或者直接關閉防火牆
。
報錯
:@ERROR: chroot failed
rsync: connection unexpectedly closed (75 bytes read so far)
rsync error: error in rsync protocol data stream (code 12) at io.c(150)
解決
:rsync.conf 中設置的 path 路徑不存在,需新建目錄。
原創不易,若是你以爲這篇文章對你有點用的話,麻煩你爲本文點個贊、評論或轉發一下,由於這將是我輸出更多優質文章的動力,感謝!
咱們下期再見!