利用rsync实现windows定时向linux同步数据(增量同步)

windows同步Linux数据

1、Linux安装rsync服务端

yum -y install rsync

准备配置文件
vim /etc/rsyncd.conf

uid = root
gid = root
use chroot = no
max connections = 2
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

[rsync]
path = /data/mysql/backup
hosts allow = 172.20.20.0/24
auth user = rsync
secrets file = /etc/rsync.txt
read only = yes

#配置文件详解
[rsync] 		#是同步目录的一个别名,path对应实际数据所在路径
hosts.allow 	#哪个网络内的主机可访问该服务
secrets file 	#账户认证文件路径,该文件存放了自定义的用于同步的账号信息(格式为:账号:密码,多个账号写多行即可)
read only = yes #表示客户端只能下载服务端的文件而不能上传(单向同步)
这应该是大部分业务的需求,如果有上传需要,设置read only = no 即可

#准备账号认证文件
vim /etc/rsync.txt
chmod 600  /etc/rsync.txt  #此步骤必须,否则客户端同步时会提示auth failed

echo "rsync:passwd" > /etc/rsync.txt

#启动服务
systemctl enable rsyncd  --now

2、windows安装rsync客户端

下载地址:https://itefix.net/dl/free-software/cwrsync_6.2.4_x64_free.zip
解压到指定目录后将bin目录的路径添加到path环境变量完成安装

3、windows客户端同步命令

rsync --list-only  rsync@172.20.20.20::rsync --password-file=/cygdrive/d/rsyncpwd.txt #查看服务端有哪些数据 

rsync -av --delete rsync@172.20.20.20::rsync  /cygdrive/d/mysql_backup/ --password-file=/cygdrive/d/rsyncpwd.txt

注意:/cygdrive/d/mysql_backup/  #该写法表示路径(本地数据存放路径):D:\mysql_bakcup\

相关参数:
--delete 表示删除本地tmpfolder目录中跟服务器test01下不一致的所有文件和目录
--password 指定存放了密码的文件路径(用于非交互式同步数据)
-v 表示采用增量的方式同步文件
-a 是 archive mode; same as -rlptgoD; 相当于简写了很多参数

-u, --update      忽略客户端上(比服务端)更加新的文件
-r, --recursive   递归同步目录
-z, --compress    传输时压缩文件数据

4、windows设置计划任务,定时同步数据:

脚本内容:

@echo off
rsync -av --delete 172.20.20.20::rsync  /cygdrive/d/mysql_backup/ --password-file=/cygdrive/d/rsyncpwd.txt