Linux-Rsync两台机器文件数据同步传输

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: rsync 服务器是指以 deamon 方式运行 rsync 服务的服务器,需要打开 rsync deamon 和启动 xinetd 服务。默认端口873。rsync 客户端是发起 rsync 连接的服务器,安装rsync即可。

rsync 服务器是指以 deamon 方式运行 rsync 服务的服务器,需要打开 rsync deamon 和启动 xinetd 服务。默认端口873。rsync 客户端是发起 rsync 连接的服务器,安装rsync即可。rsync 客户端发起连接后,rsync 服务器会检查 rsync 客户端提交 rsync 服务器内建的户名和密码是否正确,如果通过认证检测,则开始文件传输,传输的过程是按要求先比对文件的大小、属性、权限、MD5值等信息,如果两端文件信息不一致,则按要求同步文件的区别块。

安装:
wget https://xxx/pub/rsync/rsync-3.1.2.tar.gz、
tar -xzf rsync-3.1.2.tar.gz
cd rsync-3.1.2
./configure --prefix=/usr/local/rsyncd
make && make install

服务器端配置:
mkdir /etc/rsyncd
touch /etc/rsyncd/rsyncd.conf
touch /etc/rsyncd/rsyncd.secrets
touch /etc/rsyncd/rsyncd.motd
chmod 600 /etc/rsyncd/rsyncd.secrets
vim /etc/rsyncd/rsyncd.conf 
#进程 pid 文件所在位置
pid file = /var/run/rsyncd.pid
#指定监听端口,默认是873,可以自己指定
port = 873 
#服务器监听的IP地址,可省略
address = 192.168.1.171 
#守护进程所属的uid,默认是nobody,可能会碰到文件或目录权限问题,此处偷懒用的 root
uid = root
#守护进程的gid 
gid = root 
#chroot,即改变程序执行时所参考的根目录位置,在传输文件之前,服务器守护程序在将chroot 到文件系统中的目录中
#这样做的好处是可能保护系统被安装漏洞侵袭的可能。缺点是需要超级用户权限。另外对符号链接文件,将会排除在外
#也就是说,你在 rsync服务器上,如果有符号链接,你在备份服务器上运行客户端的同步数据时,只会把符号链接名同步下来,并不会同步符号链接的内容
use chroot = yes
#只读选择,只让客户端从服务器上读取文件
read only = no
#只写选择,只让客户端到服务器上写入 
write only = yes 
#允许访问的IP,可以指定单个IP,也可以指定整个网段,能提高安全性。格式是 ip 与 ip 之间、ip 和网段之间、网段和网段之间要用空格隔开;
hosts allow = 192.168.1.0/255.255.255.0 10.0.1.0/255.255.255.0
#客户端最多连接数 
max connections = 5 
#当用户登录时会看到这个信息。比如显示当前时间、公告等
motd file = /etc/rsyncd/rsyncd.motd
#rsync 服务器的日志;
log file = /var/log/rsync.log 
#记录传输文件的日志
transfer logging = yes 
#日志格式
log format = %t %a %m %f %b
#日志级别 
syslog facility = local3 
#通过该选项可以覆盖客户指定的IP超时时间。可以确保rsync服务器不会永远等待一个崩溃的客户端。超时单位为秒钟,0表示没有超时定义,这也是默认值。对于匿名rsync服务器来说,一个理想的数字是600。
timeout = 300 
 
#模块定义
#每个模块都要以[name]形式。这个名字就是在 rsync 客户端看到的名字。
#但是服务器真正同步的数据是通过 path 指定的。可以依次创建多个模块。
#每个模块要指定认证用户、密码文件,但排除并不是必须的。
[ logs ] #模块名,以下配置都属于此模块
#文件目录所在位置
path = /var/log
#当查看服务器上提供了哪些目录时是否列出来,no比较安全 
list = no
#忽略I/O错误 
ignore errors 
#指定由空格或逗号分隔的用户名列表,只有这些用户才允许连接该模块。这里的用户和系统用户没有任何关系,是 rsyncd.secrets 中的用户名!
#如果"auth users"被设置,那么客户端发出对该模块的连接请求以后会被rsync请求challenged进行验证身份。
#这里使用的 challenge/response 认证协议。
#用户的名和密码以明文方式存放在"secrets file"选项指定的文件中。默认情况下无需密码就可以连接模块(也就是匿名方式)。
auth users = zhangzk
secrets file = /etc/rsyncd/rsyncd.secrets #密码文件
exclude = error_log httpd.pid #忽略的文件或目录
comment this is my log #本模块注释,可选
vim /etc/rsyncd/rsyncd.secrets 密码文件,用户名称与密码以冒号分隔,多个用户名和密码可以多行
zhangzk:239fjdalk@893246dasaATDFBSad
zhangab:669fjbalk@8sadf$3246dasaATDFBSaf
vim /etc/rsyncd/rsyncd.motd
Welcome to use rsync
服务器端启动:
1.直接使用 --daemon 参数 
/usr/local/rsync/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
2.xinet方式
    1).修改 /etc/services,加入以下内容,如果已经有可以不加,如果端口改了,需要改掉 873 端口为指定端口
