阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Rsync服务器的安装与配置详解

157次阅读
没有评论

共计 8871 个字符,预计需要花费 23 分钟才能阅读完成。

一、Rsync 简介
1.1 什么是 Rsync

Rsync 是一款快速的,开源的,多功能的,可以实现全量和增量的远程和本地的数据同步和数据备份的工具。

全量的概念是:全部备份。

增量的概念是:差异化备份。对上一次基础上,对更新的部分作备份。
1.1.2    Rsync 简介

  Rsync 具有可以使本地和远程的两台主机之间的数据快速同步镜像远程备份的功能,这个功能类似 ssh 带 scp 的命令,但是有优于 scp 的功能,scp 每次都是全量拷贝,而 rsync 是增量拷贝。
  Rsync 还可以在本地主机的不同文件或者目录之间全量和增量的复制,类似于 cp 命令,cp 命令是全量拷贝,而 rsync 是增量拷贝。
  Rsync 还可以实现删除文件和目录的功能,相当于 rm 命令
  一个 rsync 相当于 scp,cp,rm 命令,并且还忧于他们每个命令,因为 rsync 具有增量备份的功能。

1.1.3  rsync 的特性:
  1)支持拷贝特殊文件如链接文件,设备等
  2)可以有排除指定文件或者目录同步的功能,相当于打包命令 tar 的排除功能。
  3)可以做到保持原文件或者目录的权限,时间,软硬链接,属组,主等所有属性均不改变
  4)可以实现增量备份,既只同步发生变化的数据
  5)可以勇士 rcp,rsh,ssh 等方式来配合传输文件
  6)可以通过 socket 传输文件和数据
  7)支持匿名的认证模式传输
1.1.4  rsync 三种工作方式
1)本地模式,相当于 cp 和 rm 命令
[root@rsync tmp]# rsync /etc/passwd /tmp/    ## 相当于 cp 的命令
[root@rsync tmp]# ls
passwd
 
[root@rsync mnt]# rsync -avz –delete passwd  /mnt/  ##–delete 相当于删除的功能
[root@rsync mnt]# ls
passwd
 
–delete 的作用是删除的功能,本地有什么,远端就有什么。比如本地有 passwd 的内容,不管 /mnt 目录下面有什么,都只有 passwd 的内容,所有谨慎用 –delete

2)通道模式,一般配合 ssh key 免秘钥使用,结合定时任务
[root@rsync mnt]# rsync -avz -e  ‘ssh -p 22’ /etc/passwd root@10.0.0.31:/tmp/ 
root@10.0.0.31’s password: 
sending incremental file list
passwd
[root@nfs tmp]# ls ## 在远端查看
passwd

3)daemon 模式
 
1.1.5 rsync 的参数说明
-v:详细输出
-z:传输时进行压缩以提高传输效率。
-a:归档模式,表示以递归的方式传输文件,并保持文件的属性
–exclude:排除不需要同步传输的文件或者目录
–delete:让目标目录和源目录的数据一致
–bwlimit:限制带宽,默认单位是:kb(案例:某 DBA 做数据同步,导致用户无法访问网站)

二、Rsync 服务器的安装
2.1 安装准备

2.1.1 查看 rsync 的版本号
[root@rsync ~]# rsync –version
rsync  version 3.0.6  protocol version 30
Copyright (C) 1996-2009 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:
    64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
    socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
    append, ACLs, xattrs, iconv, symtimes
rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you
are welcome to redistribute it under certain conditions.  See the GNU
General Public Licence for details.

2.1.2 查看服务器的内核,版本信息
[root@rsync ~]# cat /etc/RedHat-release 
CentOS release 6.7 (Final)
[root@rsync ~]# uname -r
2.6.32-573.el6.x86_64
[root@rsync ~]# uname -m
x86_64

2.2 主要讲一下通过 daemon 实现数据同步案例
/etc/rsyncd.conf 是 rsync 的默认配置文件,该配置文件不存在,需要编辑内容

[root@linuxidc ~]# cat /etc/rsyncd.conf 
#rsync_config_____________________________start
#created by linuxidc 15:01 2007-6-5
##rsyncd.conf start##
uid = rsync  ## 进程对应的用户,是虚拟用户。远端的命令使用 rsync 访问共享目录
gid = rsync  ## 进程对应的用户组。
use chroot = no    ## 安全相关
max connections = 200      ## 最大连接数
timeout = 300    ## 超时时间
pid file = /var/run/rsyncd.pid      ## 进程对应的进程号文件
lock file = /var/run/rsyncd.lock    ## 锁文件
log file = /var/log/rsyncd.log        ## 日志文件
[backup]    ### 模块名称
path = /backup      ### 服务器提供访问的目录
ignore errors      ## 忽略错误
read only = false    ## 可写
list = false      ## 不能列表
hosts allow = 172.16.1.0/24  ## 允许的 ip 地址
##hosts deny = 0.0.0.0/32
auth users = rsync_backup    ## 虚拟用户
secrets file = /etc/rsync.password    ### 虚拟密码
#rsync_config________________________end

