需求:
Server1:172.16.10.1
Server2:172.16.10.2
1、实现server1代码修改会推送给server2
2、server2可以主动拉取server1的代码。
1.安装rsync和xinetd,并创建目录:
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启动而启动
- 修改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
}
-
启动xinetd服务
service xinetd start
-
检查873端口
netstat -lntup |grep 873
3.在/home/rsync/目录创建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
# 这里的用户名和secrets file配置的用户名要一致
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.创建用户认证文件
- rsync.pass 文件用于其他服务器同步主服务器代码时候的认证
vim /home/rsync/rsync.pass
#用户名:密码
root:passwd
- passwd.txt 文件用于向其他服务器推送代码时候的认证,如果该服务器不需要向其他服务器推送代码则可以不创建改文件。
vim /home/rsync/passwd.txt
#密码
passwd
5.设置文件权限
chmod 600 /home/rsync/rsyncd.conf
chmod 600 /home/rsync/rsync.pass
chmod 600 /home/rsync/passwd.txt
6.重启xinetd服务
service xinetd restart
7.同步代码脚本:
- 场景:server2同步server1的[www]模块配置路径下的代码到/home/html文件夹
rsync -vzrtopg --progress root@172.16.10.1::www /home/html --password-file=/home/rsync/passwd.txt
8.推送代码脚本
- 场景:server1主动推送/var/www/html文件夹下的代码到server2的[www]模块配置的路径
如果命令里面写的是/var/www/html,[www]配置的/var/www/html/,则推送后Server1的html文件夹会在server2的/var/www/html里面
如果命令里面写的是/var/www/html/,[www]配置的/var/www/html/,则推送后Server1的html文件夹的内容会在server2的/var/www/html里面,即不会出现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.设置定时任务计划
可以通过定时任务定时同步代码
crontab -e
#每分钟执行一次
*/1 * * * * /rsync -vzrtopg --progress root@172.16.10.1::www /home/html --password-file=/home/rsync/passwd.txt
[rsync常见问题及解决办法]
(https://blog.whsir.com/post-392.html)
网友评论