第1章 rsync 軟件介紹
1.1 什么是rsync
rsync 是一款開源的、快速的、多功能的、可實現全量及增量的本地或遠程數據同步備份的優秀工具。
http://www.samba.org/ftp/rsync/rsync.html
1.1.1 全量及增量
全量:將全部數據,進行傳輸覆蓋
增量:只傳輸差異部分的數據
1.2 實現增量復制的原理
Rsync通過其獨特的“quick check”算法,實現增量傳輸數據
[root@backup ~]#man rsync Rsync finds files that need to be transferred using a “quick check” algorithm (by default) that looks for files that have changed in size or in last-modified time. Any changes in the other preserved attributes (as requested by options) are made on the destination file directly when the quick check indicates that the file’s data does not need to be updated.
在同步備份數據時,默認情況下,Rsync通過其獨特的“quick check”算法,它僅同步大小或者最后修改時間發生變化的文件或目錄,當然也可根據權限,屬主等屬性的變化同步,但需要指定相應的參數,甚至可以實現只同步一個文件里有變化的內容部分,所以,可以實現快速的同步備份數據。
centos 5 rsync 2.x 先比對再同步,速度較慢
centos 6 rsync 3.x 一邊比對,一邊對差異部分進行同步
1.2.1 軟件版本
[root@backup ~]# rsync --version rsync version 3.0.6 protocol version 30 Copyright (C) 1996-2009 by Andrew Tridgell, Wayne Davison, and others. Web site: http://rsync.samba.org/ Capabilities: 64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints, socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace, append, ACLs, xattrs, iconv, symtimes rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the GNU General Public Licence for details.
1.3 rsync 軟件功能介紹
類似於 cp 命令 -- 實現本地備份傳輸數據
類似於scp 命令 -- 遠程備份傳輸數據
類似於 rm 命令 -- 實現無差異同步備份
類似於 ls 命令 -- 本地文件信息查看
rsync 命令屬於1 v 4 的命令
1.3.1 rsync == cp
[root@backup ~]# cp -a /etc/hosts /tmp/ [root@backup ~]# ls /tmp/ hosts [root@backup ~]# \rm /tmp/hosts [root@backup ~]# rsync /etc/hosts /tmp/ [root@backup ~]# ls /tmp/hosts /tmp/hosts
1.3.2 rsync == scp
#使用scp實現
#檢查對端服務器目標位置上是否有該文件
[root@nfs01 ~]# ls /tmp/hosts ls: cannot access /tmp/hosts: No such file or directory
#從本地拷貝到遠端服務器上
[root@backup ~]# ls /tmp/hosts /tmp/hosts [root@backup ~]# scp -rp /etc/hosts 10.0.0.31:/tmp/ The authenticity of host '10.0.0.31 (10.0.0.31)' can't be established. RSA key fingerprint is d3:41:bb:0d:43:88:da:a3:2c:e8:36:91:11:c9:e4:9c. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '10.0.0.31' (RSA) to the list of known hosts. root@10.0.0.31's password: hosts 100% 357 0.4KB/s 00:00
#檢查遠端服務器上的結果
[root@nfs01 ~]# ls /tmp/hosts /tmp/hosts
#使用rsync 實現
[root@backup ~]# rsync -rp /etc/hosts 10.0.0.31:/tmp/ root@10.0.0.31's password: [root@backup ~]# [root@nfs01 ~]# ls /tmp/hosts /tmp/hosts
1.3.3 rsync == rm
1.3.3.1 環境准備
創建出來一次命令 進行操作
[root@backup tmp]# mkdir /znix [root@backup znix]# cp /tmp/* . [root@backup znix]# ll total 4 -rw-r--r-- 1 root root 357 Oct 11 15:21 hosts
#rm命令操作
[root@backup znix]# rm -rf /znix/hosts [root@backup znix]# ll /znix/hosts ls: cannot access /znix/hosts: No such file or directory
查看這文件
[root@backup ~]# l total 4 -rw-r--r-- 1 root root 357 Oct 11 15:21 hosts
創建一個空目錄,使用空目錄進行無差異同步
[root@backup ~]# mkdir /null [root@backup ~]# rsync --delete /null/ /znix/ rsync: --delete does not work without -r or -d. rsync error: syntax or usage error (code 1) at main.c(1422) [client=3.0.6] [root@backup ~]# rsync -a --delete /null/ /znix/ [root@backup ~]# ll /znix/ total 0
1.3.4 rsync == ls -l
使用rsync 可以實現與 ls 類似的功能
[root@backup ~]# ls -l install.log -rw-r--r--. 1 root root 21736 Sep 25 08:38 install.log [root@backup ~]# rsync install.log -rw-r--r-- 21736 2017/09/25 08:38:28 install.log
1.4 Rsync的特性總結(7個特性信息說明)
01. 支持拷貝普通文件與特殊文件如鏈接文件,設備等。 02. 可以有排除指定文件或目錄同步的功能,相當於打包命令tar的排除功能。 #tar zcvf backup_1.tar.gz /opt/data -exclude=clsn 說明:在打包/opt/data時就排除了clsn命名的目錄和文件。 03. 可以做到保持原文件或目錄的權限、時間、軟硬鏈接、屬主、組等所有屬性均不改變-p。 04. 可實現增量同步,既只同步發生變化的數據,因此數據傳輸效率很高(tar -N)。 # 將備份/home 目錄自 2008-01-29 以來修改過的文件 # tar -N 2008-01-29 -zcvf /backups/inc-backup_$(date +%F).tar.gz /home # 將備份 /home 目錄昨天以來修改過的文件 # tar -N $(date -d yesterday "+%F") -zcvf /backups/inc-backup_$(date +%F).tar.gz /home # 添加文件到已經打包的文件 # tar -rf all.tar *.gif 說明:這條命令是將所有.gif的文件增加到all.tar的包里面去。-r是表示增加文件的意思。 05. 可以使用rcp,rsh,ssh等方式來配合進行隧道加密傳輸文件(rsync本身不對數據加密) 06. 可以通過socket(進程方式)傳輸文件和數據(服務端和客戶端)*****。重點掌握 07. 支持匿名的或認證(無需系統用戶)的進程模式傳輸,可實現方便安全的進行數據備份及鏡像。
1.5 Rsync的企業工作場景說明
01. 兩台服務器之間數據同步(定時任務cron+rsync)
同步網站內部人員數據信息(定時任務最小周期為1分鍾)
02. 兩台服務器之間數據同步(實時任務inotify/sersync/lrsyncd+rsync)
同步網站用戶人員數據信息
第2章 rsync使用方式
2.1 rsync軟件工作方式
SYNOPSIS 本地數據同步方式 Local: rsync [OPTION...] SRC... [DEST] 遠程數據同步方式 Access via remote shell: Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST] Push: rsync [OPTION...] SRC... [USER@]HOST:DEST 守護進程方式同步數據 Access via rsync daemon: Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST] rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST] Push: rsync [OPTION...] SRC... [USER@]HOST::DEST rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
2.1.1 本地數據同步方式(類似於cp)
Local: rsync [OPTION...] SRC... [DEST]
參數 |
含義 |
rsync |
數據同步命令 |
[OPTION...] |
rsync命令參數信息 |
SRC |
要同不得數據信息(文件或目錄) |
[DEST] |
將數據傳輸到什么位置 |
2.1.1.1 實例演示命令:
[root@backup ~]# rsync /etc/hosts /tmp/ [root@backup ~]# ls /tmp/hosts /tmp/hosts
2.1.2 遠程數據同步方式(類似scp)---又稱為隧道傳輸
Access via remote shell:
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
說明:需要進行交互傳輸數據。如果想實現免交互傳輸數據,需要借助ssh+key方式實現
pull: 拉: |
|
[USER@] : |
以什么用戶身份傳輸數據信息 |
HOST: |
遠程主機信息(IP地址信息 主機名稱信息) |
SRC: |
遠端要恏過來的數據信息 |
[dest] |
恏到本地什么位置 |
push:推: |
|
SRC: |
本地要懟過去的數據信息 |
DEST |
懟到遠端什么位置 |
2.1.3 【實踐操作】pull 拉
從遠端拉文件到當前目錄
[root@nfs01 ~]# touch /tmp/1.txt [root@backup ~]# rsync nfs01:/tmp/1.txt . root@nfs01's password: [root@backup ~]# ll total 44 -rw-r--r-- 1 root root 0 Oct 11 16:16 1.txt
2.1.4 【實踐操作】push 推 (目錄)
將本地的hosts文件推到遠端服務器上
[root@backup tmp]# ll total 4 -rw-r--r-- 1 root root 357 Oct 11 15:12 hosts
使用push的格式 推整個目錄(包括目錄)
[root@backup tmp]# rsync -r /tmp nfs01:/tmp/ root@nfs01's password: [root@nfs01 tmp]# ll total 4 drwxr-xr-x 3 root root 4096 Oct 11 16:20 tmp
推整個目錄下的文件(不包括目錄本身)
[root@backup tmp]# rsync -r /tmp/ nfs01:/tmp/ root@nfs01's password: [root@nfs01 tmp]# ll total 8 -rw-r--r-- 1 root root 357 Oct 11 16:21 hosts drwxr-xr-x 3 root root 4096 Oct 11 16:20 tmp
說明:
/tmp --表示將tmp目錄本身及目錄下的內容進行傳輸
/tmp/ --表示只傳輸tmp目錄下面的內容信息
2.2 守護進程方式同步數據
[root@localhost ~]# uname -a Linux 2.6.32-696.el6.x86_64 #1 SMP Tue Mar 21 19:29:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux Access via rsync daemon: Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST] rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST] Push: rsync [OPTION...] SRC... [USER@]HOST::DEST rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
2.2.1 配置rsync守護進程方式(需要有服務端與客戶端)
規划:
1、backup服務器作為rsync服務端
2、以rysnc客戶端作為參照物,將數據推到rsync服務器上
2.2.2 配置rsync服務端(將服務端配置到 backup 服務器上)
第一個里程碑: 軟件是否存在
[root@backup ~]# rpm -qa |grep rsync rsync-3.0.6-12.el6.x86_64
第二個里程碑: 進行軟件服務配置
[root@backup ~]# vim /etc/rsyncd.conf uid = rsync gid = rsync use chroot = no max connections = 200 timeout = 300 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log ignore errors read only = false list = false hosts allow = 172.16.1.0/24 hosts deny = 0.0.0.0/32 auth users = rsync_backup secrets file = /etc/rsync.password [backup] comment = "backup dir by clsn" path = /backup
第三個里程:創建rsync用戶
[root@backup ~]# id rsync id: rsync: No such user [root@backup ~]# useradd -s /sbin/nologin -M rsync
第四個里程碑: 創建數據備份儲存目錄,目錄修改屬主
[root@backup ~]# mkdir /backup/ [root@backup ~]# chown -R rsync.rsync /backup/
第五個里程碑: 創建認證用戶密碼文件
echo "rsync_backup:clsn123" >>/etc/rsync.password chmod 600 /etc/rsync.password
第六個里程碑: 啟動rsync服
rsync --daemon
至此服務端配置完成
[root@backup ~]# ps -ef |grep rsync root 2076 1 0 17:05 ? 00:00:00 rsync --daemon root 2163 1817 0 17:38 pts/1 00:00:00 grep --color=auto rsync [root@backup ~]# netstat -lntup |grep rsync tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 2076/rsync tcp 0 0 :::873 :::* LISTEN 2076/rsync
2.2.3 配置rsync客戶端(其他服務器為客戶端)
第一個里程碑: 軟件是否存在
[root@nfs01 ~]# rpm -qa |grep rsync rsync-3.0.6-12.el6.x86_64
第二個里程碑: 創建認證文件
客戶端的認證文件只需要有密碼即可
echo "clsn123" >>/etc/rsync.password chmod 600 /etc/rsync.password
第三個里程碑: 實現數據傳輸
交互式
[root@nfs01 ~]# rsync -avzP /etc/hosts rsync_backup@172.16.1.41::backup Password: sending incremental file list hosts 357 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1) sent 63 bytes received 33 bytes 9.14 bytes/sec total size is 357 speedup is 3.72
免交互式
[root@nfs01 ~]# rsync -avzP /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password sending incremental file list hosts 357 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1) sent 199 bytes received 27 bytes 150.67 bytes/sec total size is 357 speedup is 1.58
2.3 rsync守護進程傳輸數據原理
2.4 rsync 命令同步參數選項&特殊參數
目錄參數 |
參數說明 |
-v ,--verbose |
詳細模式輸出,傳輸時的信息 |
-z,--compress |
傳輸時進行壓縮以提高傳輸效率 --compress-level=NUM 可按級別壓縮 局域網可以不用壓縮 |
-a,--archive (主要) |
歸檔模式,表示以遞歸方式傳輸文件,並保持文件屬性。等於 -rtopgDl |
-r,--recursive 歸檔於-a |
對子目錄以遞歸模式,即目錄下的所有目錄都同樣傳輸。小寫r |
-t,--times 歸檔於-a |
保持文件時間信息 |
-o,--owner 歸檔於-a |
保持文件屬主信息 |
-p,--perms 歸檔於-a |
保持文件權限 |
-g,--group 歸檔於-a |
保持文件屬組信息 |
-P,--progress |
顯示同步的過程及傳輸時的進度等信息(大P) |
-D,--devices 歸檔於-a |
保持設備文件信息 |
-l,--links 歸檔於-a |
保留軟連接(小寫字母l) |
-e,--rsh=COMMAND |
使用的信道協議(remote shell),指定替代rsh的shell程序。 例如 ssh |
--exclude=PATTERN |
指定排除不需要傳輸的文件信息 |
--exclude-from=file |
文件名所在目錄文件,即可以實現排除多個文件 |
--bwlimit=RATE |
限速功能 |
--delete |
讓目標目錄SRC和源目錄數據DST一致,即無差異數據同步 |
保持同步目錄及文件屬性: 這里的-avzP相當於 -vzetopdDlP,生產環境常用的參數為 -avzP 在腳本中可以報-vP去掉 --progress可以用-P代替 |
|
daemon啟動擴展參數 |
|
--daemon |
daemon表示以守護進程的方式啟動rsync服務。 |
--address |
綁定指定IP地址提供服務。 |
--config=FILE |
更改配置文件路徑,而不是默認的/etc/rsyncd.conf |
--port=PORT |
更改其它端口提供服務,而不是缺省的873端口 |
2.4.1 特殊參數實踐
指定ip:
[root@backup ~]# rsync --daemon --address=172.16.1.41 [root@backup ~]# netstat -lntup |grep 873 tcp 0 0 172.16.1.41:873 0.0.0.0:* LISTEN 2583/rsync 參數測試: [root@nfs01 ~]# rsync -avzP /etc/services rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password sending incremental file list services 641020 100% 19.34MB/s 0:00:00 (xfer#1, to-check=0/1) sent 127417 bytes received 27 bytes 254888.00 bytes/sec total size is 641020 speedup is 5.03
指定配置文件路徑
[root@backup ~]# rsync --daemon --config=/etc/rsyncd.conf [root@nfs01 ~]# rsync -avzP /etc/services rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password sending incremental file list sent 29 bytes received 8 bytes 74.00 bytes/sec total size is 641020 speedup is 17324.86
服務端指定服務端口:
[root@backup ~]# rsync --daemon --port=5222 [root@backup ~]# netstat -lntup|grep rsync tcp 0 0 0.0.0.0:5222 0.0.0.0:* LISTEN 2598/rsync tcp 0 0 :::5222 :::* LISTEN 2598/rsync
第3章 rsycn配置文件詳解 rsyncd.conf
3.1 部分知識補充
3.1.1 配置文件內容參考資料
man rsyncd.conf
3.1.2 配置文件內容總結
模塊之上內容為全局變量信息
模塊之下內容為局部變量信息
說明:
無論是全局變量發生變化,還是局部變量發生變化,都建議重啟rsync服務使配置生效。
3.2 利用/etc/init.d/啟動rsync服務方式
3.2.1 可以實現方式:
a. 編寫rsync啟動腳本(有一定的shell能力 if case) b. 利用xinetd服務,管理啟動rsync服務
3.2.2 利用 xinetd服務 管理rsync
第一個里程碑: 安裝xinetd軟件
[root@backup ~]# yum install -y xinetd [root@backup ~]# rpm -qa |grep xin xinetd-2.3.14-40.el6.x86_64
第二個里程碑:編輯配置文件
修改disable = yes 改為disable = no
[root@backup ~]# vim /etc/xinetd.d/rsync # default: off # description: The rsync server is a good addition to an ftp server, as it \ # allows crc checksumming etc. service rsync { disable = no flags = IPv6 socket_type = stream wait = no user = root server = /usr/bin/rsync server_args = --daemon log_on_failure += USERID }
第三個里程碑:重啟xinetd服務
[root@backup ~]# /etc/init.d/xinetd restart Stopping xinetd: [ OK ] Starting xinetd: [ OK ]
傳輸測試
[root@nfs01 ~]# rsync -avzP /etc/services rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password sending incremental file list sent 29 bytes received 8 bytes 74.00 bytes/sec total size is 641020 speedup is 17324.86
3.3 定義變量信息實現免秘鑰交互
3.3.1 通過man手冊獲得方法
Some modules on the remote daemon may require authentication. If so, you will receive a password prompt when you connect. You can avoid the password prompt by setting the environment variable RSYNC_PASSWORD to the password you want to use or using the --password-file option. This may be useful when scripting rsync. WARNING: On some systems environment variables are visible to all users. On those systems using --password-file is recommended.
在遠程進程的一些模塊可能需要認證。如果是這樣的話,你將得到一個密碼提示當您連接。你可以通過設置環境變量rsync_password要使用或使用密碼文件選項密碼避免密碼提示。這可能是有用的腳本文件。
警告:在一些系統環境變量,對所有用戶都是可見的。在這些系統中使用的密碼文件的建議。
3.3.2 使用 RSYNC_PASSWORD 變量實現免交互
未設置變量之前
[root@nfs01 ~]# rsync -avzP /etc/services rsync_backup@172.16.1.41::backup Password:
添加上環境變量
[root@nfs01 ~]# export RSYNC_PASSWORD=clsn123
測試
[root@nfs01 ~]# rsync -avzP /etc/services rsync_backup@172.16.1.41::backup sending incremental file list sent 29 bytes received 8 bytes 24.67 bytes/sec total size is 641020 speedup is 17324.86
3.4 守護進程多模塊功能配置
第一個里程碑: 編寫配置信息創建多模塊
[root@backup ~]# vim /etc/rsyncd.conf …… [nfsdata] comment = "nfsdata dir by clsn" path = /backup/nfsdata [nfsbackup] comment = "nfsbackup dir by clsn" path = /backup/nfsbackup
第二個里程碑: 創建多模塊指定的目錄
# 創建目錄,並修改目錄的權限 [root@backup ~]# mkdir /backup/nfs{data,backup} -p [root@backup ~]# chown rsync.rsync /backup/nfs{data,backup}
#查看: [root@backup ~]# ll /backup/nfs{data,backup} -d drwxr-xr-x 2 rsync rsync 4096 Oct 12 10:05 /backup/nfsbackup drwxr-xr-x 2 rsync rsync 4096 Oct 12 10:05 /backup/nfsdata
第三里程碑: 利用rsync客戶端進行測試
[root@nfs01 ~]# rsync -avz /data/ rsync_backup@172.16.1.41::nfsdata --password-file=/etc/rsync.passsword sending incremental file list ./ nfs.data sent 78 bytes received 30 bytes 216.00 bytes/sec total size is 0 speedup is 0.00
說明:
rsyncd.conf配置文件中,添加多模塊信息,可以不用重啟rsync服務,即時生效~
全局變量參數針對所有模塊生效;局部變量參數只針對指定模塊生效
read only參數默認配置為ture,即為只讀模式
全局變量發生變化,不用重啟rsync服務;局部變量發生變化,需要重啟rsync服務
注意:修改配置文件就重啟↓
無論是全局變量發生變化,還是局部變量發生變化,都建議重啟rsync服務使配置生效
3.5 守護進程的排除功能實踐
3.5.1 排除的方式
a) --exclude=要配置的目錄或文件名稱
b) --exclude-from=要排除多個目錄或文件匯總文件名稱
c) 在配置文件中進行修改,指定要排除的信息
3.5.2 排除測試
第一個里程碑: 創建模擬測試環境
[root@nfs01 data]# mkdir {a..d} [root@nfs01 data]# touch {a..d}/{1..3}.txt [root@nfs01 data]# tree . ├── a │ ├── 1.txt │ ├── 2.txt │ └── 3.txt ├── b │ ├── 1.txt │ ├── 2.txt │ └── 3.txt ├── c │ ├── 1.txt │ ├── 2.txt │ └── 3.txt └── d ├── 1.txt ├── 2.txt └── 3.txt 4 directories, 12 files
第二個里程碑 利用 --exclude參數測試排除功能
# 需求:不要a目錄中3.txt 不要b、c目錄 [root@nfs01 data]# rsync -avz /data/ --exclude=a/3.txt --exclude=b --exclude=c rsync_backup@172.16.1.41::nfsdata sending incremental file list ./ a/ a/1.txt a/2.txt d/ d/1.txt d/2.txt d/3.txt sent 300 bytes received 114 bytes 828.00 bytes/sec total size is 0 speedup is 0.00
精簡方式排除
[root@nfs01 data]# rsync -avz /data/ --exclude=a/3.txt --exclude={b,c} rsync_backup@172.16.1.41::nfsdata sending incremental file list ./ a/ a/1.txt a/2.txt d/ d/1.txt d/2.txt d/3.txt sent 300 bytes received 114 bytes 828.00 bytes/sec total size is 0 speedup is 0.00
3.5.3 利用--exclude-from 方式進行排除
第一個里程碑: 創建模擬測試環境
[root@nfs01 data]# mkdir {a..d} [root@nfs01 data]# touch {a..d}/{1..3}.txt
第二個里程碑:利用--exlude-from參數,測試排除功能
[root@nfs01 data]# vim /tmp/paichu.txt a/3.txt b c
第三個里程碑:進行排除
[root@nfs01 data]# rsync -avz /data/ --exclude-from=/tmp/paichu.txt rsync_backup@172.16.1.41::nfsdata sending incremental file list ./ a/ a/1.txt a/2.txt d/ d/1.txt d/2.txt d/3.txt sent 300 bytes received 114 bytes 828.00 bytes/sec total size is 0 speedup is 0.00
說明:
01. 排除文件中,需要利用相對路徑指定排除信息(不能利用絕對路徑)
02. 相對路徑指的是相對同步的目錄信息而言,是對rsync -avz /data/ 后面的data目錄進行相對
3.5.4 在配置文件中修改要排除的文件
第一個里程碑: 編寫修改服務端配置文件
vim /etc/rsyncd.conf [nfsdata] comment = "nfsdata dir by clsn" path = /backup/nfsdata exclude=a/3.txt b c
第二個里程碑:重啟rsync服務
killall rsync && sleep 1 && rsync --daemon
第三里程碑: 進行測試
[root@nfs01 data]# rsync -avz /data/ rsync_backup@172.16.1.41::nfsdata sending incremental file list ./ a/ a/1.txt a/2.txt skipping daemon-excluded file "a/3.txt" skipping daemon-excluded directory "b" *** Skipping any contents from this failed directory *** skipping daemon-excluded directory "c" *** Skipping any contents from this failed directory *** d/ d/1.txt d/2.txt d/3.txt sent 407 bytes received 116 bytes 1046.00 bytes/sec total size is 0 speedup is 0.00 rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]
3.6 守護進程來創建備份目錄
通過客戶端命令創建服務端備份目錄中子目錄
# 推送/etc/services文件到 服務器/backup/sda/目錄 [root@nfs01 ~]# rsync -avzP /etc/services rsync_backup@172.16.1.41::backup/dba/ sending incremental file list created directory dba services 641020 100% 19.34MB/s 0:00:00 (xfer#1, to-check=0/1)
# 推送/etc/services文件到 服務器/backup/sa/目錄 sent 127417 bytes received 27 bytes 254888.00 bytes/sec total size is 641020 speedup is 5.03 [root@nfs01 ~]# rsync -avzP /etc/services rsync_backup@172.16.1.41::backup/sa/ sending incremental file list created directory sa services 641020 100% 19.34MB/s 0:00:00 (xfer#1, to-check=0/1)
# 推送/etc/services文件到 服務器/backup/dev/目錄 sent 127417 bytes received 27 bytes 254888.00 bytes/sec total size is 641020 speedup is 5.03 [root@nfs01 ~]# rsync -avzP /etc/services rsync_backup@172.16.1.41::backup/dev/ sending incremental file list created directory dev services 641020 100% 18.71MB/s 0:00:00 (xfer#1, to-check=0/1) sent 127417 bytes received 27 bytes 254888.00 bytes/sec total size is 641020 speedup is 5.03
檢查結果:
[root@backup backup]# tree . ├── dba │ └── services ├── dev │ └── services └── sa └── services
說明:
a 目標目錄名稱后要加上 "/", 表示創建目錄,否則變為修改傳輸文件名稱了
b 利用客戶端創建服務備份子目錄時,只能創建一級子目錄。
3.7 守護進程的訪問控制配置
第一個里程碑:在服務端配置文件,編寫白名單策略或黑名單策略(只能取其一)
vim /etc/rsyncd.conf hosts allow = 172.16.1.0/24 #hosts deny = 0.0.0.0/32
關於訪問控制的說明:
01. 白名單和黑名單同時存在時,默認控制策略為不匹配的傳輸數據信息全部放行
02. 白名單單一存在時,默認控制策略為不匹配的傳輸數據信息全部禁止
03. 黑名單單一存在時,默認控制策略為不匹配的傳輸數據信息全部放行
全局變量修改控制策略信息,rsync服務必須重啟
第二個里程碑:客戶端進行測試
[root@nfs01 backup]# rsync -avz /etc/services rsync_backup@10.0.0.41::data @ERROR: Unknown module 'data' rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6] -------------------------------------------------------------------------------- [root@nfs01 backup]# rsync -avz /etc/services sync_backup@172.16.1.41::data sending incremental file list sent 29 bytes received 8 bytes 74.00 bytes/sec total size is 641020 speedup is 17324.86
3.8 守護進程無差異同步配置
3.8.1 什么是無差異:
推模式:我有什么,你就有什么;我沒有,你也不能有
拉模式:你有什么,我就有什么;你沒有,我也不能有
總結:服務端客戶端數據完全一致(一模一樣)
3.8.2 實現無差異同步方法
第一個里程碑: 創建實驗環境
[root@nfs01 ~]# ll /data/ total 16 drwxr-xr-x 2 root root 4096 Oct 12 10:29 a drwxr-xr-x 2 root root 4096 Oct 12 10:40 b drwxr-xr-x 2 root root 4096 Oct 12 10:29 c drwxr-xr-x 2 root root 4096 Oct 12 10:29 d
第二個里程:進行第一次數據同步
[root@nfs01 ~]# rsync -avz --delete /data/ rsync_backup@172.16.1.41::backup/ sending incremental file list ./ a/ a/1.txt a/2.txt a/3.txt b/ b/1.txt b/2.txt b/3.txt c/ c/1.txt c/2.txt c/3.txt d/ d/1.txt d/2.txt d/3.txt sent 669 bytes received 255 bytes 1848.00 bytes/sec total size is 0 speedup is 0.00
第三個里程:刪除指定目錄,並添加指定文件,測試無差異功能
# 刪除客戶端中的 a/ 目錄,再進行無差異傳輸 [root@nfs01 data]# rm a/ -rf [root@nfs01 data]# rsync -avz --delete /data/ rsync_backup@172.16.1.41::backup/ sending incremental file list ./ deleting a/3.txt deleting a/2.txt deleting a/1.txt deleting a/ sent 181 bytes received 14 bytes 390.00 bytes/sec total size is 0 speedup is 0.00
3.8.3 【注意】無差異同步方法的應用
01. 實現儲存數據與備份數據完全一致(慎用)
rsync -avz --delete /data/ rsync_backup@172.16.1.41::backup /
02. 快速刪除大文件數據
1.mkdir /null --創建出一個空目錄。 2.rsync -avz --delete /null/ /bigdata/ # 刪除效率高於 rm -rf /bigdata
3.9 守護進程的列表功能配置
第一個里程碑: 在服務端配置文件中開啟list列表功能
[root@backup ~]# vim /etc/rsyncd.conf list = true
第二個里程碑:重啟rsync服務
[root@backup ~]# killall rsync && sleep 1 && rsync --daemon
第三個里程碑: 客戶端查看服務端模塊信息
[root@nfs01 data]# rsync rsync_backup@172.16.1.41:: backup "backup dir by clsn" nfsdata "nfsdata dir by clsn" nfsbackup "nfsbackup dir by clsn"
說明:
為了提升備份服務器安全性,建議關閉list列表功能
第4章 常見問題
[root@nfs01 tmp]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup Password: sending incremental file list hosts rsync: mkstemp ".hosts.U5OCyR" (in backup) failed: Permission denied (13) sent 200 bytes received 27 bytes 13.76 bytes/sec total size is 371 speedup is 1.63 rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]
說明:備份目錄權限設置不正確
解決辦法:
將服務端的備份存放目錄(path值),屬主和屬組修改為rsync。
[root@backup ~]# chown -R rsync.rsync /backup/