您的位置:首页 > 运维架构 > Linux

Linux 的 rsync 远程同步工具

2018-07-30 08:17 639 查看

rsync 简介 :

rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。

它的特性如下:

可以镜像保存整个目录树和文件系统。

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

无须特殊权限即可安装。

快速:第一次同步时 rsync 会复制全部内容,但在下一次只传输修改过的文件。

rsync 在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽。

安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接。

支持匿名传输,以方便进行网站镜象。

备份基本格式 :

rsync [选项] 原始位置 目标位置

rsync -参数 用户名@同步服务器的IP::共享模块名 保存位置

例如:rsync -avzP nemo@192.168.10.1::nemo /backup

参数说明:

-a 归档模式,相当于组合选项-rlptgoD,

-r 是递归 -l 是链接文件,意思是拷贝链接文件;

-p 表示保持文件原有权限;

-t 保持文件原有时间;

-g 保持文件原有用户组;

-o 保持文件原有属主;

-D 相当于块设备文件;

-z 传输时压缩;

-P 传输进度;

-v 传输时的进度等信息,和-P有点关系,自己试试。

-delete 删除目标位置有而原始位置没有的文件。

本案环境 :

主机名系统IP地址
同步源CentOS 7.3192.168.217.128
客户端CentOS 7.3192.168.217.129

配置 rsync 源服务器 :

1.默认Linux 安装 :

[root@localhost ~]# rpm -q rsync
rsync-3.0.9-18.el7.x86_64

2.配置 rsync 配置文件 :

vim /etc/rsyncd.conf

uid = nobody
gid = nobody
use chroot = yes                     #禁锢在家目录
address = 192.168.217.128            #监听地址
port 873                             #监听端口
log file = /var/log/rsyncd.log       #日志文件位置
pid file = /var/run/rsyncd.pid       #pid文件位置
hosts allow = 192.168.217.0/24       #允许访问的客户机地址

[wwwroot]                            #共享模块名称
path = /var/www/html                 #源目录的实际路径
comment = www.kgc.cn                 #描述信息
read only = yes                      #是否为只读   允许上传需注释
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 #同步时不在压缩的文件类型
auth users = backuper                #授权账户                 匿名访问注释
secrets file = /etc/rsyncd_users.db  #存放账户信息的数据文件    匿名访问注释

3.创建授权账户信息 :

vim /etc/rsyncd_users.db
backuper:abc123                 #用户名和密码

chmod 600 /etc/rsyncd_users.db  #只允许root用户读写

mkdir -p /var/www/html    #没有创建 有无视

4.启动 rsync 服务 :

rsync --daemon    rsync#启动rsync

netstat -antp | grep rsync   # 关闭服务使用 kill
tcp        0      0 192.168.217.128:873     0.0.0.0:*     LISTEN      17059/rsync

5.在客户端下载 :

1.在源服务器创建文本 :

echo "123" > /var/www/html/test.txt   #在源服务器写

2.在客户端下载 :

[root@localhost ~]# rsync -avz backuper@192.168.217.128::wwwroot /opt/  #下载到opt
Password:
receiving incremental file list
./
test.txt

sent 83 bytes  received 158 bytes  68.86 bytes/sec
total size is 4  speedup is 0.02

3.免交互 :

vim /etc/server.pass
abc123                      #写入密码

chmod 600 /etc/server.pass

rsync -az  --password-file=/etc/server.pass backuper@192.168.217.128::wwwroot /opt     #免交互

配置 rsync+inotify 实时同步 :

1.调整 inotify 内核参数 :

vim /etc/sysctl.conf

fs.inotify.max_queued_events = 16384   #监控事件列队
fs.inotify.max_user_instances = 1024   #最多监控实例数
fs.inotify.max_user_watches = 1048576  #每个实例最多监控文件数

sysctl -p  #立即生效

2.安装 inotify-tools :

yum install gcc gcc-c++ make -y

tar zxvf inotify-tools-3.14.tar.gz -C /opt/

cd /opt/inotify-tools-3.14

./configure

make && make install

3.监控目录 :

inotifywait -mrq -e modify,create,move,delete /var/www/html/  #监控 /var/www/html/ 目录
# 参数说明 :
-m 持续监控
-r 地柜整个目录
-q 简化输出信息

modify 修改
create 创建
move   移动
delete 删除

#复制会话,重新打开一个远程,链接到装有 inotify-tools
[root@localhost ~]# cd /opt/
[root@localhost opt]# mkdir test
[root@localhost opt]# touch 1.test

#打开监视会话,显示目录下文件变动情况
[root@localhost inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/
/var/www/html/ CREATE,ISDIR test
/var/www/html/ CREATE 1.test

4.编写触发式同步脚本 :

1.在源服务器配置开启上传 :

vim /etc/rsyncd.conf
read only = no    # 改成 no

netstat -antp | grep rsync
tcp        0      0 192.168.217.128:873     0.0.0.0:*       LISTEN      16501/rsync

kill 16501

rsync --daemon


2.编写脚本 :

vim /opt/inotify.sh

#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ backuper@192.168.217.128::wwwroot/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then
$RSYNC_CMD
fi
done

chmod +x /opt/inotify.sh


注意:上传到源服务器时 ,源服务器 html 的属主属组要和配置文件的 uid gid 相同,不然无法上传 。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Linux 远程同