其中 rsync 用户默认是不存在的,需要创建用户
[root@rsync ~]# useradd rsync -s /sbin/nologin -M
[root@rsync ~]# cat  /etc/passwd|grep rsync
rsync:x:501:501::/home/rsync:/sbin/nologin
[root@rsync ~]# id rsync
uid=501(rsync) gid=501(rsync) 组 =501(rsync)
 
为什么用虚拟用户?
应答:文件和进程都要满足属主的要求,文件和进程的存在一定是需要用户的,也是为了安全问题。

创建 /backup 目录,并且属主和属组都属于 rsync
[root@rsync ~]# mkdir /backup/ -p
[root@rsync ~]# chown -R rsync.rsync /backup/
[root@rsync ~]# ls -ld /backup/
drwxr-xr-x 2 rsync rsync 4096 12 月  9 2016 /backup/

创建配置文件 /etc/rsync.password,默认不存在这个配置文件
[root@rsync ~]# cat /etc/rsync.password 
rsync_backup:linuxidc
[root@rsync ~]#chmod 600 /etc/rsync.password
[root@rsync ~]# ls -l /etc/rsync.password 
-rw——-. 1 root root 20 11 月 29 01:14 /etc/rsync.password

启动服务:
[root@rsync ~]# rsync –daemon 
[root@rsync ~]#ps -ef|grep rsync|grep -v grep ## 查看进程有没有启动
root      3046      1  0 15:19 ?        00:00:00

加入开机自启动

[root@rsync ~]# tail -1 /etc/rc.local 
/usr/bin/rsync –daemon

三、Rsync 客户端的安装

编辑配置文件 /etc/rsync.passwd,该配置文件默认不存在
vim  /etc/rsync.passwd
[root@linuxidc backup]# cat /etc/rsync.password 
linuxidc
chmod 600 /etc/rsync.passwd

创建 backup 目录
mkdir -p /backup
cd /backup
touch stu{01,100}

客户端推送:
方法 1:
[root@linuxidc backup]# rsync -avz /backup/ rsync_backup@172.16.1.41::backup/ –password-file=/etc/rsync.password 
方法 2:
[root@linuxidc backup]# rsync -avz /backup/ rsync://rsync_backup@172.16.1.41/backup/ –password-file=/etc/rsync.password

从客户端把服务端的东西拉回来的方案
服务端:
[root@linuxidc backup]# touch 1 234
[root@linuxidc backup]# ls
1  234

客户端:
[root@linuxidc ming]# rsync -avz  rsync_backup@172.16.1.41::backup/ /ming/  –password-file=/etc/rsync.password 
receiving incremental file list
./
1
234
 
sent 105 bytes  received 204 bytes  618.00 bytes/sec
total size is 0  speedup is 0.00
[root@linuxidc ming]# ls
1  234

四、Rsync 多模块实战
1.1.1 多模块实战
实例 1:
环境:
[root@linuxidc ~]# cat /etc/rsyncd.conf
#rsync_config_____________________________start
#created by linuxidc 15:01 2007-6-5
##rsyncd.conf start##
uid = rsync 
gid = rsync 
use chroot = no   
max connections = 200     
timeout = 300   
pid file = /var/run/rsyncd.pid     
lock file = /var/run/rsyncd.lock     
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password       
[backup]     
path = /backup     
[chen]
path = /chen
#rsync_config________________________end

服务器端:
[root@linuxidc ~]# mkdir /chen
[root@linuxidc ~]# ls -ld /chen/
drwxr-xr-x 2 rsync rsync 4096 12 月  2 18:58 /chen/
客户端
[root@linuxidc ~]# ls -ld /ming
drwxr-xr-x 2 root root 4096 12 月  2 18:26 /ming
[root@linuxidc ~]# rsync -avz /ming/ rsync_backup@172.16.1.41::chen/ –password-file=/etc/rsync.password 
sending incremental file list
./
ming1
ming10
ming2
ming3
ming4
ming5
ming6
ming7
ming8
ming9
 
sent 463 bytes  received 201 bytes  1328.00 bytes/sec
total size is 0  speedup is 0.00

服务端查看效果:
[root@linuxidc chen]# ls
ming1  ming10  ming2  ming3  ming4  ming5  ming6  ming7  ming8  ming9

