簡介
rsync是Linux系統下的數據鏡像備份系統。可以遠程同步,支持本地複製,或者與其他SSH,rsync主機同步。
特性
- 同步小文件
- 可以鏡像保存整個目錄樹和文件系統
- 無需特殊權限即可安裝
- 快速:第一次同步會複製全部內容,但下一次只傳輸修改過的文件;傳輸過程可以實行壓縮解壓縮,佔用帶寬少
- 安全:可以使用scp、ssh。也可以直接socket
- 支持匿名傳輸(已做好互信),方便網站同步
rsync命令
// rsync 的命令格式常用的有一下三種:
rsync [OPTION]... SRC DEST(源地址)
//拷貝本地文件至本地
[root@system1 ~]# ls
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates
Desktop Downloads Music Public Videos
[root@system1 ~]# rsync -avz anaconda-ks.cfg rsync-copy
sending incremental file list
anaconda-ks.cfg
sent 995 bytes received 31 bytes 2052.00 bytes/sec
total size is 1614 speedup is 1.57
[root@system1 ~]# ls
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures rsync-copy
rsync [OPTION]... SRC [USER@]HOST:DEST
//拷貝本地文件至遠程主機
[root@system1 etc]# rsync -avz passwd [email protected]:/tmp/
[email protected]'s password:
sending incremental file list
passwd
sent 830 bytes received 31 bytes 114.80 bytes/sec
total size is 1968 speedup is 2.29
rsync [OPTION]... [USER@]HOST:SRC DEST
//拷貝遠程主機文件至本地
[root@system1 ~]# rsync -avz [email protected]:/etc/yum.repos.d /root
[email protected]'s password:
receiving incremental file list
yum.repos.d/
yum.repos.d/iso.repo
sent 34 bytes received 167 bytes 44.67 bytes/sec
total size is 61 speedup is 0.30
[root@system1 ~]# ls
anaconda-ks.cfg Downloads Pictures Templates
Desktop initial-setup-ks.cfg Public Videos
Documents Music rsync-copy yum.repos.d
//rsync常用選項:
-a, --archive //歸檔
-v, --verbose //囉嗦模式
-q, --quiet //靜默模式
-r, --recursive //遞歸
-p, --perms //保持原有的權限屬性
-z, --compress //在傳輸時壓縮,節省帶寬,加快傳輸速度
--delete //在源服務器上做的刪除操作也會在目標服務器上同步
rsync+inotify
在目標服務器上做以下操作:
//關閉防火牆與SELINUX
[root@ldap ~]# systemctl stop firewalld
[root@ldap ~]# systemctl disable firewalld
[root@ldap ~]# getenforce
Enforcing
[root@ldap ~]# setenforce 0
[root@ldap ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
//安裝rsync服務端軟件
[root@ldap ~]# yum -y install rsync
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
myrepo | 4.1 kB 00:00
(1/2): myrepo/group_gz | 137 kB 00:00
(2/2): myrepo/primary_db | 4.0 MB 00:00
Resolving Dependencies
--> Running transaction check
---> Package rsync.x86_64 0:3.0.9-18.el7 will be installed
...
myrepo/productid | 1.6 kB 00:00
Verifying : rsync-3.0.9-18.el7.x86_64 1/1
Installed:
rsync.x86_64 0:3.0.9-18.el7
Complete!
//設置rsyncd.conf配置文件
root@ldap ~]# cat >> /etc/rsyncd.conf <<EOF
log file = /var/log/rsyncd.log //日誌文件位置,啓動rsync後自動產生這個文件,無需提前創建
pidfile = /var/run/rsyncd.pid //pid文件的存放位置
lock file = /var/run/rsync.lock //支持max connections參數的鎖文件
secrets file = /etc/rsync.pass //用戶認證配置文件,裏面保存用戶名稱和密碼,必須手動創建這個文件
[etcfromclient] //自定義同步名稱
path = /tmp/ //rsync服務端數據存放路徑,客戶端的數據將同步至此目錄
comment = sync etc from client
uid = root //設置rsync運行權限爲root
gid = root //設置rsync運行權限爲root
port = 873 //默認端口
ignore errors //表示出現錯誤忽略錯誤
use chroot = no //默認爲true,修改爲no,增加對目錄文件軟連接的備份
read only = no //設置rsync服務端爲讀寫權限
list = no //不顯示rsync服務端資源列表
max connections = 200 //最大連接數
timeout = 600 //設置超時時間
auth users = admin //執行數據同步的用戶名,可以設置多個,用英文狀態下逗號隔開
hosts allow = 172.16.30.254 //允許進行數據同步的客戶端IP地址,可以設置多個,用英文狀態下逗號隔開
hosts deny = 192.168.1.1 //禁止數據同步的客戶端IP地址,可以設置多個,用英文狀態下逗號隔開
EOF
//創建用戶認證文件
[root@ldap ~]# echo 'admin:123456' > /etc/rsync.pass
[root@ldap ~]# cat /etc/rsync.pass
admin:123456
//設置文件權限
[root@ldap ~]# chmod 600 /etc/rsync
[root@ldap ~]# ll /etc/rsync
-rw-------. 1 root root 802 Aug 9 22:45 /etc/rsyncd.conf
-rw-------. 1 root root 13 Aug 9 22:47 /etc/rsync.pass
//啓動rsync服務並設置開機自啓動
[root@ldap ~]# systemctl start rsyncd
[root@ldap ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
[root@ldap ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :22 :
LISTEN 0 100 127.0.0.1:25 :
LISTEN 0 5 :873 :
LISTEN 0 128 :::22 :::
LISTEN 0 100 ::1:25 :::
LISTEN 0 5 :::873 :::*
![](https://s1.51cto.com/images/blog/201902/19/2a97b5ca61ccff2d055d084474f70815.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
**在原服務器上**
//關閉防火牆和SELINUX
[root@system1 ~]# systemctl stop firewalld
[root@system1 ~]# systemctl disable firewalld
rm '/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service'
rm '/etc/systemd/system/basic.target.wants/firewalld.service'
[root@system1 ~]# getenforce
Permissive
[root@system1 ~]# yum -y install rsync//安裝rsync
//創建認證密碼文件
[root@system1 ~]# echo '123456' > /etc/rsync.pass
//設置文件權限,只設置文件所有者具有讀取、寫入權限
[root@system1 ~]# chmod 600 /etc/rsync.pass
[root@system1 ~]# ll /etc/rsync.pass
-rw-------. 1 root root 7 Feb 19 19:59 /etc/rsync.pass
//在源服務器上創建測試目錄,然後在源服務器運行
[root@system1 ~]# mkdir -pv /root/etc/test
mkdir: created directory ‘/root/etc’
mkdir: created directory ‘/root/etc/test’
[root@system1 ~]# rsync -avH --port 873 --progress --delete /root/etc/ [email protected]::etc_from_client --password-file=/etc/rsync.pass
sending incremental file list
./
deleting systemd-private-ySPPll/tmp/
deleting systemd-private-ySPPll/
...
![](https://s1.51cto.com/images/blog/201902/20/951ffb3acbe823abb1d5f92ce2ca8f2d.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
//安裝inotify-tools工具,實時觸發rsync進行同步
//查看服務器內核是否支持inotify
[root@system1 ~]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Aug 10 11:19 max_queued_events
-rw-r--r-- 1 root root 0 Aug 10 11:19 max_user_instances
-rw-r--r-- 1 root root 0 Aug 10 11:19 max_user_watches
//如果有這三個max開頭的文件則表示服務器內核支持inotify
//安裝inotify-tools
[root@system1 ~]# yum -y install make gcc gcc-c++
....
[root@system1 ~]# yum -y install inotify-tools
....
//寫同步腳本,此步乃最最重要的一步,請慎之又慎。讓腳本自動檢測我們制定的目錄下文件發生的變化,然後再執行rsync的命令把它同步到我們的服務器端去
[root@system1 ~]# mkdir /scripts
[root@system1 ~]# touch /scripts/inotify.sh
[root@system1 ~]# chmod 755 /scripts/inotify.sh
[root@system1 ~]# ll /scripts/inotify.sh
-rwxr-xr-x 1 root root 0 Aug 10 13:02 /scripts/inotify.sh
[root@system1 ~]# vim /scripts/inotify.sh
host=172.16.30.254 //目標服務器的ip(備份服務器)
src=/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 ;do
rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
//啓動腳本
[root@system1 ~]# nohup bash /scripts/inotify.sh &
[1] 86871
[root@system1 ~]# nohup: ignoring input and appending output to ‘nohup.out’
[root@system1 ~]# ps -ef|grep inotify
root 86871 2143 0 14:52 pts/0 00:00:00 bash /scripts/inotify.sh
root 86872 86871 0 14:52 pts/0 00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /etc
root 86873 86871 0 14:52 pts/0 00:00:00 bash /scripts/inotify.sh
root 86875 2143 0 14:52 pts/0 00:00:00 grep --color=auto inotify