rsync學習筆記

一、rsync介紹

1)rsync是什麼?
rsync(remote synchronize),一款開源的遠程數據備份的優秀工具

2)與SCP的比較:
SCP無法備份大量數據,類似Windows的複製
rsync:邊複製,邊統計,邊比較

3)第一次同步時,rsync會複製全部內容,但在下一次只傳輸修過的文件

4)數據同步的兩種方式:

push(推):rsync服務器主動推送數據給其他主機。服務器開銷大,適合後端服務器少的情況
pull(拉):客戶端主動向rsync服務器拉取數據

5)監聽端口:873

[root@qll251 ~]# grep rsync /etc/services
rsync           873/tcp                         # rsync
rsync           873/udp                         # rsync
[root@qll251 ~]#

6)Xinetd管理rsync工作
qinlulu

xinetd(extended internet daemon),新一代的網絡守護進程服務程序,又叫超級Internet服務器。用來管理多種輕量級Internet服務,比如:TFTP、rsync、Telnet等

二、rsync安裝

#  本次實驗以CentOS7.7爲例
[root@qll251 ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
[root@qll251 ~]#

1)需要使用rsync傳輸數據的主機都需要安裝rsync軟件

[root@qll251 ~]# yum -y install xinetd rsync

qinlulu2)以守護進程方式啓動rsync;查看rsync進程所使用的端口

[root@qll251 ~]# netstat -antup |grep rsync #由於此時未啓動rsync,因此不會有任何回顯
[root@qll251 ~]# rsync --daemon  #守護進程是指在後臺運行不受終端控制的進程。
[root@qll251 ~]# netstat -antup |grep rsync
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      4176/rsync
tcp6       0      0 :::873                  :::*                    LISTEN      4176/rsync
[root@qll251 ~]#

三、使用rsync進行數據傳輸

兩種傳輸方式:
1,使用系統用戶傳輸
2,無需建立系統用戶的傳輸(通過配置文件指定用戶)

下面我們詳細討論下這兩種方式

3.1 rsync使用系統用戶同步數據

1)使用rsync的主機都需要創建用戶,用於rsync數據同步

[root@qll251 ~]# useradd rsync_user
[root@qll251 ~]# echo 123 | passwd --stdin rsync_user
更改用戶 rsync_user 的密碼 。
passwd:所有的身份驗證令牌已經成功更新。
[root@qll251 ~]#

2)rsync服務器創建共享目錄,比如/rsync,並創建測試文件test.txt;並利用setfacl設置權限

[root@qll251 ~]# mkdir /rsync
[root@qll251 ~]# touch /rsync/test.txt
[root@qll251 ~]# ls /rsync/
test.txt
[root@qll251 ~]# setfacl -R -m user:rsync_user:rwx /rsync 
			   ###將/rsync/* 賦予rsync_user可讀可寫可執行的權限
[root@qll251 rsync]#

執行getfatcl /rsync,如圖所示
在這裏插入圖片描述

3)客戶端獲取rsync服務器共享目錄的兩種方式

客戶端 前提條件:
1.安裝rsync軟件
2.創建rsync_user用戶
3.創建待存放的目錄,並設置相應權限(允許rsync_user用戶可讀寫)

[root@qll251 ~]# yum -y install xinetd rsync
[root@qll252 ~]# useradd rsync_user
[root@qll252 ~]# echo 123 | passwd --stdin rsync_user
更改用戶 rsync_user 的密碼 。
passwd:所有的身份驗證令牌已經成功更新。
[root@qll252 /]# mkdir /shares  
[root@qll252 /]# chown -R rsync_user:rsync_user /shares
[root@qll252 /]#

3.1 rsync服務器端向客戶端推數據

## 語法: rsync -avz 共享路徑 客戶端用戶@客戶端IP:/目標路徑
[root@qll251 ~]# rsync -avz /rsync/ [email protected]:/shares
##想明白爲什麼rsync客戶端也需要創建rsync_user用戶了嗎?因爲服務器通過該用戶去訪問客戶端呀

在這裏插入圖片描述

上圖所示,已完成數據的同步。我們在qll252主機上ls驗證。

[root@qll252 ~]# ll /shares/
總用量 0
-rw-rwxr--. 1 rsync_user rsync_user 0 2月   5 12:10 test.txt
[root@qll252 ~]#

3.2 客戶端向服務器端拉取數據(pull)

## 語法: rsync -avz 服務器端用戶名@服務器端IP:/共享路徑 /本地路徑
[root@qll252 ~]# rsync -avz [email protected]:/rsync /shares

qinlulu

