rsync 服务快速部署手册
一、rsync服務端安裝
1、查看rsync安裝包
2、安裝rsync
3、添加rsync服務的用戶,管理本地目錄的
# useradd -s /sbin/nologin -M rsync# id rsync4、生成rsyncd.conf配置文件
#rsync_config______________________start #created by oldboy 15:00 2016-11-15 ##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 # 日志文件 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 # 服務端提供訪問的目錄[nfsbackup] path = /nfsbackup #rsync_config______________________end5、根據rsyncd.conf的auth users配置帳戶,遠程連接的,并根據secrets file 參數生成密碼文件。
6、為密碼文件配置權限
7、創建共享目錄并授權rsync服務管理
8、啟動rsync服務并檢查
9、加入開機自啟動
?
二、rsync客戶端
1、查看rsync安裝包
2、安裝rsync
3、添加rsync服務的用戶,管理本地目錄的
# useradd -s /sbin/nologin -M rsync# id rsync4、生成連接服務器需要的密碼文件
# echo "hkrt" > /etc/rsync.password # cat /etc/rsync.passwordhkrt5、為密碼文件配置權限
6、同步文件語法
基于rsync daemon同步語法:拉取: rsync [OPTION...] [USER@]HOST::SRC... [DEST]rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST] 推送: rsync [OPTION...] SRC... [USER@]HOST::DESTrsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST7、測試手動推送是否正常
[root@nfs01 backup]# pwd /backup[root@nfs01 backup]# ls a.txt c.txt e.txt g.txt i.txt k.txt m.txt o.txt q.txt s.txt u.txt w.txt y.txt b.txt d.txt f.txt h.txt j.txt l.txt n.txt p.txt r.txt t.txt v.txt x.txt z.txt# rsync -avz /backup/ rsync_backup@172.16.1.41::backup/ --password-file=/etc/rsync.password sending incremental file list ./ a.txt b.txt c.txt d.txt e.txt f.txt g.txt h.txt i.txt j.txt k.txt l.txt m.txt n.txt o.txt p.txt q.txt r.txt s.txt t.txt u.txt v.txt w.txt x.txt y.txt z.txtsent 1229 bytes received 505 bytes 3468.00 bytes/sec total size is 0 speedup is 0.00 [root@backup backup]# pwd /backup[root@backup backup]# ls a.txt c.txt e.txt g.txt i.txt k.txt m.txt o.txt q.txt s.txt u.txt w.txt y.txt b.txt d.txt f.txt h.txt j.txt l.txt n.txt p.txt r.txt t.txt v.txt x.txt z.txt?我們看到rsync daemon 服務器/backup目錄已經有文件同步過來了,說明我們已經配置成功了。接下來我們還會學習下,如何做到實例同步,我會通過rsync+inotify、rsync+serync兩種方案來實現數據實時同步!?三、rsync + inotify 實時同步方案? 本想新寫一篇博文來介紹rsync + inotify實時同步方案了,但是要實現這個方案需要滿足以下條件:1、rsync daemon安裝成功 2、rsync 客戶端可以正常推送文件到rsync daemon端 3、安裝配置inotify 前兩個我們已經配置完成了,下面我們只需要在rsync 客戶端安裝inotify并配置即可實現實時同步。?1、安裝inotify # yum install inotify-tools –y# inotifywa??? # 安裝完成后會生成以下兩個命令
inotifywait?? inotifywatch
4、創建監控腳本
我們可以看到,在/data目錄下創建的hkrt.txt文件,已經被inotifywait命令監測到了。這樣inotify就快搞定了,下面我們只需要寫個監控腳本就可以了,腳本內容就是監測到了變化,就執行rsync推送文件到備份服務器就好了,下面看下腳本是怎么寫的吧! #/bin/bash Path=/data Ip=172.16.1.41 /usr/bin/inotifywait -mrq --format '%w%f' -e close_write,delete $Path \ |while read filedocd $Path && \rsync -az ./ --delete rsync_backup@$Ip::nfsbackup --password-file=/etc/rsync.password &done5、創建啟動服務腳本并賦予執行權限
#!/bin/bash #chkconfig: 2345 38 46 . /etc/init.d/functions if [ $# -ne 1 ];thenusage: $0 [start|stop]exit ficase "$1" in start)/bin/bash /root/scripts/inotify.sh &echo $$ > /var/run/inotify.pidif [ `ps -ef|grep inotify|wc -l` -gt 2 ];thenaction "inotify service is started" /bin/trueelseaction "inotify service is started" /bin/falsefi;; stop)kill -9 `cat /var/run/inotify.pid` > /dev/null 2>&1pkill inotifywaitsleep 2if [ `ps -ef|grep inotify|grep -v grep|wc -l` -eq 0 ];thenaction "inotify service is stopped" /bin/trueelseaction "inotify service is stopped" /bin/falsefi;; *)usage: $0 [start|stop]exit 1 esac?6、添加syncd服務并設置開機自啟動
chkconfig --add syncd
chkconfig syncd on
7、啟動/停止inotifywait服務
# /etc/init.d/syncd start inotify service is started [ ] [root@nfs01 scripts]# /etc/init.d/syncd stop inotify service is stopped [ ]8、接下來我們就可以驗證實時同步了
[root@nfs01 data]# pwd /data [root@nfs01 data]# touch stu{01..10} [root@nfs01 data]# ls stu01 stu02 stu03 stu04 stu05 stu06 stu07 stu08 stu09 stu10我們到備份服務器看看有沒有實時同步過去
[root@backup nfsbackup]# pwd /nfsbackup [root@backup nfsbackup]# ls stu01 stu02 stu03 stu04 stu05 stu06 stu07 stu08 stu09 stu10可以看到,新創建的文件已經被實時同步到備份服備器了,rsync + inotify 實時同步也就部署完成了,可能過程中有些內容寫的不是很詳細,只是個安裝過程,大家需要了解更多的內容,可以參考官方技術文檔或查看其它資料。好了,就介紹到這吧。希望大家有所收獲。
四、rsync + sersync 實現實時同步
實現rsync+sersync實時同步我們繼續使用這個環境,所在先要停掉inotify服務。
如果是新環境,我們就可以直接安裝sersync了!
1、安裝sersync,下載后直接使用就可以了,不需要安裝,我們先看下目錄結構吧,這目錄結構是修改過的,如果從官方下載可以會不一樣,不過也沒有太的區別了,只不過有用的就是這兩個文件而已。
# tree sersync/ sersync/ ├── bin │ └── sersync2 ├── conf │ └── confxml.xml └── logs2、我們看下配置文件的內容
# cat /usr/local/sersync/conf/confxml.xml <?xml version="1.0" encoding="ISO-8859-1"?> <head version="2.5"><host hostip="localhost" port="8008"></host><debug start="false"/><fileSystem xfs="false"/><filter start="false"><exclude expression="(.*)\.svn"></exclude><exclude expression="(.*)\.gz"></exclude><exclude expression="^info/*"></exclude><exclude expression="^static/*"></exclude></filter><inotify> # inotify段,指定監測事件<delete start="true"/><createFolder start="true"/><createFile start="false"/><closeWrite start="true"/><moveFrom start="true"/><moveTo start="true"/><attrib start="false"/><modify start="false"/></inotify><sersync><localpath watch="/data"> # 需要同步的路徑<remote ip="172.16.1.41" name="nfsbackup"/> # 指定rsync daemon 備份服務器IP地址、模塊名稱<!--<remote ip="192.168.8.39" name="tongbu"/>--><!--<remote ip="192.168.8.40" name="tongbu"/>--></localpath><rsync><commonParams params="-avz"/> # rsync 參數設置<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/> <userDefinedPort start="false" port="874"/><!-- port=874 --><timeout start="false" time="100"/><!-- timeout=100 --><ssh start="false"/></rsync><failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--><crontab start="false" schedule="600"><!--600mins--><crontabfilter start="false"><exclude expression="*.php"></exclude><exclude expression="info/*"></exclude></crontabfilter></crontab><plugin start="false" name="command"/></sersync><plugin name="command"><param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix--><filter start="false"><include expression="(.*)\.php"/><include expression="(.*)\.sh"/></filter></plugin><plugin name="socket"><localpath watch="/opt/tongbu"><deshost ip="192.168.138.20" port="8009"/></localpath></plugin><plugin name="refreshCDN"><localpath watch="/data0/htdocs/cms.xoyo.com/site/"><cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/><sendurl base="http://pic.xoyo.com/cms"/><regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/></localpath></plugin> </head>3、配置密碼文件,我們這里使用的環境之前已經配置好了,這里就不在配置了,你應該也會明白吧。我們看下吧,記著修改權限哦!
[root@nfs01 local]# cat /etc/rsync.password
oldboy
[root@nfs01 local]# ll /etc/rsync.password
-rw------- 1 root root 7 Nov 15 23:48 /etc/rsync.password
4、啟動sersync服務
set the system param execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events parse the command param option: -r rsync all the local files to the remote servers before the sersync work option: -d run as a daemon option: -o config xml name: /usr/local/sersync/conf/confxml.xml daemon thread num: 10 parse xml config file host ip : localhost host port: 8008 daemon start,sersync run behind the console use rsync password-file : user is rsync_backup passwordfile is /etc/rsync.password config xml parse success please set /etc/rsyncd.conf max connections=0 Manually sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads) please according your cpu ,use -n param to adjust the cpu rate ------------------------------------------ rsync the directory recursivly to the remote servers once working please wait... execute command: cd /data && rsync -avz -R --delete ./ rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password >/dev/null 2>&1 run the sersync: watch path is: /dataroot 4130 1 0 14:45 ? 00:00:00 /usr/local/sersync/bin/sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml root 4146 1949 1 14:45 pts/0 00:00:00 grep sersync5、我們驗證看看有沒有實時同步吧
在sersync端/data目錄下創建幾個文件
在rsync daemon端查看/nfsbackup目錄有沒有剛剛創建的文件
可以看到,文件已經同步過來了,說明沒有問題了,也達到了實時同步的目的。好的,就介紹到這吧。介紹了兩種實時同步的方案。
轉載于:https://www.cnblogs.com/miclesvic/p/6189540.html
總結
以上是生活随笔為你收集整理的rsync 服务快速部署手册的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中班机器人教室设计方案_奇思妙想一起玩,
- 下一篇: kindle字体设置