1. 程式人生 > >Rsync服務及搭建備份服務器

Rsync服務及搭建備份服務器

copy his pairs transfer quick red 就是 命令操作 ftw

rsync復制軟件應用與實踐

[rrsync命令語法]https://download.samba.org/pub/rsync/rsync.html

1、什麽是rsync?

rsync,Rsync英語全稱為Remote synchronization,是開源、高速的、可實現本地以及遠程,全量以及增量的數據復制(拷貝)工具。

2、全量復制和增量復制的區別?

全量復制:
[[email protected] ~]# cp -a /etc/ /opt/
[[email protected] ~]# \cp -a /etc/ /opt
[[email protected] ~]# touch /etc/oldboy.txt

[[email protected] ~]# \cp -a /etc/ /opt/
增量復制:
只復制oldboy.txt(只復制另添加的文件)

3、rsync的作用?(適用於什麽地方)

工作中需要定時/實時數據備份。本地不同的機器目錄、不同的機房之間的數據備份。這些都可以用rsync完成。

4、rsync的功能特性

增量復制的原理

使用quick check算法,只對增量的部分復制,根據大小屬性的變化進行復制。
2.x比對差異後復制,3.x一邊比對一邊復制。

6、rsync三種工作模式介紹

①本地(local)

rsync就是一個命令
命令操作:
rsync命令
a.把數據從一個地方復制到另一個的的地方(僅在一臺機器增量),相當於cp。

b.通過加參數實現刪除的功能,相當於rm命令。
c.查看屬性信息功能,相當於1s。

②遠程Shell模式

借助類似ssh隧道傳輸數據,適合不同機器之間的復制。
異地考本,相當於srcp
拉門,推門
pull,拉:從遠端拉取到本地。
rsync [OPTION ...] [USER @] HOST : SRC ... [DEST]

[[email protected] ~]# rsync -avz [email protected]:/root/1.txt /opt/
[email protected]'s password: 
receiving incremental file list
1.txt

sent 43 bytes  received 106 bytes  33.11 bytes/sec
total size is 24  speedup is 0.16
[[email protected] ~]# cd /opt/
[[email protected] /opt]# ls
1.txt

rsync命令 參數選項 [認證用戶]@[主機地址]:[源地址]..[目的地址]
push,推:從本地推到遠端。
rsync [OPTION ...] SRC ... [USER @] HOST : DEST

[[email protected] ~]# rsync -avz /etc/hosts [email protected]:/opt/
The authenticity of host '172.16.1.41 (172.16.1.41)' can't be established.
ECDSAkeyfingerprintisSHA256:pSmZZNpT7WLg8LxBk9gFez5RNq3gaxYgpo/Kd9sv1Do.ECDSAkeyfingerprintisMD5:ce:42:93:22:32:b3:9c:4a:71:81:b9:f6:50:e5:bb:6b.Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.1.41' (ECDSA) to the list of known hosts.
[email protected]'s password: 
sending incremental file list
hosts

sent 218 bytes  received 35 bytes  38.92 bytes/sec
total size is 332  speedup is 1.31
[[email protected] /opt]# ls   <==檢查
hosts

推拉:
a.參照物,執行命令的機器
[email protected] 使用的用戶和主機,就用主機和用戶密碼。

重點:適合rsync

null和null/區別,null是目錄和目錄下的內容,null/只是目錄下的內容,不含本身。

③rsync守護進程模式

首先要搭建rsync服務器(要有守護進程),然後才能在客戶端實現推拉數據

7、rsync命令的參數技術分享圖片

--password-file=file 從flie中得到密碼。
企業常用參數組合:-avz或者-vzrtopg

8、rsync守護進程模式部署

(1)註:以下操作均為backup機器

①查看rsync版本

[[email protected] ~]# rsync --version <==查看rsync版本
rsync  version 3.1.2  protocol version 31
Copyright (C) 1996-2015 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, prealloc

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.

②配置配置文件

[[email protected] ~]# cp /etc/rsyncd.conf{,.ori} 
[[email protected] ~]# > /etc/rsyncd.conf
[[email protected] ~]# cat >/etc/rsyncd.conf<<EOF
> #rsync_config_______________start
> #created by oldboy
> #site: http://www.oldboyedu.com
> uid =rsync                       #--->管理備份目錄的用戶
> gid =rsync                       #--->管理備份目錄的用戶組
> use chroot = no                  #--->安全功能,數據是否鎖定到備份目錄
> max connections = 200            #--->並發連接,同時多少客戶端訪問
> timeout =600                     #--->超時時間。
> 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     #--->存放密碼的文件:格式:用戶名:密碼權限必須600
> [backup]                               #--->[模塊名]遠程訪問使用模塊名訪問
> comment = welcome to oldboyedu backup! #--->說明註釋
> path = /backup/                        #--->服務端用戶備份的目錄,用戶和組,rsync.rsync
> EOF
[[email protected] ~]# useradd rsync
[[email protected] ~]# id rsync
uid=1004(rsync) gid=1004(rsync) 組=1004(rsync)
[[email protected] ~]# mkdir -p /backup
[[email protected] ~]# chown rsync.rsync /backup/

