1、RSYNC數據備份
1.1 rsync服務簡介
rsync命令是一個遠程數據同步工具,可通過LAN/WAN快速同步多台主機間的文件。rsync使用所謂的“rsync算法”來使本地和遠程兩個主機之間的文件達到同步,這個算法只傳送兩個文件的不同部分,而不是每次都整份傳送,因此速度相當快。
1.2 rsync特點和優勢
-
rsync功能
-
作為命令,實現本地-遠程文件同步
-
作為服務,實現本地-遠程文件同步
-
rsync特點
-
可以鏡像保存整個目錄樹和文件系統
-
可以保留原有的權限(permission,mode),owner,group,時間(修改時間,modify time),軟硬鏈接,文件acl,文件屬性(attributes)信息等
-
傳輸效率高,使用同步算法,只比較變化的
-
支持匿名傳輸,方便網站鏡像;也可以做驗證,加強安全
-
rsync 在傳輸數據的過程中可以實行壓縮及解壓縮操作,因此可以使用更少的帶寬
-
rsync同類服務類型
-
sync 同步:刷新文件系統緩存,強制將修改過的數據塊寫入磁盤,並且更新超級塊。
-
async 異步:將數據先放到緩沖區,再周期性(一般是30s)的去同步到磁盤。
-
rsync 遠程同步:remote synchronous
-
常見備份分類
- 完整備份:每次備份都是從備份源將所有的文件或目錄備份到目的地
- 差量備份:備份上次完全備份以后有變化的數據(他針對的上次的完全備份,他備份過程中不清除存檔屬性)
- 增量備份:備份上次備份以后有變化的數據.(他才不管是那種類型的備份,有變化的數據就備份,他會清除存檔屬性)
注意: 通常結合shell腳本使用,效率更高。官方網站:https://rsync.samba.org/
1.3 rysnc運行模式簡介
-
運行模式和端口
-
采用C/S模式(客戶端/服務器模式)[ 就是一個點到點的傳輸,直接使用rsync命令 ]
-
端口873
-
相關名詞解釋
-
發起端:負責發起rsync同步操作的客戶機叫做發起端,通知服務器我要備份你的數據
-
備份源:負責相應來自客戶機rsync同步操作的服務器叫做備份源,需要備份的服務器
-
服務端:運行rsyncd服務,一般來說,需要備份的服務器
-
客戶端:存放備份數據
-
rysncd服務工作原理:rysncd服務通過Xinetd管理,使用rsyncd服務來同步是先通過xinetd監聽873號端口,如果rsync進來的是873號端口,那么xinetd就會通知它所管轄的rsync服務來做回應,接下來就是rsync倆服務於之間的通訊,如下圖所示:
1.4 數據同步方式
兩種數據同步方式:
- push推方式: 一台主機負責把數據傳送給其他主機,服務器開銷很大,比較適合后端服務器少的情況
- pull拉方式: 所有主機定時去找一主機拉數據,可能就會導致數據緩慢
- 常用的數據同步方式:
- 推:目的主機配置為rsync服務器,源主機周期性的使用rsync命令把要同步的目錄推過去(需要備份的機器是客戶端,存儲備份的機器是服務端)
rsync -avz 源路徑 user@遠端服務器IP:目的路徑
[root@centos7-127 tmp]# mkdir test1 # 創建測試文件見test1
[root@centos7-127 tmp]# cp /etc/passwd ./test1/ # 復制/etc/passwd作為測試文件
[root@centos7-127 tmp]# ll ./test1/
總用量 4
-rw-r--r-- 1 root root 2567 9月 17 17:43 passwd
[root@centos7-127 tmp]# rsync -avz /tmp/test1 root@192.168.87.128:/tmp/ # 將127上的/tmp/test1推到128的/tmp目錄下
root@192.168.87.128's password: # 輸入遠端服務器root密碼
sending incremental file list
test1/
test1/passwd
sent 1,127 bytes received 39 bytes 155.47 bytes/sec
total size is 2,567 speedup is 2.20 # 傳輸完成
[root@centos7-128 tmp]# ll ./test1 # 在128上查看發現數據推送成功
總用量 4
-rw-r--r-- 1 root root 2567 9月 17 17:43 passwd
- 拉:源主機配置為rsync服務器,目的主機周期性的使用rsync命令把要同步的目錄拉過來(需要備份的機器是服務端,存儲備份的機器是客戶端)
rsync -avz user@遠端服務器IP:源路徑 目的路徑
[root@centos7-127 tmp]# rsync -avz root@192.168.87.128:/tmp/test/ /tmp/test # 從128拉取數據
root@192.168.87.128's password:
receiving incremental file list
./
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
a.txt
b.txt
c.txt
d.txt
e.txt
f.txt
sent 312 bytes received 834 bytes 152.80 bytes/sec # 數據傳輸完成
total size is 0 speedup is 0.00
[root@centos7-127 tmp]# ll ./test # 查看發現數據拉取成功
總用量 0
-rw-r--r-- 1 root root 0 9月 16 10:38 1.txt
-rw-r--r-- 1 root root 0 9月 16 10:38 2.txt
-rw-r--r-- 1 root root 0 9月 16 10:38 3.txt
-rw-r--r-- 1 root root 0 9月 16 10:38 4.txt
-rw-r--r-- 1 root root 0 9月 16 10:38 5.txt
-rw-r--r-- 1 root root 0 9月 16 10:38 6.txt
-rw-r--r-- 1 root root 0 9月 16 10:38 7.txt
-rw-r--r-- 1 root root 0 9月 16 10:38 8.txt
-rw-r--r-- 1 root root 0 9月 16 10:38 9.txt
-rw-r--r-- 1 root root 0 9月 16 10:26 a.txt
-rw-r--r-- 1 root root 0 9月 16 10:26 b.txt
-rw-r--r-- 1 root root 0 9月 16 10:26 c.txt
-rw-r--r-- 1 root root 0 9月 16 10:26 d.txt
-rw-r--r-- 1 root root 0 9月 16 10:26 e.txt
-rw-r--r-- 1 root root 0 9月 16 10:26 f.txt
2 Rsync實驗測試
2.1 實驗環境說明
|:-😐:-😐:-😐:-😐
| | IP | 服務安裝 | 說明 |
| 1 | 192.168.87.127 | Rsync服務 | 服務端(數據需要備份) |
| 2 | 192.168.87.128 | 安裝Xinetd、Rsync服務 |客戶端(用於備份數據) |
2.2 服務安裝
Rsync服務依賴Xinetd,是使用超級服務來管理的
需要在目標機器上安裝rsync服務端(備注:centos7一般不是最小安裝都已經默認安裝了rsync服務)
[root@centos7-128 tmp]# yum -y install xinetd rsync # 在128上面安裝Xinetd服務和Rsync服務
[root@centos7-128 tmp]# rsync --daemon # 啟動rsync服務
[root@centos7-128 tmp]# netstat -anutp|grep 873 # 查看rsync端口873,確認rsync服務是否啟動
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 41260/rsync
tcp6 0 0 :::873 :::* LISTEN 41260/rsync
[root@centos7-127 tmp]# yum -y install rsync # 在127上面安裝Rsync服務
2.3 Rsync命令詳解
rsync命令參數說明:
參數 | 說明 |
---|---|
-a | --archive archive mode 權限保存模式,相當於 -rlptgoD 參數,存檔,遞歸,保持屬性等 |
-r | --recursive 復制所有下面的資料,遞歸處理 |
-p | --perms 保留檔案權限 ,文件原有屬性 |
-t | --times 保留時間點,文件原有時間 |
-g | --group 保留原有屬組 |
-o | --owner 保留檔案所有者(root only) |
-D | --devices 保留device資訊(root only) |
-l | --links 復制所有的連接 ,拷貝連接文件 |
-z | --compress 壓縮模式, 當資料在傳送到目的端進行檔案壓縮 |
-H | --hard-links 保留硬鏈接文件 |
-A | --acls 保留ACL屬性文件,需要配合--perms |
-P | -P參數和 --partial --progress 相同.只是為了把參數簡單化,表示傳進度 |
--version | 輸出rsync版本 |
-v | --verbose 復雜的輸出信息 |
-u | --update 僅僅進行更新,也就是跳過已經存在的目標位置,並且文件時間要晚於要備份的文件,不覆蓋新的文件 |
--port=PORT | 定義rsyncd(daemon)要運行的port(預設為tcp 873) |
--delete | 刪除那些目標位置有的文件而備份源沒有的文件 |
--password-file=FILE | 從 FILE 中得到密碼 |
--bwlimit=KBPS | 限制 I/O 帶寬 |
--filter “-filename” | 需要過濾的文件 |
--exclude=filname | 需要過濾的文件 |
--progress | 顯示備份過程 |
說明: 常用參數-avz
2.3 實驗一使用rsync命令備份數據
將源服務器127上面的/var/www/html目錄備份到目標服務器128上的/web-back目錄中
- 建立測試目錄
[root@centos7-127 tmp]# mkdir -p /var/www/html # 創建測試目錄
[root@centos7-127 var]# cp -R /boot/* /var/www/html/ # 將/boot/下的所有文件復制到測試目錄,做欸測試文件使用
[root@centos7-127 var]# ll /var/www/html/
總用量 187816
-rw-r--r-- 1 root root 151923 9月 17 19:54 config-3.10.0-957.21.3.el7.x86_64
-rw-r--r-- 1 root root 151918 9月 17 19:54 config-3.10.0-957.el7.x86_64
drwx------ 3 root root 17 9月 17 19:54 efi
drwxr-xr-x 2 root root 27 9月 17 19:54 grub
drwx------ 5 root root 97 9月 17 19:54 grub2
-rw------- 1 root root 74017022 9月 17 19:54 initramfs-0-rescue-1d6dc8e931344828b171ea7fe67c3aa2.img
-rw------- 1 root root 31563403 9月 17 19:54 initramfs-3.10.0-957.21.3.el7.x86_64.img
-rw------- 1 root root 13599461 9月 17 19:54 initramfs-3.10.0-957.21.3.el7.x86_64kdump.img
-rw------- 1 root root 31562369 9月 17 19:54 initramfs-3.10.0-957.el7.x86_64.img
-rw------- 1 root root 13600009 9月 17 19:54 initramfs-3.10.0-957.el7.x86_64kdump.img
-rw-r--r-- 1 root root 314128 9月 17 19:54 symvers-3.10.0-957.21.3.el7.x86_64.gz
-rw-r--r-- 1 root root 314036 9月 17 19:54 symvers-3.10.0-957.el7.x86_64.gz
-rw------- 1 root root 3545891 9月 17 19:54 System.map-3.10.0-957.21.3.el7.x86_64
-rw------- 1 root root 3543471 9月 17 19:54 System.map-3.10.0-957.el7.x86_64
-rwxr-xr-x 1 root root 6639904 9月 17 19:54 vmlinuz-0-rescue-1d6dc8e931344828b171ea7fe67c3aa2
-rwxr-xr-x 1 root root 6643904 9月 17 19:54 vmlinuz-3.10.0-957.21.3.el7.x86_64
-rwxr-xr-x 1 root root 6639904 9月 17 19:54 vmlinuz-3.10.0-957.el7.x86_64
[root@centos7-128 tmp]# mkdir /web-back # 在目標服務器創建測試備份目錄
- 建立測試用戶
[root@centos7-127 tmp]# useradd rget && echo rget:123456|chpasswd # 在源服務器127上面新建rget測試用戶
[root@centos7-127 tmp]# id rget
uid=1001(rget) gid=1001(rget) 組=1001(rget)
[root@centos7-128 tmp]# useradd rget && echo rget:123456|chpasswd # 在目標服務器128上面新建rget測試用戶
[root@centos7-128 tmp]# id rget
uid=1001(rget) gid=1001(rget) 組=1001(rget)
- 對目錄賦予ACL權限
[root@centos7-127 var]# setfacl -R -m user:rget:rwx /var/www/html/
# 在源服務器上面設置rget權限,-R參數是遞歸將目錄以及在目錄和文件權限均進行設置
[root@centos7-127 var]# setfacl -R -m default:rget:rwx /var/www/html/
[root@centos7-127 var]# getfacl /var/www/html
getfacl: Removing leading '/' from absolute path names
# file: var/www/html
# owner: root
# group: root
user::rwx
user:rget:rwx
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:rget:rwx
default:group::r-x
default:mask::rwx
default:other::r-x
[root@centos7-128 tmp]# chown -R rget:rget /web-back/
#在目標服務器上修改rget對備份目錄的權限
[root@centos7-128 tmp]# getfacl /web-back/
getfacl: Removing leading '/' from absolute path names
# file: web-back/
# owner: rget
# group: rget
user::rwx
group::r-x
other::r-x
- 使用rsync命令備份數據
[root@centos7-127 var]# rsync -avz --delete /var/www/html/ rget@192.168.87.128:/web-back/
rget@192.168.87.128's password:
sending incremental file list
./
System.map-3.10.0-957.21.3.el7.x86_64
System.map-3.10.0-957.el7.x86_64
config-3.10.0-957.21.3.el7.x86_64
config-3.10.0-957.el7.x86_64
initramfs-0-rescue-1d6dc8e931344828b171ea7fe67c3aa2.img
initramfs-3.10.0-957.21.3.el7.x86_64.img
initramfs-3.10.0-957.21.3.el7.x86_64kdump.img
initramfs-3.10.0-957.el7.x86_64.img
initramfs-3.10.0-957.el7.x86_64kdump.img
symvers-3.10.0-957.21.3.el7.x86_64.gz
symvers-3.10.0-957.el7.x86_64.gz
.efi
efi/EFI/centos/
...
...
grub2/locale/tr.mo
grub2/locale/uk.mo
grub2/locale/vi.mo
grub2/locale/zh_CN.mo
grub2/locale/zh_TW.mo
sent 190,101,501 bytes received 6,410 bytes 14,082,067.48 bytes/sec
total size is 207,914,035 speedup is 1.09
2.4 實驗二使用rsyncd服務進行數據備份
使用rsyncd服務進行數據備份時,需要使用系統配置文件/etc/rsyncd.conf文件,創建備份賬戶,最后把rsync以deamon方式運行
- rsyncd.conf配置文件說明
centos7一般默認都有/etc/rsyncd.conf文件,centos6下需要自己創建
配置文件分為兩部分:全局參數,模塊參數
全局參數:對rsync服務器生效,如果模塊參數和全局參數沖突,沖突的地方模塊參數生效
模塊參數:定義需要通過rsync輸出的目錄定義的參數
常見的全局參數:
參數 | 說明 |
---|---|
port | 指定后台程序使用的端口號,默認為873。 |
uid | 該選項指定當該模塊傳輸文件時守護進程應該具有的uid,配合gid選項使用可以確定哪些可以訪問怎么樣的文件權限,默認值是" nobody"。 |
gid | 該選項指定當該模塊傳輸文件時守護進程應該具有的gid。默認值為" nobody"。 |
max connections | 指定該模塊的最大並發連接數量以保護服務器,超過限制的連接請求將被告知隨后再試。默認值是0,也就是沒有限制。 |
lock file | 指定支持max connections參數的鎖文件,默認值是/var/run/rsyncd.lock。 |
motd file | " motd file"參數用來指定一個消息文件,當客戶連接服務器時該文件的內容顯示給客戶,默認是沒有motd文件的。 |
log file | " log file"指定rsync的日志文件,而不將日志發送給syslog。 |
pid file | 指定rsync的pid文件,通常指定為“/var/run/rsyncd.pid”,存放進程ID的文件位置。 |
hosts allow | 單個IP地址或網絡地址 //允許訪問的客戶機地址 |
常見的模塊參數:主要是定義服務器哪個要被同步輸出,其格式必須為“ [ 共享模塊名 ]” 形式,這個名字就是在 rsync 客戶端看到的名字,其實很像 Samba 服務器提供的共享名。而服務器真正同步的數據是通過 path 來指定的。
參數 | 說明 |
---|---|
Comment | 給模塊指定一個描述,該描述連同模塊名在客戶連接得到模塊列表時顯示給客戶。默認沒有描述定義。 |
Path | 指定該模塊的供備份的目錄樹路徑,該參數是必須指定的。 |
read only | yes為只允許下載,no為可以下載和上傳文件到服務器 |
exclude | 用來指定多個由空格隔開的多個文件或目錄(相對路徑),將其添加到exclude列表中。這等同於在客戶端命令中使用―exclude或----filter來指定某些文件或目錄不下載或上傳(既不可訪問) |
exclude from | 指定一個包含exclude模式的定義的文件名,服務器從該文件中讀取exclude列表定義,每個文件或目錄需要占用一行 |
include | 用來指定不排除符合要求的文件或目錄。這等同於在客戶端命令中使用--include來指定模式,結合include和exclude可以定義復雜的exclude/include規則。 |
include from | 指定一個包含include模式的定義的文件名,服務器從該文件中讀取include列表定義。 |
auth users | 該選項指定由空格或逗號分隔的用戶名列表,只有這些用戶才允許連接該模塊。這里的用戶和系統用戶沒有任何關系。如果" auth users"被設置,那么客戶端發出對該模塊的連接請求以后會被rsync請求challenged進行驗證身份這里使用的challenge/response認證協議。用戶的名和密碼以明文方式存放在" secrets file"選項指定的文件中。默認情況下無需密碼就可以連接模塊(也就是匿名方式)。 |
secrets file | 該選項指定一個包含定義用戶名:密碼對的文件。只有在" auth users"被定義時,該文件才有作用。文件每行包含一個username:passwd對。一般來說密碼最好不要超過8個字符。沒有默認的secures file名,注意:該文件的權限一定要是600,否則客戶端將不能連接服務器。 |
hosts allow | 指定哪些IP的客戶允許連接該模塊。定義可以是以下形式: 單個IP地址,例如:192.167.0.1,多個IP或網段需要用空格隔開, 整個網段,例如:192.168.0.0/24,也可以是192.168.0.0/255.255.255.0 “*”則表示所有,默認是允許所有主機連接。 |
hosts deny | 指定不允許連接rsync服務器的機器,可以使用hosts allow的定義方式來進行定義。默認是沒有hosts deny定義。 |
list | 該選項設定當客戶請求可以使用的模塊列表時,該模塊是否應該被列出。如果設置該選項為false,可以創建隱藏的模塊。默認值是true。 |
timeout | 通過該選項可以覆蓋客戶指定的IP超時時間。通過該選項可以確保rsync服務器不會永遠等待一個崩潰的客戶端。超時單位為秒鍾,0表示沒有超時定義,這也是默認值。對於匿名rsync服務器來說,一個理想的數字是600。 |
- 配置rsyncd.conf文件
在目標服務器128上配置rsyncd.conf文件
[root@centos7-128 ~]# vi /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example:
uid = root # 運行進程的用戶
gid = root # 運行進程的組
address = 192.168.87.128 # 監聽ip
port = 873 # 監聽端口
hosts allow = 192.168.87.0/24
# 允許同步客戶端的IP地址,可以是網段,或者用*表示所有 192.168.1.0/24或192.168.1.0/255.255.255.0,多個網段用空格隔開
use chroot = yes
# #是否囚牢,鎖定家目錄,rsync被黑之后,黑客無法再rsync運行的家目錄之外創建文件,選項設置為yes
max connections = 5 # 最大連接數
pid file = /var/run/rsyncd.pid # 進程id,自動生成
lock file = /var/run/rsync.lock # 指定max connetctions參數的鎖文件
log file = /var/log/rsync.log # 日志文件
motd file = /etc/rsyncd.motd # 客戶端登陸后所顯示提示信息的保存文件
# exclude = lost+found/
# transfer logging = yes
timeout = 100 # 等待超時時間
# ignore nonreadable = yes
# dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
# [ftp]
# path = /home/ftp
# comment = ftp export area
[wwwroot] # 共享(同步)模塊名稱
path = /web-back/ # 路徑
comment = used for web-data backup # 描述說明
read only = false # 設置文件讀寫權限,false為可讀寫,ture為只讀
list = yes # 是否容許查看模塊信息
auth users = rsyncuser # 備份使用的用戶名,和系統用戶無關
secrets file = /etc/rsync.passwd # 存放備份用戶信息的文件,格式為: 用戶名:密碼
- 創建提示文件和用戶密碼
在目標服務器128上,創建提示文件和用戶密碼文件
[root@centos7-128 ~]# echo "Welcome to Rsyncd Backup Server" >> /etc/rsyncd.motd # 編輯motd文件
[root@centos7-128 ~]# vim /etc/rsync.passwd # 編輯備份用戶信息文件
rsyncuser:password123
[root@centos7-128 ~]# chmod 600 /etc/rsync.passwd
# 目錄權限必須是700或者600,否則的話身份驗證會失效.
- 啟動服務測試
在目標服務器128上,啟動xinetd和rsyncd服務
[root@centos7-128 ~]# systemctl start xinetd # 啟動xinetd服務
[root@centos7-128 ~]# systemctl enable xinetd # 容許xinetd服務開機啟動
[root@centos7-128 ~]# rsync --daemon --config=/etc/rsyncd.conf
[root@centos7-128 ~]# netstat -nlntp|grep 873
tcp 0 0 192.168.87.128:873 0.0.0.0:* LISTEN 11597/rsync
[root@centos7-128 ~]# rm -rf /web-back/* # 刪除/web-back/下的原有文件,准備測試
在源服務器127上啟動測試,向128推送數據
語法:
pull拉數據: rsync 選項 用戶名@備份源服務器IP::共享模塊名 目標目錄
push推數據: rsync 選項 源目錄 用戶名@備份源服務器IP::共享模塊名
[root@centos7-127 ~]# rsync -avz --delete /var/www/html rsyncuser@192.168.87.128::wwwroot
Welcome to Rsyncd Backup Server
Password: # 手動輸入rsync數據傳輸用戶rsyncuser的密碼
sending incremental file list
html/
html/System.map-3.10.0-957.21.3.el7.x86_64
html/System.map-3.10.0-957.el7.x86_64
html/config-3.10.0-957.21.3.el7.x86_64
html/config-3.10.0-957.el7.x86_64
html/initramfs-0-rescue-1d6dc8e931344828b171ea7fe67c3aa2.img
html/initramfs-3.10.0-957.21.3.el7.x86_64.img
html/initramfs-3.10.0-957.21.3.el7.x86_64kdump.img
...
...
html/grub2/locale/uk.mo
html/grub2/locale/vi.mo
html/grub2/locale/zh_CN.mo
html/grub2/locale/zh_TW.mo
sent 190,101,514 bytes received 6,419 bytes 5,849,474.86 bytes/sec
total size is 207,914,035 speedup is 1.09 # 傳輸成功完成,測試成功
- 自動密碼處理
在使用rsync傳輸數據時,需要輸入傳輸用戶的密碼,其實可以通過新建一個文件保存好密碼,然后在rsync命令中使用--password-file指定此文件即可完成密碼的自動輸入。如下:
[root@centos7-127 ~]# vim /etc/rsync.passwd # 在源服務器配置存放密碼的文件rsync.passwd,這里文件名可以自己定義
password123
[root@centos7-127 ~]# chmod 600 /etc/rsync.passwd # 修改密碼文件的權限
[root@centos7-127 ~]# rsync -avz --delete --password-file=/etc/rsync.passwd /var/www/html rsyncuser@192.168.87.128::wwwroot
# 加入--password--file參數,指定密碼文件,完成自動輸入密碼測試
Welcome to Rsyncd Backup Server
sending incremental file list
html/
html/System.map-3.10.0-957.21.3.el7.x86_64
html/System.map-3.10.0-957.el7.x86_64
html/config-3.10.0-957.21.3.el7.x86_64
html/config-3.10.0-957.el7.x86_64
html/symvers-3.10.0-957.21.3.el7.x86_64.gz
html/symvers-3.10.0-957.el7.x86_64.gz
html/vmlinuz-0-rescue-1d6dc8e931344828b171ea7fe67c3aa2
html/vmlinuz-3.10.0-957.21.3.el7.x86_64
html/vmlinuz-3.10.0-957.el7.x86_64
html/efi/
...
...
html/grub2/locale/pl.mo
html/grub2/locale/pt_BR.mo
html/grub2/locale/ru.mo
html/grub2/locale/sl.mo
html/grub2/locale/sv.mo
html/grub2/locale/tr.mo
html/grub2/locale/uk.mo
html/grub2/locale/vi.mo
html/grub2/locale/zh_CN.mo
html/grub2/locale/zh_TW.mo
sent 26,786,735 bytes received 6,324 bytes 10,717,223.60 bytes/sec
total size is 43,571,771 speedup is 1.63 # 傳輸完成,測試成功
- 使用shell腳本實現自動備份
[root@centos7-127 ~]# vim autobackup.sh
#!/bin/bash
SOURDIR="/var/www/html" # 設置源服務器路徑
BACKUPUSER="rsyncuser" # 設置備份用戶
DESTADDR="192.168.87.128" # 設置遠程服務器
OBJECT="wwwroot" # 設置備份項目名稱
SECUREFILE="/opt/passfile" #
rsync -avz --delete $SOURDIR $BACKUPUSER@$DESTADDR::$OBJECT --password-file=$SECUREFILE
[root@centos7-127 ~]# chmod +x autobackup.sh 給shell腳本可執行權限
[root@centos7-127 ~]# vim /opt/passfile
password123
[root@centos7-127 ~]# chmod 600 /opt/passfile #修改密碼文件權限
[root@centos7-127 ~]# echo "1 3 * * * sh /root/autobackup.sh &" >> /var/spool/cron/root
# 設置每天的3點過1分自動執行備份腳本
3、Rsync+sersync實現數據實時同步
3.1 rsync+sersync架構作用
- sersync是基於inotify開發的,類似於inotify-tools的工具
- sersync可以記錄下被監聽目錄中發生變化的(包括增加、刪除、修改)具體某一個文件或者某一個目錄的名字,然后使用rsync同步的時候,只同步發生變化的文件或者目錄
3.2 rsync+inotify-tools與rsync+sersync架構的區別
- rsync+inotify-tools
- inotify只能記錄下被監聽的目錄發生了變化(增,刪,改)並沒有把具體是哪個文件或者哪個目錄發生了變化記錄下來;
- rsync在同步的時候,並不知道具體是哪個文件或目錄發生了變化,每次都是對整個目錄進行同步,當數據量很大時,整個目錄同步非常耗時(rsync要對整個目錄遍歷查找對比文件),因此效率很低
- rsync+sersync
- sersync可以記錄被監聽目錄中發生變化的(增,刪,改)具體某個文件或目錄的名字;
- rsync在同步時,只同步發生變化的文件或目錄(每次發生變化的數據相對整個同步目錄數據來說很小,rsync在遍歷查找對比文件時,速度很快),因此效率很高。
3.3 同步過程和原理
- 原理
- 在同步服務器上開啟sersync服務,sersync負責監控配置路徑中的文件系統事件變化;
- 調用rsync命令把更新的文件同步到目標服務器;
- 需要在主服務器配置sersync,在同步目標服務器配置rsync server(注意:是rsync服務)
- 同步過程
- 用戶實時的往sersync服務器上寫入更新文件數據;
- 此時需要在同步主服務器上配置sersync服務;
- 在另一台服務器開啟rsync守護進程服務,以同步拉取來自sersync服務器上的數據;
3.4 實驗模擬實現rsync+sersync架構
3.4.1 環境說明
IP | 系統 | 說明 |
---|---|---|
192.168.87.127 | centos7 | Sersync服務器(數據源機器) |
192.168.87.128 | centos7 | Rsync服務器(備份端,目標機器) |
3.4.2 部署程序
1、下載sersync
在google code下載sersync的可執行文件版本,里面有配置文件與可執行文件
wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz(有時下載失敗,所有要本地留存才行)
附sersync度娘下載地址:鏈接:https://pan.baidu.com/s/1nB_QCbWi-g_ISeqYYWZLUA 提取碼:jks8
上傳到服務器 /opt 目錄下
[root@centos7-127 ~]# mv ./sersync2.5.4_64bit_binary_stable_final.tar.gz /opt
# 上傳sersync安裝包到/opt
[root@centos7-127 ~]# cd /opt
[root@centos7-127 opt]# tar -xzvf sersync2.5.4_64bit_binary_stable_final.tar.gz # 解壓安裝包
GNU-Linux-x86/
GNU-Linux-x86/sersync2
GNU-Linux-x86/confxml.xml
[root@centos7-127 opt]# mv GNU-Linux-x86 sersync # 改名為sersync
2、配置sersync
[root@centos7-127 sersync]# cp confxml.xml confxml.xml.bak # 備份配置文件
[root@centos7-127 sersync]# vim confxml.xml #修改sersync配置文件
...
23 <sersync>
24 <localpath watch="/var/www/html">
# 配置監聽目錄localpath wahch為需要備份的目錄
25 <remote ip="192.168.87.128" name="wwwroot"/>
# 配置remote ip為遠端服務器地址,name為備份項目名稱
26 <!--<remote ip="192.168.8.39" name="tongbu"/>-->
27 <!--<remote ip="192.168.8.40" name="tongbu"/>-->
28 </localpath>
29 <rsync>
30 <commonParams params="-artuz"/>
31 <auth start="ture" users="rsyncuser" passwordfile="/etc/rsync.passwd"/>
# 配置auth start為ture(開啟),users為備份數據用戶,passwordfile為密碼文件
32 <userDefinedPort start="false" port="874"/><!-- port=874 -->
33 <timeout start="start" time="100"/><!-- timeout=100 -->
# 配置timeout start為ture(超時斷開是否啟動)time為超時斷開時間按需求配置
34 <ssh start="false"/>
...
3、開啟sersync守護進程同步數據
[root@centos7-127 ~]# /opt/sersync/sersync2 -d -r -o /opt/sersync/confxml.xml
4、測試
在127 /var/www/html/ 目錄 增刪改目錄文件,
看128 /web-back 目錄的變化
[root@centos7-128 html]# watch ls -l
5、設置sersync監控開機自動執行
vi /etc/rc.d/rc.local # 編輯,在最后添加一行
/opt/sersync/sersync2 -d -r -o /opt/sersync/confxml.xml #設置開機自動運行腳本
vi /opt/check_sersync.sh # 編輯,添加以下代碼
#!/bin/sh
sersync="/opt/sersync/sersync2"
confxml="/opt/sersync/confxml.xml"
status=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l)
if [ $status -eq 0 ];
then
$sersync -d -r -o $confxml &
else
exit 0;
fi
chmod +x /opt/check_sersync.sh # 添加腳本執行權限
把這個腳本加到任務計划,定期執行檢測
補充: 多實例情況
1、配置多個confxml.xml文件(比如:www、bbs、blog....等等)
2、根據不同的需求同步對應的實例文件
/usr/local/sersync/sersync2 -d -o /usr/local/sersync/www_confxml.xml
/usr/local/sersync/sersync2 -d -o /usr/local/sersync/bbs_confxml.xml
2019-9-18 23:15:16