第1章 rsync 軟件介紹
1.1 什么是rsync
rsync 是一款開源的、快速的、多功能的、可實現全量及增量的本地或遠程數據同步備份的優秀工具。
http://www.samba.org/ftp/rsync/rsync.html
下載地址:https://download.samba.org/pub/rsync/
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 root@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 root@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)
同步網站用戶人員數據信息
1.6Rsync的安裝方式
1、yum 安裝
yum install -y rsync |
2、源碼安裝rsync-3.0.7.tar.gz
#下載rsync源碼包 mkdir /tools cd /tools wget -c https://download.samba.org/pub/rsync/src/rsync-3.0.7.tar.gz #源碼安裝部署 tar -xzf rsync-3.0.7.tar.gz cd rysnc-3.0.7 ./configure –prefix=/usr/local/rsync make make install |
3、環境變量設置
echo 'export PATH=/usr/local/rsync/bin:$PATH' >> /etc/profile source /etc/profile |
[root@yum rsync]# rsync --version rsync version 3.0.7 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, no 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. |
第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 |
第二個里程碑: 進行軟件服務配置
[root@backup ~]# vim /etc/rsyncd.conf #port=873 #默認的監聽端口為873,可修改 |
第三個里程:創建rsync用戶
[root@backup ~]# id rsync |
第四個里程碑: 創建數據備份儲存目錄,目錄修改屬主
[root@backup ~]# mkdir /backup/ |
第五個里程碑: 創建認證用戶密碼文件
echo "rsync_backup:clsn123" >>/etc/rsync.password |
第六個里程碑: 啟動rsync服
rsync --daemon |
至此服務端配置完成
[root@backup ~]# ps -ef |grep rsync |
2.2.3 配置rsync客戶端(其他服務器為客戶端)
第一個里程碑: 軟件是否存在
[root@nfs01 ~]# rpm -qa |grep rsync |
第二個里程碑: 創建認證文件
客戶端的認證文件只需要有密碼即可
echo "clsn123" >>/etc/rsync.password |
第三個里程碑: 實現數據傳輸
交互式
[root@nfs01 ~]# rsync -avzP /etc/hosts rsync_backup@172.16.1.41::backup
|
免交互式
[root@nfs01 ~]# rsync -avzP /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
|
讓目標目錄SRC和源目錄數據DST一致,即無差異數據同步--delete
[root@nfs01 ~]# rsync -avzP --delete /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password |
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章 Rsync服務常見問題匯總講解:
[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/
==================================
- rsync服務端開啟的iptables防火牆 【客戶端的錯誤】 No route to host 【錯誤演示過程】 [root@nfs01 tmp]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup rsync: failed to connect to 172.16.1.41: No route to host (113) rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6] 【異常問題解決】 關閉rsync服務端的防火牆服務(iptables) [root@backup mnt]# /etc/init.d/iptables stop iptables: Setting chains to policy ACCEPT: filter [ OK ] iptables: Flushing firewall rules: [ OK ] iptables: Unloading modules: [ OK ] [root@backup mnt]# /etc/init.d/iptables status iptables: Firewall is not running.
- rsync客戶端執行rsync命令錯誤 【客戶端的錯誤】 The remote path must start with a module name not a / 【錯誤演示過程】 [root@nfs01 tmp]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::/backup ERROR: The remote path must start with a module name not a / rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6] 【異常問題解決】 rsync命令語法理解錯誤,::/backup是錯誤的語法,應該為::backup(rsync模塊)
- rsync服務認證用戶失敗* 【客戶端的錯誤】 auth failed on module oldboy 【錯誤演示過程】 [root@nfs01 tmp]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup Password: @ERROR: auth failed on module backup rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6] 【異常問題解決】
- 密碼真的輸入錯誤,用戶名真的錯誤
- secrets file = /etc/rsync.password指定的密碼文件和實際密碼文件名稱不一致
- /etc/rsync.password文件權限不是600
- rsync_backup:123456密碼配置文件后面注意不要有空格
- rsync客戶端密碼文件中只輸入密碼信息即可,不要輸入虛擬認證用戶名稱
- rsync服務位置模塊錯誤 【客戶端的錯誤】 Unknown module 'backup'
【錯誤演示過程】
[root@nfs01 tmp]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup @ERROR: Unknown module 'backup' rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6] 【異常問題解決】- /etc/rsyncd.conf配置文件模塊名稱書寫錯誤
- rsync服務權限阻止問題 【客戶端的錯誤】 Permission denied 【錯誤演示過程】 [root@nfs01 tmp]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup Password: sending incremental file list hosts rsync: mkstemp ".hosts.5z3AOA" (in backup) failed: Permission denied (13) sent 196 bytes received 27 bytes 63.71 bytes/sec total size is 349 speedup is 1.57 rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]
【異常問題解決】- 備份目錄的屬主和屬組不正確,不是rsync
- 備份目錄的權限不正確,不是755
- rsync服務備份目錄異常 【客戶端的錯誤】 chdir failed
【錯誤演示過程】
[root@nfs01 tmp]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup Password: @ERROR: chdir failed rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6] 【異常問題解決】- 備份存儲目錄沒有建立
- 建立的備份存儲目錄和配置文件定義不一致 說明:如果沒有備份存儲目錄
- rsync服務無效用戶信息 【客戶端的錯誤】 invalid uid rsync 【錯誤演示過程】
[root@nfs01 tmp]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup Password: @ERROR: invalid uid rsync rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6] 【異常問題解決】
rsync服務對應rsync虛擬用戶不存在了 - 客戶端已經配置了密碼文件,但免秘鑰登錄方式,依舊需要輸入密碼 【客戶端的錯誤】 password file must not be other-accessible 【錯誤演示過程】 [root@nfs01 tmp]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password password file must not be other-accessible continuing without password file Password: sending incremental file list sent 26 bytes received 8 bytes 5.23 bytes/sec total size is 349 speedup is 10.26 【異常問題解決】
rsync客戶端的秘鑰文件也必須是600權限 - rsync客戶端連接慢問題 IP === 域名 反向DNS解析 【錯誤日志信息】 錯誤日志輸出 2017/03/08 20:14:43 [3422] params.c:Parameter() - Ignoring badly formed line in configuration file: ignore errors 2017/03/08 20:14:43 [3422] name lookup failed for 172.16.1.31: Name or service not known 2017/03/08 20:14:43 [3422] connect from UNKNOWN (172.16.1.31) 2017/03/08 20:14:43 [3422] rsync to backup/ from rsyncbackup@unknown (172.16.1.31) 2017/03/08 20:14:43 [3422] receiving file list 2017/03/08 20:14:43 [3422] sent 76 bytes received 83 bytes total size 349 正確日志輸出 2017/03/08 20:16:45 [3443] params.c:Parameter() - Ignoring badly formed line in configuration file: ignore errors 2017/03/08 20:16:45 [3443] connect from nfs02 (172.16.1.31) 2017/03/08 20:16:45 [3443] rsync to backup/ from rsyncbackup@nfs02 (172.16.1.31) 2017/03/08 20:16:45 [3443] receiving file list 2017/03/08 20:16:45 [3443] sent 76 bytes received 83 bytes total size 349 【異常問題解決】 查看日志進行分析,編寫rsync服務端hosts解析文件
10 rsync服務沒有正確啟動 【錯誤日志信息】 Connection refused (111) 【錯誤演示過程】 [root@oldboy-muban ~]# rsync -avz /etc/hosts rsyncbackup@172.16.1.41::backup rsync: failed to connect to 172.16.1.41: Connection refused (111) rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6] 【異常問題解決】 [root@oldboy-muban ~]# rsync --daemon [root@oldboy-muban ~]# ss -lntup |grep rsync tcp LISTEN 0 5 :::873 :::* users:(("rsync",1434,5)) tcp LISTEN 0 5 *:873 : users:(("rsync",1434,4)) [root@oldboy-muban ~]# rsync -avz /etc/hosts rsyncbackup@172.16.1.41::backup Password: sending incremental file list hosts
sent 196 bytes received 27 bytes 49.56 bytes/sec total size is 349 speedup is 1.57
附錄一option詳細說明
-v, --verbose 詳細模式輸出
-q, --quiet 精簡輸出模式
-c, --checksum 打開校驗開關,強制對文件傳輸進行校驗
-a, --archive 歸檔模式,表示以遞歸方式傳輸文件,並保持所有文件屬性,等於-rlptgoD
-r, --recursive 對子目錄以遞歸模式處理
-R, --relative 使用相對路徑信息
-b, --backup 創建備份,也就是對於目的已經存在有同樣的文件名時,將老的文件重新命名為~filename。可以使用--suffix選項來指定不同的備份文件前綴。
--backup-dir 將備份文件(如~filename)存放在在目錄下。
-suffix=SUFFIX 定義備份文件前綴
-u, --update 僅僅進行更新,也就是跳過所有已經存在於DST,並且文件時間晚於要備份的文件。(不覆蓋更新的文件)
-l, --links 保留軟鏈結
-L, --copy-links 想對待常規文件一樣處理軟鏈結
--copy-unsafe-links 僅僅拷貝指向SRC路徑目錄樹以外的鏈結
--safe-links 忽略指向SRC路徑目錄樹以外的鏈結
-H, --hard-links 保留硬鏈結
-p, --perms 保持文件權限
-o, --owner 保持文件屬主信息
-g, --group 保持文件屬組信息
-D, --devices 保持設備文件信息
-t, --times 保持文件時間信息
-S, --sparse 對稀疏文件進行特殊處理以節省DST的空間
-n, --dry-run現實哪些文件將被傳輸
-W, --whole-file 拷貝文件,不進行增量檢測
-x, --one-file-system 不要跨越文件系統邊界
-B, --block-size=SIZE 檢驗算法使用的塊尺寸,默認是700字節
-e, --rsh=COMMAND 指定使用rsh、ssh方式進行數據同步
--rsync-path=PATH 指定遠程服務器上的rsync命令所在路徑信息
-C, --cvs-exclude 使用和CVS一樣的方法自動忽略文件,用來排除那些不希望傳輸的文件
--existing 僅僅更新那些已經存在於DST的文件,而不備份那些新創建的文件
--delete 刪除那些DST中SRC沒有的文件
--delete-excluded 同樣刪除接收端那些被該選項指定排除的文件
--delete-after 傳輸結束以后再刪除
--ignore-errors 及時出現IO錯誤也進行刪除
--max-delete=NUM 最多刪除NUM個文件
--partial 保留那些因故沒有完全傳輸的文件,以是加快隨后的再次傳輸
--force 強制刪除目錄,即使不為空
--numeric-ids 不將數字的用戶和組ID匹配為用戶名和組名
--timeout=TIME IP超時時間,單位為秒
-I, --ignore-times 不跳過那些有同樣的時間和長度的文件
--size-only 當決定是否要備份文件時,僅僅察看文件大小而不考慮文件時間
--modify-window=NUM 決定文件是否時間相同時使用的時間戳窗口,默認為0
-T --temp-dir=DIR 在DIR中創建臨時文件
--compare-dest=DIR 同樣比較DIR中的文件來決定是否需要備份
-P 等同於 --partial
--progress 顯示備份過程
-z, --compress 對備份的文件在傳輸時進行壓縮處理
--exclude=PATTERN 指定排除不需要傳輸的文件模式
--include=PATTERN 指定不排除而需要傳輸的文件模式
--exclude-from=FILE 排除FILE中指定模式的文件
--include-from=FILE 不排除FILE指定模式匹配的文件
--version 打印版本信息
--address 綁定到特定的地址
--config=FILE 指定其他的配置文件,不使用默認的rsyncd.conf文件
--port=PORT 指定其他的rsync服務端口
--blocking-io 對遠程shell使用阻塞IO
-stats 給出某些文件的傳輸狀態
--progress 在傳輸時現實傳輸過程
--log-format=formAT 指定日志文件格式
--password-file=FILE 從FILE中得到密碼
--bwlimit=KBPS 限制I/O帶寬,KBytes per second
-h, --help 顯示幫助信息
附錄二rsyncd.conf文件的詳細配置文件
pid file = /var/run/rsyncd.pid #進程 pid 文件所在位置
port = 873 #指定監聽端口,默認是873,可以自己指定
address = 192.168.1.171 #服務器監聽的IP地址,可省略
uid = root #守護進程所屬的uid,默認是nobody,可能會碰到文件或目錄權限問題,此處偷懶用root
gid = root#守護進程的gid
#chroot,即改變程序執行時所參考的根目錄位置,在傳輸文件之前,服務器守護程序在將chroot 到文件系統中的目錄中
#這樣做的好處是可能保護系統被安裝漏洞侵襲的可能。缺點是需要超級用戶權限。另外對符號鏈接文件,將會排除在外
#也就是說,你在 rsync服務器上,如果有符號鏈接,你在備份服務器上運行客戶端的同步數據時,只會把符號鏈接名同步下來,並不會同步符號鏈接的內容
use chroot = yes
read only = no #只讀選擇,只讓客戶端從服務器上讀取文件
write only = yes #只寫選擇,只讓客戶端到服務器上寫入
#允許訪問的IP,可以指定單個IP,也可以指定整個網段,能提高安全性。格式是 ip 與 ip 之間、ip 和網段之間、網段和網段之間要用空格隔開;
hosts allow = 192.168.1.0/255.255.255.0 10.0.1.0/255.255.255.0
max connections = 5 #客戶端最多連接數
#當用戶登錄時會看到這個信息。比如顯示當前時間、公告等
motd file = /etc/rsyncd/rsyncd.motd
log file = /var/log/rsync.log #rsync 服務器的日志;
transfer logging = yes #記錄傳輸文件的日志
log format = %t %a %m %f %b #日志格式
syslog facility = local3 #日志級別
#通過該選項可以覆蓋客戶指定的IP超時時間。可以確保rsync服務器不會永遠等待一個崩潰的客戶端。超時單位為秒鍾,0表示沒有超時定義,這也是默認值。對於匿名rsync服務器來說,一個理想的數字是600。
timeout = 300
#模塊定義
#主要是定義服務器哪個目錄要被同步。
#每個模塊都要以[name]形式。這個名字就是在 rsync 客戶端看到的名字。
#但是服務器真正同步的數據是通過 path 指定的。可以依次創建多個模塊。
#每個模塊要指定認證用戶、密碼文件,但排除並不是必須的。
[ logs ] #模塊名,以下配置都屬於此模塊
path = /var/log #文件目錄所在位置
list = no #當查看服務器上提供了哪些目錄時是否列出來,no比較安全
ignore errors #忽略I/O錯誤
#指定由空格或逗號分隔的用戶名列表,只有這些用戶才允許連接該模塊。這里的用戶和系統用戶沒有任何關系,是 rsyncd.secrets 中的用戶名!
#如果"auth users"被設置,那么客戶端發出對該模塊的連接請求以后會被rsync請求challenged進行驗證身份。
#這里使用的 challenge/response 認證協議。
#用戶的名和密碼以明文方式存放在"secrets file"選項指定的文件中。默認情況下無需密碼就可以連接模塊(也就是匿名方式)。
auth users = zhangzk
secrets file = /etc/rsyncd/rsyncd.secrets #密碼文件
exclude = error_log httpd.pid #忽略的文件或目錄
comment this is my log #本模塊注釋,可選