1.Rsync 基本概述
Rsync 是开源多功能同步工具,支持多种操作系统
Rsync 支持本地复制(优于 scp,cp)与远程同步
Rsync 支持全量备份,增量备份
Rsync 基于 C / S 架构,默认监听 tcp873 端口

2.Rsync 优点
支持增量备份,第一次全量备份,第二次增量备份。边复制边比较边统计,传输效率高。
数据集中备份,客户端可以推送数据至服务端,也可以从服务端获取数据,与客户端为参照物。
保持文件属性,符号链接,硬链接,权限,时间等。
安全方式传输,Rsync 本身不对数据加密,使用 ssh 作为传输端口。
指定排除文件,排除无需同步的文件或目录。
进程方式同步,rsync 运行在 C / S 架构,通过进程方式传输文件或数据。

. 缺点:
1. 大量小文件同步会比较慢,需要比对时间较长,可能造成 Rsync 进程停止
解决思路:将小文件进行打包,然后再同步,减小比对时间,传输效率更高
2. 同步大文件会出现中断情况,而且长时间同步会造成网络资源耗尽
解决思路:配置限速同步,未同步完之前修改为隐藏文件,同步完后修改为正常文件

Rsync 命令格式:
rsync [选项] 源文件 [user@]host:: 目录
rsync [选项] 源文件 rsync:// [user@]host:[:port]/ 目录
- a 选项
:递归传输目录
:保持文件或目录的时间、文件属性、属组、时间、软链接
:显示同步的过程
:设备
- z 选项
:传输时进行压缩以提高效率
- v 选项
:详细模式输出,打印速率,文件数量等
- e 选项
:使用的信道协议,指定替代 rsh 的 shell 程序
–delete 让目标目录和源目录数据保持一致
–partial 断点续传
–bwllimit=100 限速传输
–exclude-from=file 文件名所在的目录
–exclude=PATTERN 指定排除不需要传输的文件模式

rsync 服务部署
环境:
两台主机:源服务器:192.168.56.11
目标服务器:192.68.56.12
需求:
把源服务器上的 /etc 目录实时同步到目标服务器的 /tmp/ 目录下

在目标服务器上做以下操作:

