使用rsync定期同步windows系统文件到Linux系统
1 Windows 平台 cwRsync 安装配置成客户端,通过设置计划任务每 5 分钟推文件到 linux 的 rsync 服务端。
2 linux 配置成服务端。
3 记录同步详细日志,linux平台记录日志。
4 只同步新增或者修改过的不一样的文件,忽略时间、权限的同步。
示意图如下:
+----------------------+ | +----------------------+
| Windows IIS web |10.10.100.2 |10.10.100.0| Linux Nginx web |
| +------------+-----------+ |
| /data/htdocs/* | ---------------> | /data/htdocs/* |
+----------------------+ copy +----------------------+
一、Windows 客户端配置:
cwRsync是基于cygwin平台的rsync软件包,支持windows对windows、windows对Linux、Linux对windows高效文件同步。由于CwRsync已经集成了cygwin类库,因此安装的时候可以省去cygwin包。Cwrsync还集成了OpenSSH for windows,可以实现Linux 下Rsync一模一样的操作。使用 cwRsync 来同步文件后,只需要对一台主服务器进行文件修改,其他镜像服务器可以自动同步,包括文件的更新、删除、重命名等。
cwRsync分为付费版和免费版两种,我们只需要使用免费版即可,在官方网站上面下载 cwRsync Free Edition 版本。
https://www.itefix.net/content/cwrsync-free-edition
Name: cwRsync_5.5.0_x86_Free.zip
SHA256: 37e8ef21ac975d4ee86c9d3be40c8935e8b9d0ba84e9302fc106b9452296cb85
包含如下几个程序
Version information:
Rsync 3.1.2
Cygwin 2.3.1
OpenSSH 7.1p
OpenSSL 1.0.2e
1.2 解压 cwRsync_5.5.0_x86_Free.zip 到 D:\data\app\cwRsync_5.5.0_x86_Free 目录中
1.3 双击 cwrsync.cmd 运行,会在当前生成 home\%USERNAME%\.ssh 目录,供 ssh 认证方式使用。
1.4 为系统新建一个环境变量,目录为cwrsync的bin目录下,例:path:D:\data\app\cwRsync_5.5.0_x86_Free\bin。这样 cmd 命令行下可以直接运行 rsync 这个命令
C:\Users\Administrator>rsync --version
rsync version 3.1.2 protocol version 31
2.1 Ubunut 16.04 下安装 rsync
apt-get install rsync
# rsync --version
rsync version 3.1.1 protocol version 31
2.2 配置文件
vim /etc/rsyncd.conf
pid file = /var/run/rsync.pid
port = 873
log file = /var/log/rsyncd.log
lock file = /var/run/rsync.lock
# create new
# any name you like
[resources]
# destination directory to copy
path = /data/htdocs/
# hosts you allow to access
hosts allow = 10.10.100.2
hosts deny = *
secrets file = /etc/rsyncd.pass
# 表示该节点是否可被发现。
list = true
# 指定传输到这里文件所属的用户。
uid = www-data
# 指定传输到这里的文件所属的组。
gid = www-data
# rsync连接时的用户名,要和客户端rsync的命令一致
auth users = www-data
# 该目录是否只读
read only = no
transfer logging = yes
log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes.
timeout = 600
# 创建密码文件
touch /etc/rsyncd.pass
#权限修改
chown root:root /etc/rsyncd.pass
chmod 600 /etc/rsyncd.pass
vim /etc/rsyncd.pass
内容为:www-data:passwd # 用户名:密码
2.3 启动服务端
service rsync start
# 添加开机启动
update-rc.d rsync defaults
2.4 检查 rsync 端口,确认已启动
netstat -ntlpu | grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 25753/rsync
tcp6 0 0 :::873 :::* LISTEN 25753/rsync
3.1 客户端测试
新建立目录
D:\data\app\cwRsync_5.5.0_x86_Free\etc
创建 rsync.pass 文件
输入 rsync 服务端的密码
# -u 同步不覆盖(源到目的)目的产生的新文件内容,不修改覆盖目标目录下的新增类的文件
完整的同步命令:
1
rsync -acrtzv --password-file=/cygdrive/d/data/app/cwRsync_5.5.0_x86_Free/etc/rsync.pass /cygdrive/d/data/htdocs/ www-data@10.10.100.0::resources
日志:
2017/02/27 14:44:38 [25798] name lookup failed for 10.10.100.2: Name or service not known
解决这个 not known 的问题,把客户端的 hostname 写入服务端的系统 host 文件中
vim /etc/hosts
10.10.100.2 IISWEB01
3.2 创建同步脚本文件
D:\data\app\cwRsync_5.5.0_x86_Free\etc
dynamicres_sync.bat
@echo off
D:\data\app\cwRsync_5.5.0_x86_Free\bin\rsync -acrtzv --password-file=/cygdrive/d/data/app/cwRsync_5.5.0_x86_Free/etc/rsync.pass /cygdrive/d/data/htdocs/ www-data@10.10.100.0::resources
4.1 在Windows中创建任务计划:
每5分钟执行一次 dynamicres_sync.bat 这个同步脚本
5.1 Linux Rsync server 日志
发现日志中第二行和第三行时间是使用 UTC 时间。
# tail -100f /var/log/rsyncd.log
2017/02/27 16:15:58 [26150] connect from IISWEB01 (10.10.100.2)
2017/02/27 08:15:58 [26150] rsync to resources/ from www-data@IISWEB01 (10.10.100.2)
2017/02/27 08:15:58 [26150] receiving file list
2017/02/27 08:15:59 [26150] sent 25 bytes received 7441 bytes total size 8515857
2017/02/27 16:20:58 [26156] connect from IISWEB01 (10.10.100.2)
2017/02/27 08:20:58 [26156] rsync to resources/ from www-data@IISWEB01 (10.10.100.2)
2017/02/27 08:20:58 [26156] receiving file list
2017/02/27 08:20:59 [26156] sent 25 bytes received 7441 bytes total size 8515857
windows 版免费 server 端
github 地址
https://github.com/backuppc/cygwin-rsyncd
一,先安装 脚本工具
nsis-3.01-setup.exe
2
右键 backuppc_rsync-server.nsi
会生成
cygwin-rsyncd-3.1.2.1_installer.exe 安装工具
3 运行 cygwin-rsyncd-3.1.2.1_installer.exe 安装工具
3.1 会自动复制各个程序到 C:\rsyncd 目录下
3.1 会自动把Rsyncd添加到服务里面启动。
4 修改配置
c:\rsyncd\rsyncd.conf and c:\rsyncd\rsyncd.secrets files to set your client-specific shares,
backup user name and password.
重启rsyncd 服务才会生效
restart the Windows RsyncServer service to get the new settings.
5 配置文件权限 rsyncd.secrets 去掉 user用户组,只保留administrator 和 system
rsyncd.conf 修改限制IP项目
To ensure initial security, the c:\rsyncd\rsyncd.secrets file initially has no users, and the c:\rsyncd\rsyncd.conf only allows connections from two specific IP addresses. So unless you edit those two files you won't be able to connect to the rsyncd server.
6、开放 873 内网访问
If you have Windows firewall enabled then you will need to allow rsync to listen on TCP port 873. You can do that through the WinXX firewall menus. You can also make that rule specific to the BackupPC server IP addresses, so no other hosts can contact the rsyncd server on this client.
使用rsync定期同步windows系统文件到Linux系统:等您坐沙发呢!