nfs

Rsync服務器的安裝與配置詳解                 2016-12-26 20:06:51

標籤:服務器 主機

Rsync簡介

1.1什麼是Rsync

Rsync是一款快速的,開源的,多功能的,可以實現全量和增量的遠程和本地的數據同步和數據備份的工具。

全量的概念是:全部備份。

增量的概念是:差異化備份。對上一次基礎上,對更新的部分作備份。

1.1.2    Rsync簡介

   Rsync具有可以使本地和遠程的兩臺主機之間的數據快速同步鏡像遠程備份的功能,這個功能類似ssh帶scp的命令,但是有優於scp的功能,scp每次都是全量拷貝,而rsync是增量拷貝。

   Rsync還可以在本地主機的不同文件或者目錄之間全量和增量的複製,類似於cp命令,cp命令是全量拷貝,而rsync是增量拷貝。

   Rsync還可以實現刪除文件和目錄的功能,相當於rm命令

   一個rsync相當於scp,cp,rm命令,並且還憂於他們每個命令,因爲rsync具有增量備份的功能。


1.1.3  rsync的特性:

  1)支持拷貝特殊文件如鏈接文件,設備等

  2)可以有排除指定文件或者目錄同步的功能,相當於打包命令tar的排除功能。

  3)可以做到保持原文件或者目錄的權限,時間,軟硬鏈接,屬組,主等所有屬性均不改變

  4)可以實現增量備份,既只同步發生變化的數據

  5)可以勇士rcp,rsh,ssh等方式來配合傳輸文件

  6)可以通過socket傳輸文件和數據

  7)支持匿名的認證模式傳輸

1.1.4  rsync三種工作方式

1)本地模式,相當於cp和rm命令

1
2
3
4
5
6
7
8
9
[root@rsync tmp]# rsync /etc/passwd /tmp/    ##相當於cp的命令
[root@rsync tmp]# ls
passwd
  
[root@rsync mnt]# rsync -avz --delete passwd  /mnt/  ##--delete相當於刪除的功能
[root@rsync mnt]# ls
passwd
 
--delete的作用是刪除的功能,本地有什麼,遠端就有什麼。比如本地有passwd的內容,不管/mnt目錄下面有什麼,都只有passwd的內容,所有謹慎用--delete

2)通道模式,一般配合ssh key免祕鑰使用,結合定時任務

1
2
3
4
5
6
[root@rsync mnt]# rsync -avz -e  'ssh -p 22' /etc/passwd [email protected]:/tmp/  
[email protected]'s password: 
sending incremental file list
passwd
[root@nfs tmp]# ls ##在遠端查看
passwd

3)daemon模式

 

1.1.5 rsync的參數說明

-v :詳細輸出

-z :傳輸時進行壓縮以提高傳輸效率。

-a :歸檔模式,表示以遞歸的方式傳輸文件,並保持文件的屬性

--exclude :排除不需要同步傳輸的文件或者目錄

--delete: 讓目標目錄和源目錄的數據一致

--bwlimit: 限制帶寬,默認單位是:kb(案例:某DBA做數據同步,導致用戶無法訪問網站)


Rsync服務器的安裝

2.1安裝準備

2.1.1查看rsync的版本號

1
2
3
4
5
6
7
8
9
10
11
[root@rsync ~]# 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.

2.1.2 查看服務器的內核,版本信息

1
2
3
4
5
6
[root@rsync ~]# cat /etc/redhat-release 
CentOS release 6.7 (Final)
[root@rsync ~]# uname -r
2.6.32-573.el6.x86_64
[root@rsync ~]# uname -m
x86_64


2.2主要講一下通過daemon實現數據同步案例

/etc/rsyncd.conf是rsync的默認配置文件,該配置文件不存在,需要編輯內容


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@oldboy ~]# cat /etc/rsyncd.conf 
#rsync_config_____________________________start
#created by oldboy 15:01 2007-6-5
##rsyncd.conf start##
uid = rsync   ##進程對應的用戶,是虛擬用戶。遠端的命令使用rsync訪問共享目錄
gid = rsync   ##進程對應的用戶組。
use chroot = no    ##安全相關
max connections = 200      ##最大連接數
timeout = 300    ##超時時間
pid file /var/run/rsyncd.pid      ##進程對應的進程號文件
lock file /var/run/rsyncd.lock     ##鎖文件
log file /var/log/rsyncd.log        ##日誌文件
[backup]     ###模塊名稱
path = /backup      ###服務器提供訪問的目錄
ignore errors       ##忽略錯誤
read only = false    ##可寫
list = false      ##不能列表
hosts allow = 172.16.1.0/24  ##允許的ip地址
##hosts deny = 0.0.0.0/32
auth users = rsync_backup     ##虛擬用戶
secrets file /etc/rsync.password    ###虛擬密碼
#rsync_config________________________end

