一、rsync 簡介
rsync
(Remote Sync)是 Linux 系統下的數據鏡像備份工具,支持本地復制、遠程同步(通過 SSH 或 rsync 協議),是一個快速、安全、高效的增量備份工具。
二、rsync 特性
- 支持鏡像保存整個目錄樹和文件系統;
- 保留文件權限、時間、軟硬鏈接等屬性;
- 無須特殊權限即可安裝;
- 增量備份:僅傳輸修改過的文件,支持壓縮傳輸,節省帶寬;
- 安全:支持 SSH、SCP 等方式加密傳輸;
- 支持匿名傳輸,常用于網站鏡像。
三、rsync 的認證協議
rsync 同步前需認證,支持兩種協議:
- SSH 協議(常用)
- rsync 協議(需啟動
rsyncd
服務)
若使用 SSH 認證,無需啟動 rsync 服務端守護進程,只需遠程主機的用戶名和密碼即可同步。若不想每次輸入密碼,可使用 ssh-keygen -t rsa
設置免密登錄。
常用命令示例:
# 默認使用 SSH 協議(省略 -e ssh)
rsync -avz /SRC root@192.168.100.20:/DEST# 指定 SSH 端口,默認是22
rsync -avz /SRC -e "ssh -p2222" root@192.168.100.20:/DEST
常用選項說明:
選項 | 說明 |
---|---|
-a, --archive | 歸檔模式,保留所有屬性 |
-v, --verbose | 顯示詳細輸出 |
-q, --quiet | 靜默模式,不輸出信息 |
-r, --recursive | 遞歸傳輸目錄 |
-p, --perms | 保留權限 |
-z, --compress | 壓縮傳輸 |
--delete | 刪除目標端有而源端沒有的文件 |
四、rsync 命令格式與工作模式
三種命令格式:
rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [USER@]HOST:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST
對應三種工作模式:
- 本地拷貝(SRC 和 DEST 均無冒號)
- 推送到遠程(DEST 包含
user@host:
) - 從遠程拉取(SRC 包含
user@host:
)
五、rsync + inotify 實時同步
背景:
rsync 雖高效,但不實時。inotify
是 Linux 內核提供的文件系統事件監控機制,可監聽文件/目錄的創建、修改、刪除等事件。
組合優勢:
inotify
監控文件變化;- 觸發
rsync
實時同步; - 解決 rsync 非實時性的問題。
六、實戰配置:rsync + inotify 實時同步
環境說明:
服務器類型 | IP地址 | 應用 | 操作系統 |
---|---|---|---|
源服務器 | 192.168.100.10 | rsync + inotify | CentOS 7 |
目標服務器 | 192.168.100.20 | rsync 服務端 | CentOS 7 |
目標:
將源服務器 /root/etc
實時同步到目標服務器的 /tmp
目錄。
配置時鐘同步,將源服務器當作時鐘服務器
源服務器:
[root@server ~] vim /etc/chrony.conf# Serve time even if not synchronized to a time source.
local stratum 10[root@server ~] systemctl restart chronyd
[root@server ~] systemctl enable chronyd
[root@server ~] hwclock -w
目標服務器:
[root@YDH tmp] vim /etc/chrony.conf
server 192.168.100.10 iburst[root@YDH tmp] systemctl restart chronyd
[root@YDH tmp] systemctl enable chronyd
[root@YDH tmp] hwclock -w
[root@YDH tmp] chronyc sources
210 Number of sources = 1MS Name/IP address Stratum Poll Reach LastRx Last sample===============================================================================^? 192.168.100.10 0 8 0 - +0ns[ +0ns] +/- 0ns
步驟一:目標服務器配置(接收端)
關閉防火墻和 SELinux
[root@YDH ~] systemctl stop firewalld
[root@YDH ~] systemctl disable firewalld
[root@YDH ~] setenforce 0
[root@YDH ~] sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
安裝 rsync
yum -y install rsync
配置 /etc/rsyncd.conf
log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pass[etc_from_client]
path = /tmp/
comment = sync etc from client
uid = root
gid = root
port = 873
ignore errors = yes
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = admin
hosts allow = 192.168.100.10
hosts deny = 192.168.1.1
創建密碼文件并設置權限
[root@YDH tmp] echo 'admin:redhat' > /etc/rsync.pass
[root@YDH tmp] cat /etc/rsync.pass
admin:redhat
#設置文件權限
[root@YDH tmp] chmod 600 /etc/rsync*
[root@YDH tmp] ll /etc/rsync*
-rw-------. 1 root root 384 Sep 20 16:14 /etc/rsyncd.conf
-rw-------. 1 root root 13 Sep 20 16:15 /etc/rsync.pass
啟動 rsync 服務并設置開機自啟
[root@YDH ~] rsync --daemon --config=/etc/rsyncd.conf
[root@YDH ~] echo 'rsync --daemon --config=/etc/rsyncd.conf' >> /etc/rc.d/rc.local
[root@YDH ~] netstat -tulnp | grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 2043/rsync
tcp6 0 0 :::873 :::* LISTEN 2043/rsync
步驟二:源服務器配置(發送端)
關閉防火墻和 SELinux
[root@server ~] systemctl stop firewalld
[root@server ~] systemctl disable firewalld
[root@server ~] setenforce 0
[root@server ~] sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
安裝 rsync 和 inotify-tools
[root@server ~] yum -y install rsync inotify-tools
創建密碼文件
[root@server ~] echo 'redhat' > /etc/rsync.pass
[root@server ~] cat /etc/rsync.pass
redhat
[root@server ~] chmod 600 /etc/rsync.pass
測試同步是否正常
[root@server ~] mkdir -p /root/etc/test
mkdir: created directory '/root/etc'
mkdir: created directory '/root/etc/test'[root@server ~] rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.100.20::etc_from_client --password-file=/etc/rsync.passsending incremental file list
deleting .font-unix/
deleting .esd-0/socket
deleting .esd-0/
deleting .XIM-unix/
deleting .X11-unix/X1024
deleting .X11-unix/
deleting .Test-unix/
deleting .ICE-unix/1486
deleting .ICE-unix/
deleting .viminfo
deleting .X1024-lock
./
test/sent 77 bytes received 191 bytes 536.00 bytes/sec
total size is 0 speedup is 0.00
#運行完成后,在目標服務器上查看,在/tmp目錄下有test目錄,說明數據同步成功
[root@YDH ~] cd /tmp/
[root@YDH tmp] ls
test
步驟三:配置實時同步腳本
安裝inotify-tools工具,實時觸發rsync進行同步
#查看服務器內核是否支持inotify
[root@server ~] ll /proc/sys/fs/inotify
total 0
-rw-r--r--. 1 root root 0 Sep 20 16:54 max_queued_events
-rw-r--r--. 1 root root 0 Sep 20 16:54 max_user_instances
-rw-r--r--. 1 root root 0 Sep 20 2022 max_user_watches
#如果有這三個max開頭的文件則表示服務器內核支持inotify#安裝inotify-tools
[root@server ~] yum -y install make gcc gcc-c++[root@server ~] yum -y install inotify-tools#寫同步腳本,此步乃最最重要的一步,請慎之又慎。讓腳本自動去檢測我們制定的目錄下 \
#文件發生的變化,然后再執行rsync的命令把它同步到我們的服務器端去[root@server ~] mkdir /yangduhan
[root@server ~] touch /yangduhan/inotify.sh
[root@server ~] chmod +x /yangduhan/inotify.sh
創建監控腳本 /yangduhan/inotify.sh
[root@server ~] vim /yangduhan/inottify.sh#!/bin/bash
host=192.168.100.20 # 目標服務器的ip(備份服務器)
src=/root/etc # 在源服務器上所要監控的備份目錄(此處可以自定義,但是要保證存在)
des=etc_from_client # 自定義的模塊名,需要與目標服務器上定義的同步名稱一致
password=/etc/rsync.pass # 執行數據同步的密碼文件
user=admin # 執行數據同步的用戶名
inotifywait=/usr/bin/inotifywait$inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' \-e modify,delete,create,attrib $src | while read files; dorsync -avzP --delete --timeout=100 --password-file=$password $src $user@$host::$desecho "${files} was rsynced" >> /tmp/rsync.log 2>&1
done
賦予執行權限并后臺運行
[root@server ~] chmod +x /yangduhan/inotify.sh
[root@server ~] nohup /bin/bash /yangduhan/inotify.sh &
[1] 32503nohup: ignoring input and appending output to 'nohup.out'[root@server ~] ps -ef | grep inotify
root 32503 2458 0 17:05 pts/1 00:00:00 bash /yangduhan/inotify.sh
root 32504 32503 0 17:05 pts/1 00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /root/etc
root 32505 32503 0 17:05 pts/1 00:00:00 bash /yangduhan/inotify.sh
root 32510 2458 0 17:05 pts/1 00:00:00 grep --color=auto inotify
設置開機自啟
echo 'nohup /bin/bash /yangduhan/inotify.sh &' >> /etc/rc.d/rc.local
步驟四:驗證實時同步
在源服務器創建文件:
[root@localhost ~] touch /root/etc/ydh123
查看日志:
[root@server ~] tail -f /tmp/rsync.log
20220920 17:06 /root/etc/ydh123CREATE was rsynced
20220920 17:06 /root/etc/ydh123ATTRIB was rsynced
#從日志上可以看到,我們生成了一個test文件,并且添加了內容到其里面
在目標服務器檢查:
ls /tmp/etc/
源服務器中,設置腳本開機啟動:
[root@server ~] chmod +x /etc/rc.d/rc.local
[root@server ~] ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 474 Mar 24 2020 /etc/rc.d/rc.local
[root@server ~] echo 'nohup /bin/bash /yangduhan/inotify.sh &' >> /etc/rc.d/rc.local
[root@server ~] tail /etc/rc.d/rc.local
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.touch /var/lock/subsys/localnohup /bin/bash /yangduhan/inotify.sh &#到目標服務器上去查看是否把新生成的文件自動傳上去了:
[root@YDH tmp] ls
etc test
[root@YDH tmp] cd etc/
[root@YDH etc] ls
yangduhan123 test
[root@YDH etc] pwd
/tmp/etc#由此可見,已將源服務器的/root/etc目錄整個同步到了目標服務器,且新增的test文件也自動同步了