Rsync服務搭建小結

來源:互聯網
上載者:User

最近由於業務上的考慮,把內容的點擊數、播放數等變化頻換(每日1000W-2000w次)但是對於業務沒有太大即時意義的計數,由即時操作DB變更為只記錄動作記錄,每晚匯總各機器的動作記錄,計算各內容點擊與播放的總數一次性更新DB,將DB的操作降低到百萬層級以內了。
這個過程中使用rsync來進行動作記錄的同步,為避免遺忘特記錄下配置rsync服務以及進行記錄檔傳輸的過程。
機器以及配置過程如下:
192.168.1.100作為rsync伺服器,提供rsync服務;
192.168.1.101 、192.168.1.102 、192.168.1.103、192.168.1.104作為應用伺服器;
(1)配置rsync服務端(192.168.1.100)
當前的絕大多數Linux作業系統都安裝有rsnyc服務,如果不升級版本的話,可以直接使用,要是需要升級的話,可以參考網上教程。
檢查是否存在/etc/rsyncd.conf 檔案,不存在則建立,檔案的內容如下。

pid file = /var/run/rsyncd.pidport = 873#只監控內網的IP地址,更加安全address = 192.168.1.100uid =rootgid = rootuse chroot = yes#用戶端只能寫入不能讀取read only = nowrite only = yes#只允許指定IP地址的機器能夠訪問hosts allow = 192.168.1.101 192.168.1.102 192.168.1.103 192.168.1.104max connections = 4motd file = /etc/rsyncd/rsyncd.motdtimeout = 300transfer logging = yeslog file = /var/log/rsync.log#用戶端上傳檔案時候的儲存模組[ logs ]         #用戶端上傳檔案的儲存地址        path = /var/log/access_log          list = yes        ignore errors        auth users = zhangzk        #密碼檔案        secrets file = /etc/rsyncd/rsyncd.secrets        exclude = error_log httpd.pid

建立密碼檔案 /etc/rsyncd/rsyncd.secrets,使用者名稱稱與密碼以冒號分割,整個檔案內容如下:

zhangzk:239fjdalk@893246dasaATDFBSad

設定該檔案的600許可權

chmod 600 /etc/rsyncd/rsyncd.secrets

以守護進程方式啟動該服務:

/usr/local/bin/rsync --daemon
檢查服務是否運行正常:

-bash-3.2# ps -aef |grep rsyncroot      8510  8481  0 18:30 pts/0    00:00:00 grep rsyncroot     31787     1  0 Aug14 ?        00:00:00 rsync --daemon

至此服務端全部搞定了。

(2)用戶端(以192.168.1.101為例)

用戶端相對要簡單得多了,只需要設定密碼檔案即可。

建立密碼檔案/etc/rsyncd/ rsyncd.pass,該密碼檔案的內容與服務端的密碼檔案中的密碼必須一致。

239fjdalk@893246dasaATDFBSad

設定該檔案的600許可權

chmod 600 /etc/rsyncd/rsyncd.pass

OK!現在可以使用rsync服務從用戶端192.168.1.101上無密碼的方式傳輸檔案到192.168.1.100上去了!

rsync -vzrtopg --delete --progress $tarFile zhangzk@192.168.1.100::logs  --password-file=/etc/rsyncd/rsyncd.pass

(3)建立crontab任務來定時傳輸記錄檔

檢查並且建立指令碼檔案/var/scripts/rsync_operate_log.sh

#!/bin/shfileName=my_play_download.log.`date -d yesterday +%Y-%m-%d`filePath=/var/log/app/logFile=$filePath$fileNameipAddress=101tarName=$fileName.$ipAddress.tar.gztarFile=$filePath$tarNamecd $filePathif [ -e "$logFile" ]; then  tar -zcvf $tarName $fileName;else  echo "log file not existed.file=$logFile"fiif [ -e "$tarFile" ]; then   rsync -vzrtopg --delete --progress $tarFile zhangzk@192.168.1.100::logs  --password-file=/etc/rsyncd/rsyncd.pass   rm -rf $tarFileelse   echo "log file not existed.file=$tarFile"fi

設定該crontab指令檔的700許可權:

chmod 700 /var/scripts/rsync_operate_log.sh

再使用crontab -e命令來設定每日淩晨3點定時執行指令檔即可:

0 3 * * * /var/scripts/rsync_operate_log.sh > /dev/null 2>&1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.