其中rsync用戶默認是不存在的,需要創建用戶

1
2
3
4
5
6
7
8
[root@rsync ~]# useradd rsync -s /sbin/nologin -M
[root@rsync ~]# cat  /etc/passwd|grep rsync
rsync:x:501:501::/home/rsync:/sbin/nologin
[root@rsync ~]# id rsync
uid=501(rsync) gid=501(rsync) 組=501(rsync)
 
爲什麼用虛擬用戶?
應答:文件和進程都要滿足屬主的要求,文件和進程的存在一定是需要用戶的,也是爲了安全問題。

創建/backup目錄,並且屬主和屬組都屬於rsync

1
2
3
4
[root@rsync ~]# mkdir /backup/ -p
[root@rsync ~]# chown -R rsync.rsync /backup/
[root@rsync ~]# ls -ld /backup/
drwxr-xr-x 2 rsync rsync 4096 12月  9 2016 /backup/


創建配置文件/etc/rsync.password,默認不存在這個配置文件

1
2
3
4
5
[root@rsync ~]# cat /etc/rsync.password 
rsync_backup:oldboy
[root@rsync ~]#chmod 600 /etc/rsync.password
[root@rsync ~]# ls -l /etc/rsync.password 
-rw-------. 1 root root 20 11月 29 01:14 /etc/rsync.password


1
2
3
4
啓動服務:
[root@rsync ~]# rsync --daemon 
[root@rsync ~]#ps -ef|grep rsync|grep -v grep ##查看進程有沒有啓動
root       3046      1  0 15:19 ?        00:00:00


加入開機自啓動

1
2
[root@rsync ~]# tail -1 /etc/rc.local 
/usr/bin/rsync --daemon


Rsync客戶端的安裝


編輯配置文件/etc/rsync.passwd,該配置文件默認不存在

1
2
3
4
vim  /etc/rsync.passwd
[root@oldboy backup]# cat /etc/rsync.password 
oldboy
chmod 600 /etc/rsync.passwd


創建backup目錄

1
2
3
mkdir -p /backup
cd /backup
touch stu{01,100}


客戶端推送:

1
2
3
4
方法1:
[root@oldboy backup]# rsync -avz /backup/ [email protected]::backup/ --password-file=/etc/rsync.password 
方法2:
[root@oldboy backup]# rsync -avz /backup/ rsync://[email protected]/backup/ --password-file=/etc/rsync.password



 從客戶端把服務端的東西拉回來的方案

服務端:

1
2
3
[root@oldboy backup]# touch 1 234
[root@oldboy backup]# ls
1  234


 

客戶端:

1
2
3
4
5
6
7
8
9
10
[root@oldboy ming]# rsync -avz   [email protected]::backup/ /ming/  --password-file=/etc/rsync.password 
receiving incremental file list
./
1
234
  
sent 105 bytes  received 204 bytes  618.00 bytes/sec
total size is 0  speedup is 0.00
[root@oldboy ming]# ls
1  234



Rsync多模塊實戰

1.1.1 多模塊實戰

實例1

環境:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@oldboy ~]# cat /etc/rsyncd.conf
#rsync_config_____________________________start
#created by oldboy 15:01 2007-6-5
##rsyncd.conf start##
uid = rsync  
gid = rsync   
use chroot = no    
max connections = 200      
timeout = 300    
pid file /var/run/rsyncd.pid     
lock file /var/run/rsyncd.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]     
path = /backup      
[chen]
path = /chen
#rsync_config________________________end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
服務器端:
[root@oldboy ~]# mkdir /chen
[root@oldboy ~]# ls -ld /chen/
drwxr-xr-x 2 rsync rsync 4096 12月  2 18:58 /chen/
客戶端
[root@oldboy ~]# ls -ld /ming
drwxr-xr-x 2 root root 4096 12月  2 18:26 /ming
[root@oldboy ~]# rsync -avz /ming/ [email protected]::chen/ --password-file=/etc/rsync.password 
sending incremental file list
./
ming1
ming10
ming2
ming3
ming4
ming5
ming6
ming7
ming8
ming9
  
