千家信息网

CentOS 6.x 7.x中用rsync远程同步文件

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,CentOS 6.x中用rsync远程同步文件系统环境:Centos 6.9 x64 (Centos 7.3 x64)目的:服务器110.112.200.12中/u01文件夹需要同步复制到110.21
千家信息网最后更新 2026年11月01日CentOS 6.x 7.x中用rsync远程同步文件

CentOS 6.x中用rsync远程同步文件

系统环境:Centos 6.9 x64 (Centos 7.3 x64)


目的:

服务器110.112.200.12中/u01文件夹需要同步复制到110.210.250.58里面进行备份。

将200.12做xinetd 服务器,将其/u01 文件夹复制同步到250.58里面去,250.58做客户端。


一、服务器端的配置

Centos 6.9 x64

在源服务器110.112.200.12中配置

[root@mail test]# yum -y install xinetd rsync

再修改配置: # vi /etc/xinetd.d/rsync

service rsync

{

disable = NO

socket_type = stream

wait = no

user = root

server = /usr/bin/rsync

server_args = --daemon

log_on_failure += USERID

}


把原来的disable ,由YES改成NO


[root@mail test]# vi /etc/rsyncd.conf

uid = root

gid = root

use chroot = no

max connections = 20

secrets file = /etc/rsync_pass

pid file = /var/run/rsyncd.pid

log file = /var/log/rsyncd.log

[backup]

path = /u01

comment = Rsync share test

auth users = ruser

read only = yes

hosts allow = 110.210.250.58

hosts deny = *


注意,path = /u01 ;表示要备份的文件夹为/u01


配置同步的帐号、密码

[root@mail test]# vi /etc/rsync_pass

ruser:123456


[root@mail test]# chown root:root /etc/rsync_pass

[root@mail test]# chmod 600 /etc/rsync_pass

[root@mail test]# chkconfig xinetd on

[root@mail test]# service xinetd restart


检查是否出现873端口

[root@mail test]# netstat -natp

tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 20959/xinetd

注意:如果服务器上装有防火墙记得要打开端口,默认端口是873


另外一种启动、停止方法(不建议使用)

有些人喜欢用命令rsync --daemon --config=/etc/rsyncd.conf 来启动rsync,停用它时要用pkill命令来杀掉进程,不如用xinetd来管理启动rsync方便。

再有些人喜欢写个脚本来启动、停止rsync,系统本身有xinetd来管理启动rsync而不用它,有点舍近求远的味道。


[root@mail test]# rsync --daemon --config=/etc/rsyncd.conf

[root@mail test]# pgrep -l rsync

5132 rsync

[root@mail test]# pkill rsync

[root@mail test]# pgrep -l rsync



------------------------------------------------------------------------------------------

如果你是用Centos 7.3 x64做服务端,请看下面的配置

[root@mail test]# rpm -qa|grep rsync

rsync-3.0.9-17.el7.x86_64


4、配置rsync的配置文件

vi /etc/rsyncd.conf

uid = root

gid = root

use chroot = no

port = 873

max connections = 20

timeout = 200

secrets file = /etc/rsync_pass

pid file = /var/run/rsyncd.pid

log file = /var/log/rsyncd.log

lock file = /var/run/rsyncd.lock

log format = %t %a %m %f %b

[backup]

path = /u01

comment = Rsync share test

list = yes

auth users = ruser

read only = yes

hosts allow = 110.210.250.58

hosts deny = *



[root@mail test]# yum install xinetd.x86_64

[root@mail test]# rpm -qa xinetd

xinetd-2.3.15-13.el7.x86_64


再修改配置: # vi /etc/xinetd.d/rsync

service rsync

{

disable = NO

socket_type = stream

wait = no

user = root

server = /usr/bin/rsync

server_args = --daemon

log_on_failure += USERID

}


把原来的disable ,由YES改成NO


安装完成后,将xinetd服务加入开机自启动:

[root@mail test]# systemctl enable xinetd.service

最后,重新启动服务即可:

[root@mail test]# systemctl restart xinetd


检查是否出现873端口

[root@mail test]# netstat -natp

tcp6 0 0 :::873 :::* LISTEN 25167/xinetd


加入防火墙允许

[root@mail test]# firewall-cmd --add-service=rsyncd --permanent

[root@mail test]# firewall-cmd --reload

[root@mail test]# systemctl restart firewalld



-------------------------------------------------------------------------------------------

