[Linux常用服务器配置——Rsync服务】

目录

1.Rsync简介

2.rsync的基本特点:

3.基础命令

4.Rsync常用选项:

5.Rsync服务器端安装步骤

01服务器端安装服务

02服务器端修改配置文件

03创建共享目录/common

04创建密码文件

05创建服务器提示信息文件并向该文件中导入欢迎词

06添加防火墙规划

07启动服务

08客户端同步数据


1.Rsync简介

rsync是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。

2.rsync的基本特点:

(1)可以镜像保存整个目录树和文件系统;

(2)可以很容易做到保持原来文件的权限、时间、软硬链接等;

(3)无须特殊权限即可安装;

(4)优化的流程,文件传输效率高;

(5)可以使用rcp、ssh等方式来传输文件,当然也可以通过直接的socket连接;

(6)支持匿名传输。

3.基础命令

查询是否安装服务

# rpm –qa|grep rsnc

创建共享目录

# mkdir common

修改默认配置文件

# vi /etc/rsyncd.conf

创建密码文件

# /etc/rsyncd.secrets

4.Rsync常用选项:

-v 详细模式输出

-a 归档模式,以递归的方式传输文件,并保持所有文件属性

-p 保留文件权限

-P 显示同步的过程及传输时的进度等信息

-t 保持文件时间信息

-g 保持文件属组信息

-o 保持文件属主信息

-D 保持设备文件信息,表示支持b,c,s,p类型的文件

-A 保留acl(访问控制权限)权限

-S 对稀疏文件进行特殊处理以节省DST(目标主机)的空间

-n 现实哪些文件将被传输。

-w 拷贝文件,不进行增量检测

-x 不要跨越文件系统边界

--delete 删除那些DST(目标主机)中SRC(源主机)没有的文件

--progress 显示备份过程

-z 对备份的文件在传输时进行压缩处理

--progress 在传输时现实传输过程

--password-file=FILE 从FILE中得到密码

5.Rsync服务器端安装步骤

01服务器端安装服务

首先查看服务器端是否已安装rsync。

[root@master ~]# rpm -qa|grep rsync
rsync-3.1.2-4.el7.x86_64

可以发现,系统默认安装了NFS服务,如果没有安装,可以运行“yum -y install rsync”命令来安装。 

02服务器端修改配置文件

[root@master ~]# vi /etc/rsyncd.conf

添加以下内容:

# 设置进行数据传输时所使用的账户名称或ID号,默认使用nobody 
uid=rsync
# 设置进行数据传输时所使用的组名称或GID号,默认使用nobody 
gid=rsync
# 设置user chroot为yes后,rsync会首先进行chroot设置,将根映射到path参数路径下,对客户端而言,系统的根就是path参数所指定的路径。但这样做需要root权限,并且在同步符号连接资料时仅会同步名称,而内容将不会同步。
use chroot=no
# 设置服务器所监听网卡接口的IP地址
# address=192.168.125.128
# 设置服务器监听的端口号,默认为873
port=873
# 设置日志文件名称,可以通过log format参数设置日志格式
log file=/var/log/rsyncd.log
# 设置Rsync进程号保存文件名称
pid file=/var/run/rsyncd.pid
# 设置服务器信息提示文件名称,在该文件中编写提示信息
motd file=/etc/rsyncd.motd
# 开启Rsync数据传输日志功能
transfer logging=yes
# 设置锁文件名称
lock file=/var/run/rsync.lock
# 设置并发连接数,0代表无限制。超出并发数后,如果依然有客户端连接请求,则将会收到稍后重试的提示消息
max connections=10
# 模块,rsync通过模块定义同步的目录,模块以[name]的形式定义,可以定义多个模块
[common]
# comment定义注释说明字串
# comment=Web content
# 同步目录的真实路径通过path指定
path=/common/
# 是否允许客户端上传数据
read only=false
# 忽略一些IO错误
ignore errors=true
# exclude可以指定例外的目录,即将common目录下的某个目录设置为不同步数据
# exclude=test/
# 设置允许连接服务器的账户,账户可以是系统中不存在的用户
auth users=tql
# 设置密码验证文件名称,注意该文件的权限要求为只读,建议权限为600,仅在设置auth users参数后有效
secrets file=/etc/rsyncd.secrets 
# 设置允许哪些主机可以同步数据,可以是单个IP,也可以是网段,多个IP与网段之间使用逗号分隔
hosts allow=192.168.125.128,192.168.128.129
#设置拒绝所有(除hosts allow定义的主机外)
hosts deny=*
# 客户端请求显示模块列表时,本模块名称是否显示,默认为true
list=false

03创建共享目录/common

创建共享目录“/common”:

[root@master ~]# mkdir /common
[root@master ~]# cd /common
[root@master common]# touch up{01..100}
[root@master common]# ls
up001  up009  up017  up025  up033  up041  up049  up057  up065  up073  up081  up089  up097
up002  up010  up018  up026  up034  up042  up050  up058  up066  up074  up082  up090  up098
up003  up011  up019  up027  up035  up043  up051  up059  up067  up075  up083  up091  up099
up004  up012  up020  up028  up036  up044  up052  up060  up068  up076  up084  up092  up100
up005  up013  up021  up029  up037  up045  up053  up061  up069  up077  up085  up093
up006  up014  up022  up030  up038  up046  up054  up062  up070  up078  up086  up094
up007  up015  up023  up031  up039  up047  up055  up063  up071  up079  up087  up095
up008  up016  up024  up032  up040  up048  up056  up064  up072  up080  up088  up096
[root@master ~]# useradd rsync -s /sbin/nologin -M
[root@master ~]# tail -1 /etc/passwd
[root@master ~]# chown rsync.rsync /common/
[root@master ~]# ll -d /common/

04创建密码文件

创建密码文件“/etc/rsyncd.secrets”,在该文件中输入两个账户:tql账户的密码是pas123,lxy账户的密码是zb123。需要注意的是,密码文件不可以对所有的人开放可读权限,为了安全,建议设置权限为600。

[root@master ~]# echo "tql:pas123" > /etc/rsyncd.secrets
[root@master ~]# echo "lxy:zb123" >> /etc/rsyncd.secrets
[root@master ~]# chmod 600 /etc/rsyncd.secrets

05创建服务器提示信息文件并向该文件中导入欢迎词

[root@master ~]# echo "welcome to access" >/etc/rsyncd.motd

06添加防火墙规划

添加防火墙规则,允许873端口的数据访问

[root@master ~]# firewall-cmd --permanent --add-port=873/tcp
success
[root@master ~]# systemctl restart firewalld.service
[root@master ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: ssh dhcpv6-client
  ports: 111/tcp 111/udp 2049/tcp 2049/udp 4001-4004/tcp 4001-4004/udp 873/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 	
  rich rules:

07启动服务

由于rsync默认不是开机启动服务,为了实现开机启动rsync服务,可以通过echo将“rsync --daemon”追加至开机启动文件/etc/rc.local。

[root@master ~]# rsync --daemon
[root@master ~]# netstat -lntup |grep 873
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      3265/rsync          
tcp6       0      0 :::873                  :::*                    LISTEN      3265/rsync

若出现上面类似的信息,则rsync启动成功。

[root@master ~]# echo "/usr/bin/rsync --daemon" >> /etc/rc.local

centos7下/etc/rc.local文件里配置的开机启动项,默认是不执行的,那怎么办呢?下面是解决办法:

[root@master ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 12月  4 15:32 /etc/rc.local -> rc.d/rc.local
[root@master ~]# ll /etc/rc.d/rc.local
-rw-r--r--. 1 root root 497 3月  21 03:01 /etc/rc.d/rc.local
[root@master ~]# chmod +x /etc/rc.d/rc.local
[root@master ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 497 3月  21 03:01 /etc/rc.d/rc.local

重启服务,需执行下面两条命令:

[root@master ~]# pkill rsync
[root@master ~]# rsync --daemon

08客户端同步数据

在客户端同样使用rsync命令进行初始化数据传输,使用同样的程序,但客户端主机不需要--daemon选项。

[root@slave ~]# rpm -qa|grep rsync
rsync-3.1.2-4.el7.x86_64
[root@slave ~]# mkdir /test
[root@slave ~]# useradd rsync -s /sbin/nologin -M
[root@slave ~]# tail -1 /etc/passwd
[root@slave ~]# chown rsync.rsync /test/
[root@slave ~]# ll -d /test/
[root@slave ~]# echo "zb123" > /etc/rsyncd.secrets
[root@slave ~]# chmod 600 /etc/rsyncd.secrets

现在可以开始同步数据。

[root@master ~]# rm -f /test/*
[root@master ~]# ls /test/
[root@slave ~]# rsync -avz --progress --password-file=/etc/rsyncd.secrets lxy@192.168.125.128::common/ /test/
welcome to access
 receiving incremental file list
./
up001
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=99/101)
up002
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=98/101)
up100
              0 100%    0.00kB/s    0:00:00 (xfr#100, to-chk=0/101)
 
sent 1,927 bytes  received 4,902 bytes  13,658.00 bytes/sec
total size is 0  speedup is 0.00
[root@slave ~]# ls /test/
up001  up007  up013  up019  up025  up031  up037  up043  up049  up055  up061  up067  up073

[root@slave ~]# rm -f /test/*
[root@slave ~]# ls /test/
[root@slave ~]# rsync -avz --progress --password-file=/etc/rsyncd.secrets rsync://lxy@192.168.125.128/common/ /test/
welcome to access
 
receiving incremental file list
./
up001
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=99/101)
up002
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=98/101)
……
up100
              0 100%    0.00kB/s    0:00:00 (xfr#100, to-chk=0/101)
 
sent 1,927 bytes  received 4,902 bytes  4,552.67 bytes/sec
total size is 0  speedup is 0.00

[root@slave ~]# ls /test/
up001  up007  up013  up019  up025  up031  up037  up043  up049  up055  up061  up067  up073  up079  up085  up091  up097
up002  up008  up014  up020  up026  up032  up038  up044  up050  up056  up062  up068  up074  up080  up086  up092  up098
up003  up009  up015  up021  up027  up033  up039  up045  up051  up057  up063  up069  up075  up081  up087  up093  up099
up004  up010  up016  up022  up028  up034  up040  up046  up052  up058  up064  up070  up076  up082  up088  up094  up100
up005  up011  up017  up023  up029  up035  up041  up047  up053  up059  up065  up071  up077  up083  up089  up095
up006  up012  up018  up024  up030  up036  up042  up048  up054  up060  up066  up072  up078  up084  up090  up096

服务器端添加一个文件:

[root@master ~]# echo "This is test file">/common/test1.txt

客户端再次同步数据:

[root@slave ~]# rsync -avz --progress --password-file=/etc/rsyncd.secrets rsync://lxy@192.168.125.128/common/ /test/
welcome to access
 
receiving incremental file list
./
test1.txt
             18 100%   17.58kB/s    0:00:00 (xfr#1, to-chk=100/102)
 
sent 46 bytes  received 1,384 bytes  2,860.00 bytes/sec
total size is 18  speedup is 0.01
[root@slave ~]# ls /test/
test1.txt  up006  up012  up018  up024  up030  up036  up042  up048  up054  up060  up066  up072  up078  up084  up090  up096
up001      up007  up013  up019  up025  up031  up037  up043  up049  up055  up061  up067  up073  up079  up085  up091  up097

到这里就全部配置完了

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Goat_1 3

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值