以守護進程(socket)的方式傳輸數據(重點)
以守護進程(socket)的方式傳輸數據(重點)
部署環境:
分別用uname命令查看各系統相關信息
[[email protected]~]# uname -r 6.18-308.el5 [[email protected]~]# uname -s Linux [[email protected]~]# uname -o GNU/Linux [[email protected]~]# uname -i x86_64 [[email protected]~]# uname -n A-Server58
操作系統
主機名 | 網卡eth0 | 默認網關 | 用途 |
[email protected] | 192.168.1.111 | 192.168.1.1 | Rsync服務端 |
[email protected] | 192.168.1.121 | 192.168.1.1 | Rsync 節點 |
[email protected] | 192.168.1.119 | 192.168.1.1 | Rsync 節點 |
子網掩碼均為255.255.255.0
具體需求:
要求在A-Server上以rsync守護進程的方式部署rsync服務,使得所有rsync節點客戶端主機,可以把本地數據通過rsync的方式備份到數據備份服務器A-Server上,本例的客戶端僅以
備份拓撲
1.部署rsync服務
1)rsync服務端配置過程
配置rsyncd.conf
首先確認軟件是否安裝
[[email protected] ~]# rpm -aq rsync rsync-3.0.6-4.el5_7.1 [[email protected]
[[email protected] ~]# dos2unix /etc/rsyncd.conf dos2unix: converting file /etc/rsyncd.confto UNIX format ...
[[email protected] /]# mkdir dingjian [[email protected] /]# ls -ld dingjian drwxr-xr-x 2 root root 4096 Apr 23 01:05dingjian
創建同步的本地目錄/dingjian 並根據需要授權
目錄和/etc/rsync.password為配置文件中path = /dingjian/參數的配置
配置用於rsync同步的賬號、密碼及賬號文件以限
[[email protected] ~]# echo"rsync_backup:95862909">/etc/rsync.password
#其中rsync_backup:95862909中的rsync_backup為同步傳輸用到的虛擬賬號,這個賬號僅為rsync的賬號,不需要是系統賬號,後面的95862909為密碼,不超過8位
[[email protected] ~]# chmod 600/etc/rsync.password #權限必須為600 [[email protected] ~]# cat/etc/rsync.password rsync_backup:95862909 [[email protected] ~]# ll /etc/rsync.password -rw------- 1 root root 22 Apr 22 19:47/etc/rsync.password [[email protected] ~]#
啟動rsync服務
以守護進程方式來啟動rsync服務
[[email protected] ~]# rsync --daemon
拓展:rsync的進程參數選項
--daemon #表示以守護進程的方式啟動rsync服務
--address #綁定指定ip地址
--config=FILE #更改配置文件路徑,而不是默認的/etc/rsyncd.conf
--port=PORT #更改其它端口提供服務,而不是缺省的873端口
提示:以上幾個選項為了解內容,生產場景使用的不多
通過端口查服務
[[email protected] ~]# lsof -i tcp:873 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rsync 27185 root 3u IPv4 44404 0t0 TCP *:rsync (LISTEN) [[email protected] ~]# netstat -lntup|grep873 tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 27185/rsync
設置rsync服務開機自啟動
[[email protected] ~]# echo"/usr/bin/rsync --daemon">>/etc/rc.local [[email protected] ~]# cat /etc/rc.local #!/bin/sh # # This script will be executed *after* allthe other init scripts. # You can put your own initialization stuffin here if you don‘t # want to do the full Sys V style initstuff. touch /var/lock/subsys/local /usr/bin/rsync –daemon
註意當然還可以用chkconfig rsync on命令,但是必須要編寫適合chkconfig操作腳本才行
重起rsync的組合命令
[[email protected] ~]# pkill rsync #關閉rsync服務 [[email protected] ~]# rsync --daemon #啟動rsync服務
檢查啟動的進程
[[email protected] ~]# ps -ef |grep rsync
幾個進程管理命令
殺進程:pkill rsync
killall rsync 可能一次殺不死,要連按連殺
killall -9 rsync 強制殺死進程 -9
kill -9 4084 利用端口號加-9 強制進程,
2)rsync客戶端配置過程
請註意與服務端的配置區別
服務端192.168.1.119 192.168.1.121分別做如下操作
[[email protected] ~]# echo"95862909">/etc/rsync.password #這裏僅配置密碼,不需要賬號,這是與服務端的區別
[[email protected] ~]# chmod 600/etc/rsync.password #必須為600權限 [[email protected] ~]# cat/etc/rsync.password 95862909 [[email protected] ~]# ll /etc/rsync.password -rw-------. 1 root root 9 Apr 22 20:01/etc/rsync.password
此時rsync服務配置大功告成!
檢查部置的rsync服務
默認情況,以下均為rsync客戶端執行操作,下面以ip192.168.1.121B-Server為例說明:
推送(即從客戶端同步文件或目錄到服務器端)
從客戶端推送/etc 目錄到服務端rsync指定的目錄(本文檔為/dingjian)下
rsync -avzP [email protected]::dingjian/ --password-file=/etc/rsync.password
從服務端指定的/dingjian目錄把數據拉取到客戶端本地/tmp目錄下
rsync [email protected]::dingjian/ --password-file=/etc/rsync.password /tmp
2.
排除打包
方法一:
在客戶端操作:
[[email protected]]# rsync -avz --exclude=etc --exclude=tmp .[email protected]::dingjian --password-file=/etc/rsync.password
使用--exclude=進行排除
方法二:
在服務端配置rsyncd.conf參數實現
加入排除參數 exclude=etc tmp
[[email protected] dingjian]# cat/etc/rsyncd.conf sync server #created by oldboy 15:01 2009-6-5 ##rsyncd.conf start## uid = root gid = root use chroot = no max connections = 2000 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 = 192.168.1.1/24 hosts deny = 0.0.0.0/32 auth users = rsync_backup secrets file = /etc/rsync.password exclude=tmp etc [dingjian] comment = www by old0boy 14:18 2012-1-13 path = /dingjian/
重啟rsync服務殺進程,重啟服務
[[email protected] dingjian]# pkill rsync [[email protected] dingjian]# ps -ef|greprsync root 27624 27033 0 22:45 pts/0 00:00:00 grep rsync [[email protected] dingjian]# rsync --daemon [[email protected] dingjian]# ps -ef|greprsync root 27626 1 0 22:45 ? 00:00:00 rsync --daemon root 27632 27033 0 22:45 pts/0 00:00:00 grep rsync
測試推送備份
[[email protected] tmp]# rsync -avz .[email protected]::dingjian --password-file=/etc/rsync.password
3.
無差異同步
要實現這種同步方法就要使用--delete參數了
本地測試
[[email protected] tmp]# rsync -avzP --delete/null/ /tmp/
拉取方法
[[email protected] tmp]# rsync -avzrtopg--delete --progress [email protected]::dingjian/ /tmp/ --password-file=/etc/rsync.password
推送方法
[[email protected] tmp]# rsync -avzrtopg--delete --progress . [email protected]::dingjian/--password-file=/etc/rsync.password
執行--delete參數從rsync服務端往rsync客戶端拉取數據時,一定要小心,最好不用,它比從rsync客戶端帶--delete參數往rsync服務端推送危險得多,客戶端帶--delete參數往服務端推送僅刪除服務端模塊下的數據,而前者有能力刪除rsync客戶端本地的所有數據,包括根下的所有目錄
生場場景沒有特殊要求,應避免使用,一般是有需要兩臺服務器之間,必須要求數據一致且時時性又不是很高的情況,如兩臺負載均衡下面的web服務器之間的同步,或者高可用雙機配置之間的同步等。
4.
多目錄模塊同步
配置/etc/rsyncd.conf配置文件
#Rsync server #created by oldboy 15:01 2009-6-5 ##rsyncd.conf start## uid = root gid = root use chroot = no max connections = 2000 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 = 10.0.0.0/24 hosts deny = 0.0.0.0/32 auth users = rsync_backup secrets file = /etc/rsync.password ######################################## [dingjian] comment = www by old0boy 14:18 2012-1-13 path = /dingjian/ ##################################### [www] comment = www by old0boy 14:18 2012-1-13 path = /data0/www/www/ ##################################### [bbs] comment = bbs by old0boy 14:18 2012-1-13 path = /data0/www/bbs/ ##################################### [blog] comment = blog by old0boy 14:18 2012-1-13 path = /data0/www/blog/
重啟rsync服務
pkill rsync
ps -ef|grep rsync
rsync --daemon
ps -ef|grep rsync
本文出自 “Mr.Xiong`s 運維日誌” 博客,請務必保留此出處http://mrxiong2017.blog.51cto.com/12559394/1933720
以守護進程(socket)的方式傳輸數據(重點)