最近在弄国产化操作系统适配,由于别人公司不让用VPN, 只能通过一个公网映射端口做ssh登录,而安装代码又是放在内网的其它服务器上, 代码调试很不方便,所以想到用rsync来从公网服务器上自动同步代码到内网服务器上
首先搭建rsync服务:
- 安装rsync:
1apt-get install rsync - 在source服务器上运行命令(可以同时同步到两台内网服务器上)
1234567nohup inotifywait -mqr -e modify --timefmt '%Y-%m-%d %H:%M:%S' \--format '%w %f %e %T' /data/iuap_installer | while read file; do \for i in 192.168.1.3 192.168.1.98; do \rsync -avzP -e ssh /data/iuap_installer/ root@$i:/data/iuap_installer/ \>/dev/null 2>&1; \done; \done &
如果用cron来定时同步的话, 可以简单地配置ssh免密, 来通过ssh同步:
|
1 2 3 |
for dir in /armbuild /data; do rsync -avzP -e "ssh -i /root/.ssh/id_rsa -p 22" $dir rsync@10.x.x.x:/ali-nas/gPaaS/48.49/ done |
注: 如果用rsync模块来同步的话, 可以来配置rsync服务器:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
cat > /etc/rsyncd.conf << EOF # /etc/rsyncd: configuration file for rsync daemon mode # See rsyncd.conf man page for more options. # 传输文件使用的用户和用户组,如果是从服务器=>客户端,要保证www用户对文件有读取的权限;如果是从客户端=>服务端,要保证www对文件有写权限。 uid = root gid = root # 允许chroot,提升安全性,客户端连接模块,首先chroot到模块path参数指定的目录下,chroot为yes时必须使用root权限,且不能备份path路径外的链接文件 use chroot = yes # 只读 read only = no # 只写 write only = no # 设定白名单,可以指定IP段(172.18.50.1/255.255.255.0),各个Ip段用空格分开 hosts allow = 10.10.0.0/255.255.0.0,192.168.0.0/255.255.0.0 #hosts deny = * # 允许的客户端最大连接数 max connections = 10 # 欢迎文件的路径,非必须 motd file = /etc/rsync/rsyncd.motd # pid文件路径 pid file = /var/run/rsyncd.pid # 记录传输文件日志 transfer logging = yes # 日志文件格式 log format = %t %a %m %f %b # 指定日志文件 log file = /var/log/rsync.log # 剔除某些文件或目录,不同步 exclude = lost+found/ # 设置超时时间 timeout = 900 ignore nonreadable = yes # 设置不需要压缩的文件 dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 # 模块,可以配置多个,使用如: sate@172.18.50.125::125to110 [test] # 模块的根目录,同步目录,要注意权限 path = /data/iuap_installer # 是否允许列出模块内容 list = no # 忽略错误 ignore errors # 添加注释 comment = ftp export area # 模块验证的用户名称,可使用空格或者逗号隔开多个用户名 auth users = root # 模块验证密码文件 可放在全局配置里 secrets file = /etc/rsyncd.secrets # 剔除某些文件或目录,不同步 exclude = lost+found/ conf/ man/ |
|
1 2 3 |
cat > /etc/rsyncd.secrets <<EOF mypassword EOF |
具体使用方法如下:
|
1 |
rsync -avzP --password-file=/etc/rsyncd.secrets root@192.168.1.3::test |
0 Comments