1. 程式人生 > >數據同步服務Rsync

數據同步服務Rsync

%d centos7 ttr set 傳輸 並運行 aos 本地 表示

1.Rsync基本概述
Rsync是開源多功能同步工具,支持多種操作系統
Rsync支持本地復制(優於scp,cp)與遠程同步
Rsync支持全量備份,增量備份
Rsync基於C/S架構,默認監聽tcp873端口
2.Rsync優點
支持增量備份,第一次全量備份,第二次增量備份。邊復制邊比較邊統計,傳輸效率高。
數據集中備份,客戶端可以推送數據至服務端,也可以從服務端獲取數據,與客戶端為參照物。
保持文件屬性,符號鏈接,硬鏈接,權限,時間等。
安全方式傳輸,Rsync本身不對數據加密,使用ssh作為傳輸端口。
指定排除文件,排除無需同步的文件或目錄。
進程方式同步,rsync運行在C/S架構,通過進程方式傳輸文件或數據。
.缺點:
1.大量小文件同步會比較慢,需要比對時間較長,可能造成Rsync進程停止
解決思路:將小文件進行打包,然後再同步,減小比對時間,傳輸效率更高
2.同步大文件會出現中斷情況,而且長時間同步會造成網絡資源耗盡
解決思路:配置限速同步,未同步完之前修改為隱藏文件,同步完後修改為正常文件

Rsync命令格式:
rsync [選項] 源文件 [user@]host::目錄
rsync [選項] 源文件 rsync:// [user@]host:[:port]/目錄
-a選項
:遞歸傳輸目錄
:保持文件或目錄的時間、文件屬性、屬組、時間、軟鏈接
:顯示同步的過程
:設備
-z選項
:傳輸時進行壓縮以提高效率

-v選項
:詳細模式輸出,打印速率,文件數量等
-e選項
:使用的信道協議,指定替代rsh的shell程序
--delete 讓目標目錄和源目錄數據保持一致
--partial 斷點續傳
--bwllimit=100 限速傳輸
--exclude-from=file 文件名所在的目錄
--exclude=PATTERN 指定排除不需要傳輸的文件模式

rsync服務部署
環境:
兩臺主機:源服務器:192.168.56.11
目標服務器:192.68.56.12
需求:
把源服務器上的/etc目錄實時同步到目標服務器的/tmp/目錄下
在目標服務器上做以下操作:
1.關閉防火墻和selinux
[root@hyj ~]# systemctl stop firewalld

[root@hyj ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@hyj ~]# setenforce 0
[root@hyj ~]# sed -ri ‘s/^(SELINUX=).*/\1disabled/g‘ /etc/sysconfig/selinux

2.安裝rsync服務端軟件
[root@hyj ~]# yum install rsync -y
3.配置rsync.conf配置文件

[root@hyj ~]# cat >> /etc/rsyncd.conf <<EOF

log file = /var/log/rsyncd.log // 日誌文件位置,啟動rsync後自動產生
pidfile = /var/run/rsyncd.pid //pid文件存放位置
lock file = /var/run/rsync.lock //支持max connections參數的鎖文件
secrets file = /etc/rsync.pass //用戶認證配置文件,裏面保存用戶名稱和密碼,必須手動創建這個文件
[etc_from_client] //自定義同步名稱
path = /heyuanjie/ //rsync服務端數據存放路徑
,客戶端的數據將同步至此目錄
comment = gaosiao
uid = root //設置rsync運行權限為root
gid = root //設置rsync運行權限為root
port = 873 //默認端口
ignore errors //表示出現錯誤忽略錯誤
use chroot = no // 默認為true,修改為no,增加對目錄文件軟連接的備份
read only = no //設置rsync服務端為讀寫權限
list = no //不顯示rsync服務端資源列表
max connection= 200 //最大連接數
timeout = 600 //設置超時時間
auth users = admin //執行數據同步的用戶名,可設置多個,用英文狀態下逗號隔開
hosts allow = 192.168.56.11 //允許進行數據同步的客戶端ip,可設置多個,逗號隔開
hosts deny = 192.168.1.1 //禁止數據同步的客戶端IP地址
EOF

4.創建用戶認證文件,並設置文件權限

[root@hyj ~]# echo ‘admin:123456‘ > /etc/rsync.pass
[root@hyj ~]# cat /etc/rsync.pass
admin:123456
[root@hyj ~]# chmod 600 /etc/rsync
[root@hyj ~]# ll /etc/rsync

-rw-------. 1 root root 825 Aug 16 15:42 /etc/rsyncd.conf
-rw-------. 1 root root 13 Aug 16 15:55 /etc/rsync.pass

5.啟動rsync服務並加入開機自啟