## qll252客戶端驗證數據同步情況
[root@qll252 ~]# ls /shares/
rsync
[root@qll252 ~]# ls /shares/rsync/
test.txt
[root@qll252 ~]#

以上便是採用系統用戶方式同步rsync數據,接下來我們討論不使用系統用戶,如何完成rsync的數據同步?

3.2 非系統用戶同步數據

1.使用系統配置文件/etc/rsyncd.conf來同步數據;
2.在配置文件中創建rsync賬號 [auth users = rsyncuser]
3.將rsync以daemon方式運行

1)編輯rsync配置文件:/etc/rsyncd.conf

## vim /etc/rsyncd.conf          //若文件不存在,需要自己創建
uid = root        #運行進程的身份
gid = root       #運行進程的組
address =192.168.1.252   #監聽IP
port =873          #監聽端口
hosts allow =192.168.1.0/24   #允許客戶端的IP地址,可以是網段,或者用*表示所有 
use chroot = yes     #若rsync被黑客入侵,則鎖定家目錄,黑客無法再rsync運行的家目錄之外創建文件
max connections =5        #最大連接數
pid file =/var/run/rsyncd.pid       #進程PID,自動生成
lock file =/var/run/rsync.lock      #指max connectios參數的鎖文件
log file =/var/log/rsyncd.log       #日誌文件位置
motd file =/etc/rsyncd.motd #客戶端登陸之後彈出的消息,需要創建
[image01]             #共享模塊名稱,可自定義
path =/mysql-bak   #本地服務器的路徑
comment = used for mysql-data    #描述
read only = false     #設置服務端文件讀寫權限
list = yes            #是否允許查看模塊信息
auth users = rsyncuser   #備份的用戶,和系統用戶無關
secrets file =/etc/rsync.passwd     #存放用戶的密碼文件,格式是  用戶名:密碼

2)啓動服務

[root@qll252 ~]# systemctl start xinetd && systemctl enable xinetd #啓動並設置xinetd進程開機自啓
[root@qll252 ~]# systemctl start rsyncd && systemctl enable rsync 
[root@qll252 ~]# rsync --daemon --config=/etc/rsyncd.conf #以守護進程方式運行rsync,且加載配置文件
[root@qll252 ~]# netstat -antup | grep rsync
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      1312/rsync
tcp6       0      0 :::873                  :::*                    LISTEN      1312/rsync
[root@qll252 ~]#

3)創建我們的共享目錄,及測試文件

[root@qll252 ~]# mkdir /mysql-bak
[root@qll252 ~]# touch /mysql-bak/test{1..5}.txt
[root@qll252 ~]# ls /mysql-bak/
test1.txt  test2.txt  test3.txt  test4.txt  test5.txt
[root@qll252 ~]#

4)客戶端向服務器端拉取數據(pull)

若提示:No route to hsot (113),則說明防火牆未關閉,我們關閉即可。
在這裏插入圖片描述

[root@qll252 ~]# systemctl stop  firewalld && systemctl disable firewalld

再次執行,rsync同步命令:

在這裏插入圖片描述

還是報錯,這次我們看下服務日誌:tailf /var/log/rsyncd.log。看下圖
在這裏插入圖片描述
好了,通過日誌我們得知:客戶端向服務器驗證用戶時,沒有這個密碼文件(未創建/etc/rsync.passwd)

## 根據日誌提示,我們創建密碼文件即可,格式 用戶名:密碼,且文本權限必須爲600
[root@qll252 ~]# echo "rsyncuser:123123" >> /etc/rsync.passwd
[root@qll252 ~]# chmod 600 /etc/rsync.passwd

開始進行備份

[root@qll251 ~]# rsync -avz [email protected]::image01 /tmp

在這裏插入圖片描述

我們也可以在執行命令時,提前定義好密碼:

# 本地創建密碼文本,並設置600權限
echo "123123" > /etc/rsync.mypasswd && chmod 600  /etc/rsync.mypasswd
## 執行命令時,加上 --password-file=/etc/rsync.mypasswd 即可
###完整命令如下:
rsync -avz rsync -avz [email protected]::image01 /tmp --password-file=/etc/rsync.mypasswd

以上便是兩種rsync同步方式,生產環境中多用於重要數據的定時備份。
我們可以通過寫一個簡單的shell腳本來完成這個定時任務。

## 新建一個定時備份腳本
vim /root/bak.sh
#!/bin/bash
rsync -avz rsync -avz [email protected]::image01 /tmp --password-file=/etc/rsync.mypasswd

chmod 755 /root/bak/sh
echo " 0 2 * * * sh /root/bak.sh " #設置每天凌晨兩點執行該腳本

更多IT技術,請微信搜索公衆號秦露露或者掃描下方二維碼關注

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章