天天看點

搭建rsync服務

文章目錄

      • 1.Rsync基本概述
      • 2.Rsync應用場景
      • 3.Rsync傳輸模式
    • 一、搭建rsync服務
      • 1.源伺服器和目标伺服器
    • 二、安裝rsync
    • 三、目标伺服器配置
      • 1.建立使用者認證檔案
      • 2.修改rsyncd.conf配置檔案
      • 3.設定檔案權限
      • 4.啟動rsync服務并設定為開機自啟
    • 四、源伺服器配置
      • 1.安裝rsync服務端軟體
      • 2.建立認證密碼檔案,并設定權限為600
      • 3.建立測試目錄,運作指令
      • 4.安裝inotify-tools
      • 5.配置同步腳本
      • 6.啟動腳本
    • 五、驗證結果
      • 1.源伺服器
      • 2.目标伺服器
      • 3.源伺服器
      • 4.目标伺服器

1.功能說明

1.Rsync基本概述

rsync是一款開源的備份工具,可以在不同主機之間進行同步,

可實作全量備份與增量備份,是以非常适合用于架構集中式備份或異地備份等應用。

rsync監聽端口:873

rsync運作模式:C/S 用戶端/服務端 B/S 浏覽器/服務端

2.Rsync應用場景

關于資料同步的兩種方式

01.rsync應用場景-上傳(推)

會導緻資料同步緩慢(适合少量資料備份)

搭建rsync服務

02.rsync應用場景-下載下傳(拉)

會導緻備份伺服器開銷大

搭建rsync服務

03.rsync應用場景-多server備份

搭建rsync服務

04.異地備份實作思路

搭建rsync服務

3.Rsync傳輸模式

Rsync大緻使用三種主要的資料傳輸方式

01.本地方式cp PC U盤

Local:  rsync [OPTION...] SRC... [DEST]
    [[email protected] ~]# rsync /etc/passwd /tmp/
    [[email protected] ~]# ls /tmp/passwd 
    /tmp/passwd
           

02.遠端方式scp

Access via remote shell: 遠端傳輸
     Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]  下載下傳(拉)
     Push: rsync [OPTION...] SRC... [USER@]HOST:DEST    上傳(推)
     
下載下傳pull``
[[email protected] ~]# pwd
/root
[[email protected] ~]# echo "This Nfs" > file
[[email protected] ~]# rsync -avz [email protected]:/root/file /opt/
[[email protected] ~]# cat /opt/file
This Nfs
上傳push(将backup的file2檔案上傳至NFS伺服器的/mnt目錄)
[[email protected] ~]# pwd
/root
[[email protected] ~]# echo "This Rsync" > file2
[[email protected] ~]# rsync -avz /root/file2 [email protected]:/mnt
[[email protected] ~]# cat /mnt/file2 
This Rsync
推送目錄(推送/root/目錄下面的所有檔案和目錄,不會推送/root/目錄本身)
[[email protected] ~]# rsync -avz /root/ [email protected]:/tmp
推送目錄,推送目錄本身以及目錄下面的所有檔案
[[email protected] ~]# rsync -avz /root [email protected]:/tmp
遠端方式存在的缺陷:
        1.需要使用系統使用者(不安全)
        2.使用普通使用者(權限存在問題)
        3.需要走ssh協定
           

03.守護程序(服務,持續背景運作)

Access via rsync daemon:    守護程序方式傳輸
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST] 下載下傳
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
           

首先環境說明

伺服器說明 ip位址 應用 系統
源伺服器 192.168.100.100 rsync inotify tools腳本 red hat7
目标伺服器 192.168.100.129 rsync centos7

1.部署rsync+inotify同步/dxk目錄至目标伺服器/tmp/dxk下

部署rsync服務同步

部署rsync+inotify同步/qinyong目錄至目标伺服器/tmp/qinyong下

源伺服器

一、搭建rsync服務

1.源伺服器和目标伺服器

關閉源伺服器和目标伺服器的防火牆和SELINUX