二、客户端的配置

在目标服务器250.58中配置

[root@vmevan test]# yum -y install rsync

[root@vmevan test]# vi /etc/rsync_pass

123456

注意,客户端的密码文件只需要密码,而不需要用户名!


[root@vmevan test]# chmod 600 /etc/rsync_pass


建立一个备份用的文件夹

[root@vmevan test]# mkdir /bakcup200.12


在客户端中,有些文件(如named.run)不需要同步,所以要添加到排除列表exclude.list中去。

[root@vmevan test]# vi /etc/exclude.list

named.run


[root@vmevan test]# chmod 600 /etc/exclude.list

[root@vmevan test]# vi /usr/sbin/rsyncdns

#!/bin/bash

# by evan.li 2017.6.21

rsync -vzrtopgu --progress --delete --exclude-from="/etc/exclude.list" --password-file=/etc/rsync_pass ruser@110.112.200.12::backup /bakcup200.12



[root@vmevan test]# chmod +x /usr/sbin/rsyncdns


每1小时同步

[root@vmevan test]# vi /etc/crontab

0 */1 * * * root /usr/sbin/rsyncdns


1、在客户端中,设定异地主机之间的同步

[root@vmevan test]# rsync -vzrtopgu --progress --delete --password-file=/etc/rsync_pass ruser@110.112.200.12::backup /bakcup200.12

或者用

[root@vmevan test]# rsync -zrtopgu --delete --password-file=/etc/rsync_pass ruser@110.112.200.12::backup /bakcup200.12


这个命令行中-vzrtopg里的v是verbose,详细的。

z是压缩传输,

r是recursive,递归。

topg都是保持文件原有属性如属主、时间的参数。

u是只同步已经更新的文件,避免没有更新的文件被重复更新一次,不过要注意两者机器的时钟的同步。

-progress是指显示出详细的进度情况,

-delete是指如果服务器端删除了这一文件,那么客户端也相应把文件删除,保持真正的一致。

后 面的ruser@110.112.200.12::backup中,之后的backup是模块名, 也就是在/etc/rsyncd.conf中自定义的名称, ruser是指定模块中指定的可以同步的用户名。

最后的/bakcup200.12是备份到本地的目录名。

在这里面,还可以用-e ssh的参数建立起加密的连接。

可以用-password-file=/password/path/file来指定密码文件,这样就可以在脚本中使用而无需交互式地输入验证密码了,这里需要注意的是这份密码文件权限属性要设得只有属主可读。


在客户端中,将rsync放入crontab计划任务,每天早上5点同步一次

[root@vmevan test]# vi /etc/crontab

0 5 * * * root /usr/bin/rsync -vzrtopgu --delete --password-file=/etc/rsync_pass ruser@114.112.200.12::backup /bakcup200.12

50 6 * * * root /usr/bin/rsync -avu -progress -delete /u01 /u03/backup



2、在服务端(源服务器中)中, 能建立本地目录之间的同步


[root@vmevan test]# rsync -avu -progress -delete /u01/555 /mnt/sdb1/u01

将源目录/u01/555,同步放到/mnt/sdb1/u01文件夹下。对src-dir目录内容向dst-dir目录下进行差异更新,有增加/更新则添加替换,有减少则对其删减 。




测试完成于2017.06.21

by evan.li


文件 同步 服务 配置 服务器 客户 密码 客户端 文件夹 更新 备份 目录 端口 命令 端的 之间 参数 属性 模块 用户 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 小米移动互联网科技园 云之行互联网科技 科技平台互联网 疫情对互联网科技公司影响 互联网科技金融 宜安科技 互联网金融 长亮科技互联网金融 多迪互联网科技有限公司 互联网科技有限公司 翻译 恒银科技与互联网金融 福州新华互联网科技学校 知乎 互联网系的金融科技公司 武汉健翔互联网络科技有限公司 阿大互联网科技有限公司 广州富喜互联网络科技 工业互联网科技创业 互联网科技企业商标注意事项 互联网科技收购价值 瑞安市蟠桃会互联网科技有限公司 互联网科技快速发展优美句子 百单网互联网科技有限公司 腾讯阿里互联网科技 合肥互联网科技公司 互联网科技ppt模板 农村开互联网科技讲堂 深圳前海聚择互联网科技有限公司 深圳国际航运科技和互联网 领航科技大厦互联网公司 抖集互联网科技技术咨询 广州智享互联网科技
0