利用xinetd+rsync實(shí)現(xiàn)多臺(tái)主機(jī)代碼同步

需求:

Server1:172.16.10.1
Server2:172.16.10.2
1、實(shí)現(xiàn)server1代碼修改會(huì)推送給server2
2、server2可以主動(dòng)拉取server1的代碼。

1.安裝rsync和xinetd,并創(chuàng)建目錄:

yum install rsync xinetd
mkdir -p /home/rsync/
mkdir -p /home/rsync/log/
mkdir -p /home/rsync/pid/
mkdir -p /home/rsync/run/

2.修改/etc/xinetd.d/rsync文件,使其隨xinetd啟動(dòng)而啟動(dòng)

  • 修改rsync文件(或者新建,rsync文件如下)
vim /etc/xinetd.d/rsync
......將disable = yes 修改為 disable = no
disable = no

/etc/xinetd.d/rsync文件如下:

# default: off

# description: The rsyncserver is a good addition to an ftp server, as it \

#       allows crc checksumming etc.

service rsync

{

         disable     = no

         flags                   =IPv6

         socket_type     =stream

         wait            = no

         user            = root

         server          =/usr/bin/rsync

         server_args     =--daemon

         log_on_failure  +=USERID

}
  • 啟動(dòng)xinetd服務(wù)

    service xinetd start
    
  • 檢查873端口

    netstat -lntup |grep 873
    

3.在/home/rsync/目錄創(chuàng)建rsyncd.conf

# /etc/rsyncd: configuration file for rsync daemon mode

# see rsyncd.conf man page for more options.

# configuration example:

# uid = nobody
# gid = nobody
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#        path = /home/ftp
#        comment = ftp export area
#
uid = root
gid = root
port=873
use chroot = no
read only = no
list = no
max connections = 0
timeout = 600
log file = /home/rsync/log/rsyncd.log
pidfile = /home/rsync/pid/rsyncd.pid
lock file = /home/rsync/run/rsync.lock
[www]
path = /home/html/
comment = rsync
ignore errors
# 這里的用戶(hù)名和secrets file配置的用戶(hù)名要一致
auth users =  root
secrets file = /home/rsync/rsync.pass
hosts allow = 172.16.0.1,172.16.0.2
hosts deny = *

配置軟鏈接,/etc/rsyncd.conf如果存在就先刪除。

ln -s /home/rsync/rsyncd.conf /etc/rsyncd.conf

4.創(chuàng)建用戶(hù)認(rèn)證文件

  • rsync.pass 文件用于其他服務(wù)器同步主服務(wù)器代碼時(shí)候的認(rèn)證
vim /home/rsync/rsync.pass
#用戶(hù)名:密碼
root:passwd
  • passwd.txt 文件用于向其他服務(wù)器推送代碼時(shí)候的認(rèn)證,如果該服務(wù)器不需要向其他服務(wù)器推送代碼則可以不創(chuàng)建改文件。
vim /home/rsync/passwd.txt
#密碼
passwd

5.設(shè)置文件權(quán)限

chmod 600 /home/rsync/rsyncd.conf
chmod 600 /home/rsync/rsync.pass
chmod 600 /home/rsync/passwd.txt

6.重啟xinetd服務(wù)

service xinetd restart

7.同步代碼腳本:

  • 場(chǎng)景:server2同步server1的[www]模塊配置路徑下的代碼到/home/html文件夾
rsync -vzrtopg --progress root@172.16.10.1::www /home/html --password-file=/home/rsync/passwd.txt

8.推送代碼腳本

  • 場(chǎng)景:server1主動(dòng)推送/var/www/html文件夾下的代碼到server2的[www]模塊配置的路徑
    如果命令里面寫(xiě)的是/var/www/html,[www]配置的/var/www/html/,則推送后Server1的html文件夾會(huì)在server2的/var/www/html里面
    如果命令里面寫(xiě)的是/var/www/html/,[www]配置的/var/www/html/,則推送后Server1的html文件夾的內(nèi)容會(huì)在server2的/var/www/html里面,即不會(huì)出現(xiàn)server1的html文件夾放到server2的html文件夾里面的情況。
rsync -avH --port=873 --progress /var/www/html/ ougd2@172.16.10.2::www --password-file=/home/rsync/passwd.txt

9.設(shè)置定時(shí)任務(wù)計(jì)劃

可以通過(guò)定時(shí)任務(wù)定時(shí)同步代碼

crontab -e
#每分鐘執(zhí)行一次
*/1 * * * * /rsync -vzrtopg --progress root@172.16.10.1::www /home/html --password-file=/home/rsync/passwd.txt

[rsync常見(jiàn)問(wèn)題及解決辦法]
(https://blog.whsir.com/post-392.html)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容