[[email protected] ~]# systemctl stop firewalld(源伺服器)
[[email protected] ~]# getenforce 
Disabled
[[email protected] ~]# systemctl stop firewalld(目标伺服器)
[[email protected] ~]# getenforce
Disabled
           

二、安裝rsync

[[email protected] ~]# yum install rsync -y
           

三、目标伺服器配置

1.建立使用者認證檔案

[[email protected] run]# echo 'qinyong:123456'>/etc/rsync.pass
[[email protected] run]# cat /etc/rsync.pass 
qinyong:123456
           

2.修改rsyncd.conf配置檔案

[[email protected] ~]# cat >> /etc/rsyncd.conf <<EOF
> log file = /var/log/rsyncd.log
> pidfile = /var/run/rsyncd.pid
> lock file = /var/run/rsync.lock
> secrets file = /etc/rsync.pass
> [qinyong] 
> path = /nihao/
> comment = sync etc from client
> uid = root
> gid = root
> port = 873
> ignore errors
> use chroot = no
> read only = no
> list = no
> max connections = 200
> timeout = 600
> auth users = qinyong
> EOF
           

3.設定檔案權限

[[email protected] ~]# chmod 600 /etc/rsync*
[[email protected] ~]# ll /etc/rsync*
-rw------- 1 root root 793 9月  19 10:03 /etc/rsyncd.conf
-rw------- 1 root root  16 9月  19 10:06 /etc/rsync.pass
           

4.啟動rsync服務并設定為開機自啟

[[email protected] ~]# systemctl start rsyncd
[[email protected] ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
           

四、源伺服器配置

1.安裝rsync服務端軟體

[[email protected] ~]# yum install epel-release -y
           

2.建立認證密碼檔案,并設定權限為600

[[email protected] ~]# echo '123456' > /etc/rsync.pass
[[email protected] ~]# chmod 600 /etc/rsync.pass 
[[email protected] ~]# ll /etc/rsync.pass
-rw------- 1 root root 8 9月  19 10:32 /etc/rsync.pass
           

3.建立測試目錄,運作指令

[[email protected] ~]# mkdir -pv /root/nihao
mkdir: 已建立目錄 "/root/nihao"
[[email protected] ~]# rsync -avH --port 873 --progress --delete /root/nihao/ [email protected]::qinyong --password-file=/etc/rsync.pass
sending incremental file list
./
.inotify.sh.swp
          4,096 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=4/6)
abc
             20 100%   26.53kB/s    0:00:00 (xfr#2, to-chk=3/6)
nohup.out
            151 100%  230.46kB/s    0:00:00 (xfr#3, to-chk=2/6)
123/
aaaa/

sent 4,586 bytes  received 92 bytes  9,356.00 bytes/sec
total size is 4,267  speedup is 0.91
           

4.安裝inotify-tools

[[email protected] ~]# yum install inotify-tools
           

5.配置同步腳本

[[email protected] ~]# mkdir /scripts
[[email protected] ~]# cd /scripts/
[[email protected] scripts]# touch inotify.sh
[[email protected] scripts]# chmod 755 inotify.sh 
[[email protected] scripts]# vim inotify.sh 

host=192.168.100.129
src=/root/nihao
des=qinyong
password=/etc/rsync.pass
user=root
inotifywait=/usr/bin/inotifywait
$inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files ;do
 rsync -avzP --delete --timeout=100 --password-file=${password} $src $u
ser@$host::$des
 echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
           

6.啟動腳本

[[email protected] scripts]# nohup bash /scripts/inotify.sh &
[1] 59758
           

五、驗證結果

1.源伺服器

[[email protected] ~]# cd nihao/
[[email protected] nihao]# ls
55  da  hehe  nohup.out
           

2.目标伺服器

[[email protected] tmp]# cd /qinyong/
[[email protected] qinyong]# ls
55  da  hehe  nohup.out
           

3.源伺服器

[[email protected] nihao]# mkdir qinyong
[[email protected] nihao]# ls
dad  555  aaa nihao  nohup.out
[[email protected] nihaoi]# touch lala
           

4.目标伺服器

[[email protected] qinyong]# ls
dad  555  aaa nihao  nohup.out lala
           

繼續閱讀