1. 程式人生 > >rsync搭建部署和配置檔案詳解

rsync搭建部署和配置檔案詳解

Rsync是一種快速並且非常通用的檔案複製工具,它以delta-transfer演算法聞名,通過僅傳送原始檔和目的地中現有檔案之間的差異來減少在網路上傳送的資料量。RSyc廣泛用於備份和映象,並作為日常使用的複製命令。

環境:

Centos 7 

[[email protected] ~]# uname -a
Linux localhost.localdomain 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
ip: 192.168.253.153   rsync服務端

ip :192.168.253.137   rsync客戶端

0.先把客戶端和服務端的防火牆和selinux關了

systemctl  stop firewalld
setenforce 0

1.服務端安裝採用編譯安裝

cd /usr/local/src
mkdir rsync
cd rsync
yum install gcc gcc-devel  -y
wget https://download.samba.org/pub/rsync/src/rsync-3.1.3.tar.gz
tar -zxf rsync-3.1.3.tar.gz
cd rsync-3.1.3
./configure --prefix=/usr/local/rsync 
make && make install
PATH="/usr/local/rsync/bin/:$PATH"

2.服務端編寫配置檔案/etc/rsyncd.conf(文末會解釋每一行意義)

[[email protected] rsync-3.1.3]# cat /etc/rsyncd.conf 
#create 2018.5.29 ip 192.168.253.153 by Hal
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 = 192.168.253.0/24
hosts deny = 0.0.0.0/32 
auth users = rsync_backup 
secrets file = /etc/rsync.password 
[backup] 
comment = "this is a comment" 
path = /backup

3.服務端新增相關檔案和目錄

[[email protected] rsync]# mkdir /backup
[[email protected] backup]# chown -R rsync:rsync /backup
[[email protected] rsync]# useradd rsync
[[email protected] rsync]# cat /etc/rsync.password 
rsync_backup:123456
[[email protected] rsync]# chmod 600 /etc/rsync.password

4.服務端啟動rsync

[[email protected] rsync]# rsync --daemon

5.檢視一下埠有沒有開啟


成功

6.在客戶端yum安裝rsync

yum install rsync -y

7.客戶端建立密碼檔案並修改許可權

[[email protected] ~]# cat /etc/rsync_passwd 
123456
[[email protected] ~]# chmod 600 /etc/rsync_passwd
8.客戶端測試能否正常使用rsync服務
[[email protected] ~]# echo "hello world" > hello.txt
[[email protected] ~]# rsync -avz -P hello.txt  [email protected]::backup --password-file=/etc/rsync_passwd 
sending incremental file list
hello.txt
             12 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/1)

sent 108 bytes  received 43 bytes  100.67 bytes/sec
total size is 12  speedup is 0.08

9.檢視一下服務端的/backup目錄是否有hello.txt檔案

[[email protected] backup]# cat /backup/hello.txt 
hello world

rsync配置檔案詳解

#create 2018.5.29 ip 192.168.253.153 by Hal  
uid = rsync     #rsync使用的使用者,預設nobody
gid = rsync     #rsync使用的gid 預設nobody
use chroot = no    #是否限定在該目錄下,預設為true,當有軟連線時,需要改為fasle,如果為
                   #true就限定為模組預設目錄,通常都在內網使用rsync所以不配也可以
max connections = 200 #設定最大連線數timeout = 300 #超時時間 建議300-600
pid file = /var/run/rsyncd.pid    #pid檔案位置
lock file = /var/run/rsync.lock  #指定lock檔案用來支援“max connections ”引數使總連線不會超過限制
log file = /var/log/rsyncd.log #日誌檔案路徑
ignore errors #忽略io錯誤
read only = false #指定客戶端是否可以上傳檔案,預設
truelist = false #是否允許客戶端檢視可用模組 
hosts allow = 192.168.253.0/24 #允許連線的ip段或個別ip,預設任何人都可以連線
hosts deny = 0.0.0.0/32 #不允許連線的IP段或個別ip
auth users = rsync_backup #指定以空格或逗號分隔的使用者可以使用哪些模組,使用者不需要再本
                          #系統存在,預設所有使用者都可以無密碼登入
secrets file = /etc/rsync.password #指定使用者名稱和密碼檔案 格式: 使用者名稱:密碼 密碼不超過8位
                                   #這個是密碼檔案  全線最好是600
[backup] comment = "this is a comment" #此引數指定在客戶端獲取可用模組列表時顯示在模組名稱旁邊的描述字
                                       ##符串,預設沒有這個引數
path = /backup #模組在服務端的絕對路徑




rsync常用命令

命令格式 Local: rsync [OPTION...] SRC... [DEST] =====>本地的推送格式 Access via remote shell: Pull: rsync [OPTION...] [[email protected]]HOST:SRC... [DEST] Push: rsync [OPTION...] SRC... [[email protected]]HOST:DEST Access via rsync daemon: Pull: rsync [OPTION...] [[email protected]]HOST::SRC... [DEST] rsync [OPTION...] rsync://[[email protected]]HOST[:PORT]/SRC... [DEST] Push: rsync [OPTION...] SRC... [[email protected]]HOST::DEST rsync [OPTION...] SRC... rsync://[[email protected]]HOST[:PORT]/DEST常用的選項-v 詳細模式輸出 傳輸時候顯示進度等資訊-z 傳輸時進行壓縮提高傳輸速率 --compuress-level=num 可以按級別壓縮-r 對子目錄以遞迴模式,即目錄下的所有目錄都同樣傳輸-t 保持檔案時間資訊-o 保持檔案屬性資訊 -g 保持穩健屬組資訊-a 歸檔模式 相當於 rtopg-P 顯示同步過程及傳輸時進度資訊-e 使用通道協議 指定替代rsh 的shell程式 如ssh--exclude=PATTERN 指定排除不需要傳輸的檔案 支援*來代替任意字元-D 保持裝置檔案資訊-l 保留軟連線常用的選項為 avz 常見用法
將當前目錄的hello.txt推送到服務端的backup模組
rsync -avz -P hello.txt  [email protected]::backup --password-file=/etc/rsync_passwd 
或者
rsync -avz -P hello.txt  rsync://[email protected]:/backup --password-file=/etc/rsync_passwd

將遠端的backup目錄拉取到當前目錄
rsync -avz -P   rsync://[email protected]:/backup ./  --password-file=/etc/rsync_passwd 
或者
rsync -avz -P   [email protected]::backup ./  --password-file=/etc/rsync_passwd
也可以利用ssh

利用ssh傳輸
[[email protected] ~]# rsync -avz -P -e 'ssh -p 22' ./hello.txt  [email protected]:/backup/ 
[email protected]'s password: [email protected]'s password: 
sending incremental file list


sent 50 bytes  received 12 bytes  17.71 bytes/sec
total size is 0  speedup is 0.00



相關推薦

no