0
点赞
收藏
分享

微信扫一扫

怎样在linux服务器之间同步程序

芭芭蘑菇 2022-01-22 阅读 71

在linux之间同步程序或者linux和window之间同步,用到软件rsync。由于有主备web两个网站,没人给我们部署同步服务推来推去的,改程序要更新两个地方,很麻烦,只好自己动手了。部署的也是linux之间的同步。脚本不难,主要是要测试。

首先写部署脚本install.sh

#!/bin/bash
#shell检验基础维护脚本
#20210726
#zlz
#----------------------------------------------------------
yum install rsync -y
mypath=$(dirname $0)
echo "请选择不是服务端还是客户端0:服务端  1:客户端"
read check
if [ ${check} -eq 0 ];then 
	echo "拷贝rsyncd.conf到/etc/rsyncd.conf"
	cp ${mypath}/rsyncd.conf  /etc/rsyncd.conf
	echo "拷贝lisrsyncservice.service到/etc/systemd/system/lisrsyncservice.service"
	cp ${mypath}/lisrsyncservice.service /etc/systemd/system/lisrsyncservice.service
	systemctl daemon-reload
	systemctl enable lisrsyncservice.service
	systemctl stop lisrsyncservice.service
	systemctl start lisrsyncservice.service
	systemctl status lisrsyncservice.service
	ps aux | grep rsync
elif [ ${check} -eq 1 ];then 
	echo "拷贝rsyncclient.sh到/dthealth/app/dthis/imedicallis/tool/rsyncclient.sh"
	cp ${mypath}/rsyncclient.sh  /dthealth/app/dthis/imedicallis/tool/rsyncclient.sh
	echo "拷贝rsyncexclude.txt到/dthealth/app/dthis/imedicallis/tool/rsyncexclude.txt"
	cp ${mypath}/rsyncexclude.txt  /dthealth/app/dthis/imedicallis/tool/rsyncexclude.txt
	echo "拷贝lisrsync.service到/etc/systemd/system/lisrsync.service"
	cp ${mypath}/lisrsync.service /etc/systemd/system/lisrsync.service
	systemctl daemon-reload
	systemctl enable lisrsync.service
	systemctl stop lisrsync.service
	systemctl start lisrsync.service
fi
echo "安装完成"

然后准备好默认的服务端rsync配置

#yum install rsync -y
#vim /etc/rsyncd.conf
#[ -f /var/run/rsyncd.pid ] && kill `cat /var/run/rsyncd.pid`
#/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
#ps aux | grep rsync
######### 全局配置参数 ##########
# 指定rsync端口。默认873
port=8874
# rsync服务的运行用户,默认是nobody,文件传输成功后属主将是这个uid
uid = root 
# rsync服务的运行组,默认是nobody,文件传输成功后属组将是这个gid
gid = root 
# rsync daemon在传输前是否切换到指定的path目录下,并将其监禁在内
use chroot = no 
# 指定最大连接数量,0表示没有限制
max connections = 5 
# 确保rsync服务器不会永远等待一个崩溃的客户端,0表示永远等待
timeout = 300         
# 客户端连接过来显示的消息
motd file = /var/rsyncd/rsync.motd   
# 指定rsync daemon的pid文件
pid file = /var/run/rsyncd.pid 
# 指定锁文件      
lock file = /var/run/rsync.lock    
# 指定rsync的日志文件,而不把日志发送给syslog  
log file = /var/log/rsyncd.log      
# 指定哪些文件不用进行压缩传输 
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2  
 
###########下面指定模块,并设定模块配置参数,可以创建多个模块###########
# 模块ID
[imedicallis]     
# 指定该模块的路径,该参数必须指定。启动rsync服务前该目录必须存在。rsync请求访问模块本质就是访问该路径。   
path = /dthealth/app/dthis/imedicallis/ 
# 忽略某些IO错误信息
ignore errors    
# 指定该模块是否可读写,即能否上传文件,false表示可读写,true表示可读不可写。所有模块默认不可上传  
read only = true  
# 指定该模式是否支持下载,设置为true表示客户端不能下载。所有模块默认可下载
write only = false 
# 客户端请求显示模块列表时,该模块是否显示出来,设置为false则该模块为隐藏模块。默认true
list = false    
# 指定允许连接到该模块的机器,多个ip用空格隔开或者设置区间   
#hosts allow = 10.0.0.0/24 
# 指定不允许连接到该模块的机器
hosts deny = 0.0.0.0/32   
# 指定连接到该模块的用户列表,只有列表里的用户才能连接到模块,用户名和对应密码保存在secrts file中,
#auth users = rsync_backup 
# 这里使用的不是系统用户,而是虚拟用户。不设置时,默认所有用户都能连接,但使用的是匿名连接
#secrets file = /etc/rsyncd.passwd # 保存auth users用户列表的用户名和密码,每行包含一个username:passwd。由于"strict modes"
                                  # 默认为true,所以此文件要求非rsync daemon用户不可读写。只有启用了auth users该选项才有效。

然后实现客户端同步时候执行的脚本rsyncclient.sh

#!/bin/bash
#shell检验基础维护脚本
#20210726
#zlz
#----------------------------------------------------------
serverip=192.168.127.83
echo "执行客户端同步任务"
rsync -vzrtopg --port=8874 --progress --exclude-from '/dthealth/app/dthis/imedicallis/tool/rsyncexclude.txt' --delete test@${serverip}::imedicallis  /dthealth/app/dthis/imedicallis/ 
exit

同时准备排除的配置文件rsyncexclude.txt

appsettings.json
imedicallis/download/printtmp
imedicallis/download/tmp
Logs
CTrak

然后写客户端定时启动任务lisrsync.service

[Unit]
Description=lisrsync
[Service]
WorkingDirectory=/dthealth/app/dthis/imedicallis
ExecStart=/usr/bin/bash /dthealth/app/dthis/imedicallis/tool/rsyncclient.sh
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=300
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target

以上文件准备好放到一个文件夹下上传到linux后运行cd到脚本目录bash install.sh根据服务端还是客户端选择部署

服务端启动命令
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf

服务端启动后客户端就会定时同步更新文件

如果程序更新某些文件后需要重启还能在rsyncclient.sh写相关重启命令来实现。

举报

相关推荐

0 条评论