实例 2:
环境:
[root@linuxidc chen]# cat /etc/rsyncd.conf
#rsync_config_____________________________start
#created by linuxidc 15:01 2007-6-5
##rsyncd.conf start##
uid = rsync 
gid = rsync 
use chroot = no   
max connections = 200     
timeout = 300   
pid file = /var/run/rsyncd.pid     
lock file = /var/run/rsyncd.lock     
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password       
[backup]     
path = /backup     
[chen]
path = /chen
[luo]
path = /luo
ignore errors
read only = false
list = false

hosts allow = 172.16.1.0/24
hosts deny = 0.0.0.0/32
auth users = tang
secrets file = /etc/tang
#rsync_config________________________end
[root@linuxidc chen]# mkdir /luo
[root@linuxidc chen]# chown rsync.rsync /luo
[root@linuxidc chen]# ls -ld /luo/
drwxr-xr-x 2 rsync rsync 4096 12 月  2 19:18 /luo/
[root@linuxidc chen]# cat /etc/tang 
tang:tangguo
[root@linuxidc luo]# ls /etc/tang  -ld
-rw——- 1 root root 13 12 月  2 19:34 /etc/tang
权限一定要是 600

客户端配置:
[root@linuxidc ming]# cat /etc/tang 
tangguo
[root@linuxidc ming]# ls /etc/tang -ld
-rw——- 1 root root 8 12 月  2 19:35 /etc/tang
客户端权限也一定要是 600
[root@linuxidc ming]# rsync -avz /ming/ tang@172.16.1.41::luo/  –password-file=/etc/tang 
sending incremental file list
./
ming1
ming10
ming2
ming3
ming4
ming5
ming6
ming7
ming8
ming9
 
sent 463 bytes  received 201 bytes  1328.00 bytes/sec
total size is 0  speedup is 0.00

五、Rsync 案例排错
5.1 案例 1

[root@linuxidc ming]# rsync -avz /ming/ tang@172.16.1.41::luo/  –password-file=/etc/tang 
@ERROR: auth failed on module luo
rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
[root@linuxidc luo]# tail -3 /var/log/rsyncd.log 
2016/12/02 19:46:18 [3601] secrets file must not be other-accessible (see strict modes option)
2016/12/02 19:46:18 [3601] continuing without secrets file
2016/12/02 19:46:18 [3601] auth failed on module luo from unknown (172.16.1.31): missing secret for user “tang”
 
报错的原因是服务器端的 /etc/tang 的权限问题没有设置为 600,我们查看一下。
[root@linuxidc luo]# ls -ld /etc/tang 
-rwxr-xr-x 1 root root 13 12 月  2 19:34 /etc/tang
权限改为 600 就可以了

5.2 案例 2
[root@linuxidc ~]# rsync -avz /ming/ tang@172.16.1.41::luo/  –password-file=/etc/tang 
@ERROR: auth failed on module luo
rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
查看日志
[root@linuxidc luo]# tail -3 /var/log/rsyncd.log 
2016/12/02 19:52:12 [3614] name lookup failed for 172.16.1.31: Name or service not known
2016/12/02 19:52:12 [3614] connect from UNKNOWN (172.16.1.31)
2016/12/02 19:52:12 [3614] auth failed on module luo from unknown (172.16.1.31): password mismatch
password mismatch,密码错误,客户端和服务器端的密码不一致导致的问题。
【注意】有的客户端和服务器端密码看起来一样,实际里面有空格,也能报错,注意一下

5.3 案例 3
[root@linuxidc ~]# rsync -avz /backup/ rsync://rsync_backup@172.16.1.41/backup/ –password-file=/etc/rsync.password 
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
原因:服务端的 backup 目录不存在

5.4  案例 4
[root@linuxidc ~]# rsync -avz /backup/ rsync://rsync_backup@172.16.1.41/backup/ –password-file=/etc/rsync.password 
sending incremental file list
./
rsync: failed to set times on “.” (in backup): Operation not permitted (1)
1
 
sent 4325 bytes  received 1911 bytes  12472.00 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]
原因:服务端 backup 的属组和属主问题

RSync 实现文件备份同步详解   http://www.linuxidc.com/Linux/2014-09/106967.htm

利用 inotifywait 监控主机文件和目录 http://www.linuxidc.com/Linux/2013-03/81075.htm

利用 inotify+rsync 实现 Linux 文件批量更新 http://www.linuxidc.com/Linux/2012-01/52132.htm

inotify-tools+rsync 实时同步文件安装和配置 http://www.linuxidc.com/Linux/2012-06/63624.htm

rsync 同步完整配置 http://www.linuxidc.com/Linux/2013-06/85781.htm

CentOS 6.5 下 Rsync 远程同步 http://www.linuxidc.com/Linux/2014-05/101084.htm

Linux 文件夹对比并提取的差分文件技巧 -rsync 的妙用 http://www.linuxidc.com/Linux/2016-02/128307.htm

Rsync 的详细介绍:请点这里
Rsync 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-01/139888.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计8871字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中