man rsync 查命令的參數
man rsyncd.conf 查配置參數
[rrsync命令語法]https://download.samba.org/pub/rsync/rsync.html
③啟動和檢查

rsync --daemon(CentOS6 以前)
systemctl start rsyncd
systemctl enable rsyncd
systemctl status rsyncd

④配置密碼文件

[[email protected] ~]# echo "rsync_backup:oldboy" > /etc/rsync.password
[[email protected] ~]# chmod 600 /etc/rsync.password
[[email protected] ~]# cat /etc/rsync.password 
rsync_backup:oldboy
[[email protected] ~]# ll /etc/rsync.password 
-rw------- 1 root root 20 4月  15 11:52 /etc/rsync.password

rsync服務端配置完成

(2)註:以下操作都是在客戶端服務器

兩個方法二選一
方法1:認證密碼文件

[[email protected] ~]# echo "oldboy" > /etc/rsync.password
[[email protected] ~]# chmod 600 /etc/rsync.password 
[[email protected] ~]# ll /etc/rsync.password 
-rw------- 1 root root 7 4月  15 11:55 /etc/rsync.password

方法2:

[[email protected] ~]# echo 'export RSYNC_PASSWORD=oldboy' >>/etc/bashrc
[[email protected] ~]# tail -1 /etc/bashrc 
export RSYNC_PASSWORD=oldboy
[[email protected] ~]# . /etc/bashrc
[[email protected] ~]# echo $RSYNC_PASSWORD
oldboy

rsync客戶端nfs01配置完成

(3)守護進程模式,客戶端rsync的命令和語法:

配置服務器守護進程,實現數據傳輸:
1.服務器端守護進程。2.客戶端執行命令。
拉門,推門
pull,拉:從遠端拉取到本地。
語法1:
rsync [OPTION ...] [USER @] HOST :: SRC ... [DEST]
rsync命令 參數選項 [虛擬用戶]@[主機地址]::[模塊名]..[本地路徑]
語法2:
rsync [OPTION ...] rsync:// [USER @] HOST :: SRC ... [DEST]
rsync命令 參數選項 rsync:// [虛擬用戶]@[主機地址]/[模塊名]..[本地路徑
push,推:從本地推到遠端。
語法1:
rsync [OPTION ...] [DEST] [USER @] HOST :: SRC ...
rsync命令 參數選項 [本機路徑] [虛擬用戶]@[主機地址]::[模塊名]...
語法2:
rsync [OPTION ...] [DEST] rsync:// [USER @] HOST :: SRC ...
rsync命令 參數選項 [本地路徑] rsync:// [虛擬用戶]@[主機地址]/[模塊名]..

(4)測試結果

錯誤1:

[[email protected] ~]# rsync -avz /etc/hosts [email protected]::backup --password-file=/etc/rsync.password
@ERROR: invalid uid rsync
rsync error: error starting client-server protocol (code 5) at main.c(1648) [sender=3.1.2]

解答:

[[email protected] ~]# useradd rsync
[[email protected] ~]# id rsync
uid=1001(rsync) gid=1001(rsync) 組=1001(rsync)
[[email protected] ~]# mkdir -p /backup
[[email protected] ~]# chown -R rsync.rsync /backup/
[[email protected] ~]# ls -ld /backup/
drwxr-xr-x 2 rsync rsync 6 4月  15 12:12 /backup/

錯誤2:

[[email protected] ~]# rsync -avz /etc/hosts [email protected]::backup --password-file=/etc/rsync.password
sending incremental file list
hosts
rsync: chgrp ".hosts.YDuTjO" (in backup) failed: Operation not permitted (1)

sent 223 bytes  received 124 bytes  694.00 bytes/sec
total size is 332  speedup is 0.96
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]

解答:增加如下參數到/etc/rsyncd.conf

fake  super  = yes #不用root用戶也可以存儲文件的完整屬性。
This allows the full attributes of a file to  be  stored  without having to have the daemon actually running as root.
[[email protected] ~]# grep fake /etc/rsyncd.conf
fake super = yes

改完配置,要重啟服務:

[[email protected] ~]# systemctl restart rsyncd

在測試:成功

[[email protected] ~]# rsync -avz /etc/hosts [email protected]::backup --password-file=/etc/rsync.password
sending incremental file list
hosts

sent 89 bytes  received 49 bytes  276.00 bytes/sec
total size is 332  speedup is 2.41

rsync -avz /etc [email protected]::backup --password-file=/etc/rsync.password

服務端檢查:

[[email protected] ~]# ls /backup/
hosts
[[email protected] ~]# ls /backup/
etc  hosts

如果客戶端按照環境變量的方式配置,則可以忽略--password-file=/etc/rsync.password參數。

[[email protected] ~]# rsync -avz /etc [email protected]::backup
sending incremental file list

sent 52,071 bytes  received 644 bytes  105,430.00 bytes/sec
total size is 31,244,350  speedup is 592.70

測試增量

[[email protected] ~]# touch /etc/oldboy.txt
[[email protected] ~]# rsync -avz /etc [email protected]::backup
sending incremental file list
etc/oldboy.txt

sent 52,098 bytes  received 655 bytes  105,506.00 bytes/sec
total size is 31,244,350  speedup is 592.28

Rsync服務及搭建備份服務器