sent 463 bytes  received 201 bytes  1328.00 bytes/sec
total size is 0  speedup is 0.00


服務端查看效果:

1
2
[root@oldboy chen]# ls
ming1  ming10  ming2  ming3  ming4  ming5  ming6  ming7  ming8  ming9


實例2

環境:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@oldboy chen]# cat /etc/rsyncd.conf
#rsync_config_____________________________start
#created by oldboy 15:01 2007-6-5
##rsyncd.conf start##
uid = rsync  
gid = rsync   
use chroot = no    
max connections = 200      
timeout = 300    
pid file /var/run/rsyncd.pid     
lock file /var/run/rsyncd.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]     
path = /backup      
[chen]
path = /chen
[luo]
path = /luo
ignore errors
read only = false
list = false
1
2
3
4
5
6
7
8
9
10
11
12
13
14
hosts allow = 172.16.1.0/24
hosts deny = 0.0.0.0/32
auth users = tang
secrets file /etc/tang
#rsync_config________________________end
[root@oldboy chen]# mkdir /luo
[root@oldboy chen]# chown rsync.rsync /luo
[root@oldboy chen]# ls -ld /luo/
drwxr-xr-x 2 rsync rsync 4096 12月  2 19:18 /luo/
[root@oldboy chen]# cat /etc/tang 
tang:tangguo
[root@oldboy luo]# ls /etc/tang  -ld
-rw------- 1 root root 13 12月  2 19:34 /etc/tang
權限一定要是600


 

客戶端配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@oldboy ming]# cat /etc/tang 
tangguo
[root@oldboy ming]# ls /etc/tang -ld
-rw------- 1 root root 8 12月  2 19:35 /etc/tang
客戶端權限也一定要是600
[root@oldboy ming]# rsync -avz /ming/ [email protected]::luo/  --password-file=/etc/tang 
sending incremental file list
./
ming1
ming10
ming2
ming3
ming4
ming5
ming6
ming7
ming8
ming9
  
sent 463 bytes  received 201 bytes  1328.00 bytes/sec
total size is 0  speedup is 0.00


Rsync案例排錯

5.1 案例1


1
2
3
4
5
6
7
8
9
10
11
12
[root@oldboy ming]# rsync -avz /ming/ [email protected]::luo/  --password-file=/etc/tang 
@ERROR: auth failed on module luo
rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
[root@oldboy luo]# tail -3 /var/log/rsyncd.log 
2016/12/02 19:46:18 [3601] secrets file must not be other-accessible (see strict modes option)
2016/12/02 19:46:18 [3601] continuing without secrets file
2016/12/02 19:46:18 [3601] auth failed on module luo from unknown (172.16.1.31): missing secret for user "tang"
 
報錯的原因是服務器端的/etc/tang的權限問題沒有設置爲600,我們查看一下。
[root@oldboy luo]# ls -ld /etc/tang 
-rwxr-xr-x 1 root root 13 12月  2 19:34 /etc/tang
權限改爲600就可以了


5.2 案例2

1
2
3
4
5
6
7
8
9
10
[root@oldboy ~]# rsync -avz /ming/ [email protected]::luo/  --password-file=/etc/tang 
@ERROR: auth failed on module luo
rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
查看日誌
[root@oldboy luo]# tail -3 /var/log/rsyncd.log 
2016/12/02 19:52:12 [3614] name lookup failed for 172.16.1.31: Name or service not known
2016/12/02 19:52:12 [3614] connect from UNKNOWN (172.16.1.31)
2016/12/02 19:52:12 [3614] auth failed on module luo from unknown (172.16.1.31): password mismatch
password mismatch,密碼錯誤,客戶端和服務器端的密碼不一致導致的問題。
【注意】有的客戶端和服務器端密碼看起來一樣,實際裏面有空格,也能報錯,注意一下


5.3 案例3

1
2
3
4
[root@oldboy ~]# rsync -avz /backup/ rsync://[email protected]/backup/ --password-file=/etc/rsync.password 
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
原因:服務端的backup目錄不存在


5.4  案例4

1
2
3
4
5
6
7
8
9
10
[root@oldboy ~]# rsync -avz /backup/ rsync://[email protected]/backup/ --password-file=/etc/rsync.password 
sending incremental file list
./
rsync: failed to set times on "." (in backup): Operation not permitted (1)
1
  
sent 4325 bytes  received 1911 bytes  12472.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]
原因:服務端backup的屬組和屬主問題


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