1. 关闭防火墙和 selinux
[root@linuxidc ~]# systemctl stop firewalld
[root@linuxidc ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.Fedoraproject.FirewallD1.service.
[root@linuxidc ~]# setenforce 0
[root@linuxidc ~]# sed -ri ‘s/^(SELINUX=).*/\1disabled/g’ /etc/sysconfig/selinux

2. 安装 rsync 服务端软件
[root@linuxidc ~]# yum install rsync -y

3. 配置 rsync.conf 配置文件

[root@linuxidc ~]# cat >> /etc/rsyncd.conf <<EOF

    log file = /var/log/rsyncd.log // 日志文件位置,启动 rsync 后自动产生
    pidfile = /var/run/rsyncd.pid //pid 文件存放位置
    lock file = /var/run/rsync.lock // 支持 max connections 参数的锁文件
    secrets file = /etc/rsync.pass // 用户认证配置文件,里面保存用户名称和密码,必须手动创建这个文件
    [etc_from_client] // 自定义同步名称
    path = /heyuanjie/ //rsync 服务端数据存放路径
,客户端的数据将同步至此目录
    comment = gaosiao
    uid = root // 设置 rsync 运行权限为 root
    gid = root // 设置 rsync 运行权限为 root
    port = 873 // 默认端口
    ignore errors // 表示出现错误忽略错误
    use chroot = no // 默认为 true,修改为 no,增加对目录文件软连接的备份
    read only = no // 设置 rsync 服务端为读写权限
    list = no // 不显示 rsync 服务端资源列表
    max connection= 200 // 最大连接数
    timeout = 600 // 设置超时时间
    auth users = admin // 执行数据同步的用户名,可设置多个,用英文状态下逗号隔开
    hosts allow = 192.168.56.11 // 允许进行数据同步的客户端 ip,可设置多个,逗号隔开
    hosts deny = 192.168.1.1 // 禁止数据同步的客户端 IP 地址
    EOF

4. 创建用户认证文件,并设置文件权限

[root@linuxidc ~]# echo ‘admin:123456’ > /etc/rsync.pass
[root@linuxidc ~]# cat /etc/rsync.pass
admin:123456
[root@linuxidc ~]# chmod 600 /etc/rsync
[root@linuxidc ~]# ll /etc/rsync
-rw——-. 1 root root 825 Aug 16 15:42 /etc/rsyncd.conf
-rw——-. 1 root root 13 Aug 16 15:55 /etc/rsync.pass

5. 启动 rsync 服务并加入开机自启

[root@linuxidc ~]# systemctl start rsyncd
[root@linuxidc ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi
user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
[root@linuxidc ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :22 :
LISTEN 0 100 127.0.0.1:25 :
LISTEN 0 5 :873 :
LISTEN 0 128 :::22 :::
LISTEN 0 100 ::1:25 :::
LISTEN 0 5 :::873 :::*

在源服务器上做以下操作:
1. 关闭防火墙和 selinux
[root@hejie ~]# systemctl stop firewalld
[root@hejie ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@hejie ~]# setenforce 0
[root@hejie ~]# sed -ri ‘s/^(SELINUX=).*/\1disabled/g’ /etc/sysconfig/selinux

2. 配置 yum 源

[root@hejie yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@hejie yum.repos.d]# sed -i ‘s/\$releasever/7/g’ /etc/yum.repos.d/CentOS7-Base-163.repo
[root@hejie yum.repos.d]# sed -i ‘s/^enabled=.*/enabled=1/g’ /etc/yum.repos.d/CentOS7-Base-163.repo
[root@hejie ~]# yum -y install epel-release

3. 安装 rsync 服务端软件,只需安装,不要启动,不需要配置
[root@hejie ~]# yum -y install rsync

4. 创建认证密码文件,并设置权限,设置文件所有者具有读取、写入权限即可
[root@hejie ~]# echo ‘123456’ > /etc/rsync.pass
[root@hejie ~]# cat /etc/rsync.pass
123456
[root@hejie ~]# chmod 600 /etc/rsync.pass
[root@hejie ~]# ll /etc/rsync.pass

-rw——-. 1 root root 7 Aug 16 16:44 /etc/rsync.pass

5. 在源服务器上创建测试目录,并运行以下命令

[root@hejie ~]# mkdir -pv /root/etc/test
mkdir: created directory‘/root/etc’
mkdir: created directory‘/root/etc/test’
[root@hejie ~]# rsync -avH –port 873 –progress –delete /root/etc/ admin@192.168.56.12::etc_from_client –password-file=/etc/rsync.pass

6. 运行完成后,在目标服务器上查看,在 /heyuanjie 目录下有 test 目录,说明数据同步成功

[root@linuxidc ~]# cd /heyuanjie
[root@linuxidc heyuanjie]# ls
test

7. 安装 inotify-tools 工具,实时触发 rsync 进行同步
  // 查看服务器内核是否支持 inotify

[root@hejie ~]# ll /proc/sys/fs/inotify/
total 0
-rw-r–r–. 1 root root 0 Aug 16 16:57 max_queued_events
-rw-r–r–. 1 root root 0 Aug 16 16:57 max_user_instances
-rw-r–r–. 1 root root 0 Aug 16 16:57 max_user_watches
如果有以上三个 max 开头的文件则表示服务器内核支持 inotify
// 安装 inotify-tools
[root@hejie ~]# yum -y install make gcc gcc-c++
[root@hejie ~]# yum -y install inotify-tools

// 写同步脚本,让脚本自动去检测我们制定的目录下文件发生的变化,然后再执行 rsync 的命令把它同步到我们的服务器端去
[root@hejie ~]# mkdir /scripts
[root@hejie ~]# touch /scripts/inotify.sh
[root@hejie ~]# chmod 755 /scripts/inotify.sh
[root@hejie ~]# vi /scripts/inotify.sh

host=192.168.56.12 // 目标服务器的 ip(备份服务器)
src=/etc // 在源服务器上所要监控的备份目录(可自定义,但必须存在)
des=etc_from_client // 自定义模块名,需要与目标服务器上定义的同步名称一致
password=/etc/rsync.pass // 执行数据同步的密码文件
user=admin // 执行数据同步的用户名
inotifywait=/usr/bin/inotifywait

$inotifywait -mrq –timefmt ‘%Y%m%d %H:%M’ –format ‘%T %w%f%e’ -e modify,delete,create,attrib $src \
| while read files ; do
rsync -avzP –delete –timeout=100 –password-file=${password} $src $user@$host::$des
echo “${files} was rsynced” >>/tmp/rsync.log 2>&1
done

// 启动脚本
[root@linuxidc ~]# nohup bash /scripts/inotify.sh &
[1] 8784
[root@linuxidc ~]# nohup: ignoring input and appending output to‘nohup.out’
[root@linuxidc ~]# ps -ef|grep inotify
root 8784 8528 0 00:46 pts/0 00:00:00 bash /scripts/inotify.sh
root 8785 8784 0 00:46 pts/0 00:00:00 /usr/bin/inotifywait -mrq –timefmt %Y%m%d %H:%M –format %T %w%f%e -e modify,delete,create,attrib /etc
root 8786 8784 0 00:46 pts/0 00:00:00 bash /scripts/inotify.sh
root 8788 8528 0 00:47 pts/0 00:00:00 grep –color=auto inotify

// 在源服务器上生成一个新文件
[root@linuxidc ~]# mkdir /etc/aaa
会触发同步到目标服务器上的 /heyuanjie/etc 中
[root@linuxidc ~]# cd /heyuanjie/
[root@linuxidc heyuanjie]# ls
etc test
[root@linuxidc heyuanjie]# cd etc/
[root@linuxidc etc]# ls
aaa
// 查看 inotify 生成的日志,可以看到生成了一个 aaa 目录文件
[root@linuxidc ~]# tail /tmp/rsync.log
20180817 00:49 /etc/aaaCREATE,ISDIR was rsynced

设置脚本开机自启动:
[root@linuxidc ~]# chmod +x /etc/rc.d/rc.local
[root@linuxidc ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 473 Aug 5 2017 /etc/rc.d/rc.local
[root@linuxidc ~]# echo ‘nohup /bin/bash /scripts/inotify.sh’ >> /etc/rc.d/rc.local
[root@linuxidc ~]# tail -1 /etc/rc.d/rc.local

nohup /bin/bash /scripts/inotify.sh

更多 rsync 相关教程见以下内容:

CentOS 6.5 rsync+inotify 实现数据实时同步备份 https://www.linuxidc.com/Linux/2016-11/137655.htm
rsync+inotify 实现数据的实时同步 https://www.linuxidc.com/Linux/2017-01/139778.htm
rsync+inotify 实现服务器之间文件实时同步详解  https://www.linuxidc.com/Linux/2016-11/137659.htm
Rsync 结合 Inotify 实时同步配置  https://www.linuxidc.com/Linux/2017-02/140877.htm
RSync 实现数据���份  https://www.linuxidc.com/Linux/2017-06/144913.htm
inotify+rsync 实现数据实时同步  https://www.linuxidc.com/Linux/2017-10/147901.htm
rsync+inotify 实现数据的实时备份  https://www.linuxidc.com/Linux/2016-11/137630.htm
rsync+inotify 实现数据自动同步  https://www.linuxidc.com/Linux/2017-03/141717.htm
使用 rsync 实现数据实时同步备份  https://www.linuxidc.com/Linux/2017-05/143462.htm
unison+inotify 实现数据实时双向同步  https://www.linuxidc.com/Linux/2018-01/150468.htm