[root@hyj ~]# systemctl start rsyncd
[root@hyj ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi
user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
[root@hyj ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :22 :
LISTEN 0 100 127.0.0.1:25
:
LISTEN 0 5
:873 :
LISTEN 0 128 :::22 :::
LISTEN 0 100 ::1:25 :::

LISTEN 0 5 :::873 :::*

在源服務器上做以下操作:
1.關閉防火墻和selinux
[root@hejie ~]# systemctl stop firewalld
[root@hejie ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@hejie ~]# setenforce 0
[root@hejie ~]# sed -ri ‘s/^(SELINUX=).*/\1disabled/g‘ /etc/sysconfig/selinux

2.配置yum源

[root@hejie yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@hejie yum.repos.d]# sed -i ‘s/\$releasever/7/g‘ /etc/yum.repos.d/CentOS7-Base-163.repo
[root@hejie yum.repos.d]# sed -i ‘s/^enabled=.*/enabled=1/g‘ /etc/yum.repos.d/CentOS7-Base-163.repo
[root@hejie ~]# yum -y install epel-release

3.安裝rsync服務端軟件,只需安裝,不要啟動,不需要配置
[root@hejie ~]# yum -y install rsync

4.創建認證密碼文件,並設置權限,設置文件所有者具有讀取、寫入權限即可
[root@hejie ~]# echo ‘123456‘ > /etc/rsync.pass
[root@hejie ~]# cat /etc/rsync.pass
123456
[root@hejie ~]# chmod 600 /etc/rsync.pass
[root@hejie ~]# ll /etc/rsync.pass

-rw-------. 1 root root 7 Aug 16 16:44 /etc/rsync.pass

5.在源服務器上創建測試目錄,並運行以下命令

[root@hejie ~]# mkdir -pv /root/etc/test
mkdir: created directory ‘/root/etc’
mkdir: created directory ‘/root/etc/test’
[root@hejie ~]# rsync -avH --port 873 --progress --delete /root/etc/ [email protected]::etc_from_client --password-file=/etc/rsync.pass

6.運行完成後,在目標服務器上查看,在/heyuanjie目錄下有test目錄,說明數據同步成功

[root@hyj ~]# cd /heyuanjie
[root@hyj heyuanjie]# ls
test

7.安裝inotify-tools工具,實時觸發rsync進行同步
  //查看服務器內核是否支持inotify

[root@hejie ~]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r--. 1 root root 0 Aug 16 16:57 max_queued_events
-rw-r--r--. 1 root root 0 Aug 16 16:57 max_user_instances
-rw-r--r--. 1 root root 0 Aug 16 16:57 max_user_watches
如果有以上三個max開頭的文件則表示服務器內核支持inotify
//安裝inotify-tools
[root@hejie ~]# yum -y install make gcc gcc-c++
[root@hejie ~]# yum -y install inotify-tools

//寫同步腳本,讓腳本自動去檢測我們制定的目錄下文件發生的變化,然後再執行rsync的命令把它同步到我們的服務器端去
[root@hejie ~]# mkdir /scripts
[root@hejie ~]# touch /scripts/inotify.sh
[root@hejie ~]# chmod 755 /scripts/inotify.sh
[root@hejie ~]# vi /scripts/inotify.sh

host=192.168.56.12 //目標服務器的ip(備份服務器)
src=/etc //在源服務器上所要監控的備份目錄(可自定義,但必須存在)
des=etc_from_client //自定義模塊名,需要與目標服務器上定義的同步名稱一致
password=/etc/rsync.pass //執行數據同步的密碼文件
user=admin //執行數據同步的用戶名
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 $user@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done

//啟動腳本
[root@hyj ~]# nohup bash /scripts/inotify.sh &
[1] 8784
[root@hyj ~]# nohup: ignoring input and appending output to ‘nohup.out’
[root@hyj ~]# ps -ef|grep inotify
root 8784 8528 0 00:46 pts/0 00:00:00 bash /scripts/inotify.sh
root 8785 8784 0 00:46 pts/0 00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /etc
root 8786 8784 0 00:46 pts/0 00:00:00 bash /scripts/inotify.sh
root 8788 8528 0 00:47 pts/0 00:00:00 grep --color=auto inotify

//在源服務器上生成一個新文件
[root@hyj ~]# mkdir /etc/aaa
會觸發同步到目標服務器上的/heyuanjie/etc中
[root@hyj ~]# cd /heyuanjie/
[root@hyj heyuanjie]# ls
etc test
[root@hyj heyuanjie]# cd etc/
[root@hyj etc]# ls
aaa
//查看inotify生成的日誌,可以看到生成了一個aaa目錄文件
[root@hyj ~]# tail /tmp/rsync.log
20180817 00:49 /etc/aaaCREATE,ISDIR was rsynced

設置腳本開機自啟動:
[root@hyj ~]# chmod +x /etc/rc.d/rc.local
[root@hyj ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 473 Aug 5 2017 /etc/rc.d/rc.local
[root@hyj ~]# echo ‘nohup /bin/bash /scripts/inotify.sh‘ >> /etc/rc.d/rc.local
[root@hyj ~]# tail -1 /etc/rc.d/rc.local

nohup /bin/bash /scripts/inotify.sh

數據同步服務Rsync