rsync  873/tcp  # rsync 
rsync  873/udp  # rsync
2).修改 /etc/xinetd.d/rsync,主要是要打开rsync這個daemon, 一旦有rsync client要连接時, xinetd会把它转介給 rsyncd(port 873)。
service rsync
{
    disable = no
    socket_type     = stream
    wait            = no
    user            = root
    server          = /usr/bin/rsync
    server_args     = --daemon --config=/etc/rsyncd/rsynd.conf
    log_on_failure  += USERID
}
重启 xinetd
service xinetd restart
客户端配置:
创建密码文件,/etc/rsyncd/rsyncd.pass,并修改为 600 权限
touch /etc/rsyncd/rsyncd.pass
chmod 600 /etc/rsyncd/rsyncd.pass
  /etc/rsyncd/rsyncd.pass 其内密码需要和服务端内/etc/rsyncd/rsyncd.pass指定用户的密码保持一致
239fjdalk@893246dasaATDFBSad  
七、客户端向服务端传输文件,如果是 873 端口,可以把 --port 去掉
rsync -vzrtopg --delete --progress /var/log/access.log zhangzk@192.168.1.100::logs  --password-file=/etc/rsyncd/rsyncd.pass --port=873
  若有需要也可以从服务端拉取文件,需要把服务器的 /etc/rsyncd/rsyncd.conf 中的 write only = yes 给去掉
#拉取整个目录
rsync -vzrtopg --delete --progress  --password-file=/etc/rsyncd/rsyncd.pass --port=873 zhangzk@192.168.1.100::logs  /var/log
 
#拉取单个文件
rsync -vzrtopg --delete --progress  --password-file=/etc/rsyncd/rsyncd.pass --port=873 zhangzk@192.168.1.100::logs/a.log  /var/log
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
12天前
|
Linux 数据安全/隐私保护 Windows
命令方式:window向linux传文件
【10月更文挑战第6天】本文介绍了如何在Linux系统中通过命令`ip a`获取IP地址,并在Windows系统下使用CMD命令行工具和SCP命令实现文件传输。示例展示了如何将D盘中的`mm.jar`文件上传至IP地址为192.168.163.122的Linux系统的/up/目录下,最后在Linux系统中确认文件传输结果。
167 65
|
18天前
|
Linux Shell
Linux系统文件默认权限
Linux系统文件默认权限
34 2
|
10天前
|
Linux 开发工具 数据安全/隐私保护
linux异常一:feng 不在 sudoers 文件中,此事将被报告。yum提示Another app is currently holding the yum lock; waiting for
这篇文章介绍了在CentOS 7系统中安装Docker时遇到的两个常见问题及其解决方法:用户不在sudoers文件中导致权限不足,以及yum被锁定的问题。
24 2
linux异常一:feng 不在 sudoers 文件中,此事将被报告。yum提示Another app is currently holding the yum lock; waiting for
|
6天前
|
Linux Shell 数据库
Linux文件查找新姿势:总有一种你没见过
文件查找是Linux用户提升工作效率的关键技能。本文介绍了几种不常见的文件查找方法,包括使用`find`结合`column`美化输出、利用`locate`和`mlocate`快速查找、编写Shell脚本自动化任务、使用现代工具`fd`以及结合`grep`和`rg`进行内容搜索。此外,还推荐了几款图形界面搜索工具。掌握这些技巧,让你的文件查找更加高效便捷。
32 2
|
13天前
|
Linux C++
Linux c/c++文件的基本操作
在Linux环境下使用C/C++进行文件的基本操作,包括文件的创建、写入、读取、关闭以及文件描述符的定位。
13 0
Linux c/c++文件的基本操作
|
18天前
|
移动开发 Linux
Linux 文件与目录管理
Linux 文件与目录管理
17 3
|
18天前
|
关系型数据库 MySQL Linux
Linux 文件基本属性
Linux 文件基本属性
15 3
|
13天前
|
Linux C++
Linux c/c++文件虚拟内存映射
这篇文章介绍了在Linux环境下,如何使用虚拟内存映射技术来提高文件读写的速度,并通过C/C++代码示例展示了文件映射的整个流程。
31 0
|
13天前
|
Linux C++
Linux c/c++文件移动
这篇文章介绍了在Linux环境下,使用C/C++语言通过命令方式和文件操作方式实现文件移动的方法。
46 0
|
18天前
|
资源调度 JavaScript 前端开发
yarn源|yarn 国内镜像+linux删除大量文件
yarn源|yarn 国内镜像yarn源|yarn 国内镜像+linux删除大量文件
187 0