1 前言

1.1 概述

本文介绍使用rsync和 inotify-tools,实现linux 上的本地实时同步和本地定时同步的方法。

1.2 实验环境

服务器两台
操作系统: CentOS-7.4

软件:
rsync.x86_64 3.0.9-18.el7
inotify-tools.x86_64 3.14-9.el7
xinetd.x86_64 2:2.3.15-13.el7

本地同步环境信息:
源目录: /root/data/
目的目录:/root/backup/

远程同步环境信息:
源主机(即原始文件所在的服务器)的ip地址:10.40.239.234
目标主机(存放备份文件的服务器)的ip地址:10.40.239.236
源目录: /root/data/
目的目录:/root/backup/

1.3 软件介绍

1.3.1 rsync

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

  1. 可以镜像保存整个目录树和文件系统。
  2. 可以很容易做到保持原来文件的权限、时间、软硬链接等等。
  3. 无须特殊权限即可安装。
  4. 快速:第一次同步时 rsync 会复制全部内容,但在下一次只传输修改过的文件。rsync 在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽。
  5. 安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接。
    支持匿名传输,以方便进行网站镜像。

1.3.2 inotify-tools

inotify-tools 是为linux下inotify文件监控工具提供的一套c的开发接口库函数,同时还提供了一系列的命令行工具,这些工具可以用来监控文件系统的事件。 inotify-tools是用c编写的,除了要求内核支持inotify外,不依赖于其他。inotify-tools提供两种工具,一是inotifywait,它是用来监控文件或目录的变化,二是inotifywatch,它是用来统计文件系统访问的次数。

2 实时同步

实时同步是使用 rsync和inotify-tools来完成的。实时同步方式中,服务器主动将文件差异部分发送给客户端。

2.1 本地实时同步

  1. 下载并安装inotify-tools,有两种方法:
    (1) 在https://sourceforge.net/projects/inotify-tools/ 下载inotify。
    解压下载后的文件,本文中使用的是inotify-tools-3.14.tar.gz。

tar -zxvf inotify-tools-3.14.tar.gz

   进入解压后的目录,编译和安装inotify-tools:

cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify make &&
make install

(2) 使用yum 安装。

yum –y inotify-tools

查看 inotify 的安装位置是 /usr/local/inotify。两个命令inotifywait 和 inotifywatch位于 目录 /usr/local/inotify/bin/ 中。

  1. 安装 rsync

yum install rsync

  1. 配置 rsync 参数文件 /etc/rsyncd.conf,内容如下:

    # /etc/rsyncd: configuration file for rsync daemon mode
    # See rsyncd.conf man page for more options.
    # configuration example:

    uid = nobody
    gid = nobody
    use chroot = no
    max connections = 200
    timeout = 300
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsync.lock
    log file = /var/log/rsyncd.log
    read only = false
    list = false
    dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

  2. 启动 rsync:

systemctl start rsync

  1. 修改inotify默认参数(inotify默认内核参数值太小)
    查看系统默认参数值:

sysctl -a | grep max_queued_events

结果是:fs.inotify.max_queued_events = 16384

sysctl -a | grep max_user_watches

结果是:fs.epoll.max_user_watches = 378327

sysctl -a | grep max_user_instances

结果是:fs.inotify.max_user_instances = 128

修改参数:

sysctl -w fs.inotify.max_queued_events=“99999999”
sysctl -w fs.inotify.max_user_watches=“99999999”
sysctl -w fs.inotify.max_user_instances=“65535”

参数说明:
max_queued_events:
inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确
max_user_watches:
要同步的文件包含多少目录,可以用:find /Data/xqsj_upload -type d | wc -l 统计这些源目录下的目录数,必须保证max_user_watches值大于统计结果(这里/Data/xqsj_upload为同步的源文件目录)
max_user_instances:
每个用户创建inotify实例最大值。

  1. 创建实时同步脚本rsync_inotify.sh,内容如下:
#!/bin/bash
src_dir=/root/data
dest_dir=/root/backupexport PATH=/usr/local/inotify/bin/:$PATH
inotifywait -mrq --timefmt '%Y/%m/%d %H:%M:%S' --format '%T %w%f %e' -e create,delete,close_write,attrib,move $src_dir | while read line
do# $line, for exiample:# 2020/01/27 16:57:14 /root/data/a.txt CREATEfile_path=$(echo $line | awk '{print $3}')# If file exists, that means, the event is "create", "close_write", "attrib" or "move_to"if [ -f $file_path ] || [ -d $file_path ]then# rsync the file,  ${file_path#$src_dir/} is the relative path of the file/usr/bin/rsync -avz --port=873 --delete $file_path $dest_dir/${file_path#$src_dir/}echo "$(date +%F%T): $file_path is rsynced" >> /tmp/rsync.log 2>&1else# IF file does not exist, that means, the event is "delete" or "move_from"# rsync the directory of the filefile_path_parent=$(dirname $file_path)/usr/bin/rsync -avz --port=873 --delete $file_path_parent $dest_dir/${file_path_parent#$src_dir/}echo "$(date +%F%T): $file_path_parent is rsynced" >> /tmp/rsync.log 2>&1fi
done

本实验中,这个文件的路径是/root/rsync_inotify.sh

chmod 744 rsync_inotify.sh

上面的脚本中,

inotifywait -mrq --timefmt ‘%Y/%m/%d %H:%M:%S’ --format ‘%T %w%f %e’ -e create,delete,close_write,attrib,move $src_dir

这条命令的作用是实时监控目录及其内容的变更,并将变更的时间,变更的文件和目录的路径以及变更的事件类型打印出来。下文会讲解命令中的参数的含义。
打印的信息示例如下:
2020/01/27 16:57:14 /root/data/a.txt CLOSE_WRITE
表示结束了/root/data/a.txt 的写

| while read line do
  file_path=$(echo $line | awk ‘{print $3}’)
  if [ -f $file_path ] || [ -d $file_path ]
  then
   /usr/bin/rsync -avz --port=873 --delete $file_path $dest_dir/${file_path#$src_dir/}
  echo “$(date +%F%T): $file_path is rsynced” >> /tmp/rsync.log 2>&1
  else
    file_path_parent=$(dirname $file_path)
    /usr/bin/rsync -avz --port=873 --delete $file_path_parent $dest_dir/${file_path_parent#$src_dir/}
    echo “$(date +%F%T): $file_path_parent is rsynced” >> /tmp/rsync.log 2>&1
  fi
done

则根据 inotify 打印的信息,得到发生变更的文件和目录,并rsync它们。如果其中的某个文件或目录存在,则说明发生在其上的事件是"create", “close_write”, “attrib” 或者 “move_to”(移入),程序会同步该文件。如果某个文件或目录不存在,则说明发生在其上的事件是"delete" or “move_from”(移出),则程序会同步该文件的上层目录。

inotifywait 参数说明

参数名称 参数说明
-m,–monitor 始终保持事件监听状态
-r,–recursive 递归查询目录
-q,–quiet 只打印监控事件的信息
-excludei 排除文件或目录时,不区分大小写
-t,–timeout 超时时间
–timefmt 指定时间输出格式
–format 指定时间输出格式
-e,–event 后面指定删、增、改等事件

inotifywait events事件说明

事件名称 事件说明
access 读取文件或目录内容
modify 修改文件或目录内容
attrib 文件或目录的属性改变
close_write 修改真实文件内容
close_nowrite
close
open 文件或目录被打开
moved_to 文件或目录移动到
moved_from 文件或目录从移动
move 移动文件或目录移动到监视目录
create 在监视目录下创建文件或目录
delete 删除监视目录下的文件或目录
delete_self
unmount 卸载文件系统

我们只需要监控的create,delete,close_write,attrib,move 这些参数,他们都和文件的变更有关。modify表示文件或目录内容的修改,它只有在保存后才有意义。如果修改被保存,则事件close_write会产生,因此不需要监控事件modify。 事件moved_to和move_from 属于move的子类型,也不需要监控。

  1. 在后台运行这个脚本:

./rsync_inotify.sh

  1. 将脚本的执行添加到开机启动项中。

echo ‘sh /root/rsync_inotify.sh &’ >>/etc/rc.local

这样就实现了本地的实时同步。
查看后台运行的作业:

jobs

 结果如下:

[1]+ Running ./test.sh &

可以看到,它的作业号为1

如果想结束这个作业,可以执行:

fig %1

其中1表示作业号。

2.2 远程实时同步

2.2.1 shell模式

  1. 需要在目标主机和服务器端都安装rsync:
    使用 yum 安装rsync

yum install rsync

  1. 在源主机配置 rsync 参数文件 /etc/rsyncd.conf,内容如下:

# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example:

pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
logfile = /var/log/rsyncd.log
uid = nobody
gid = nobody
use chroot = no
max connections = 200
timeout = 300
port = 873
read only =false
list = false
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

  1. 在源主机和目标主机重启 rsync:

systemctl restart rsync

  1. 如果要求源主机主动将文件的差异部分发送给目标主机, 那么源主机需要免密连接到目标主机。所以要在源主机生成公钥和私钥,命令如下:

ssh-keygen -t rsa

输入命令后只需要一直点击enter键即可,直到出现如图界面,那么它提示你公钥和私钥保存在哪个位置。

打开公钥文件/root/.ssh/id_rsa,复制它的内容。

  1. 在目标主机中,进入目录 /root/.ssh,编辑文件 authorized_keys,将源主机公钥的内容粘贴到文件末尾。

  2. 在目标主机重启ssh:

systemctl restart sshd

  1. 在源主机验证能否免密登录

ssh root@10.40.239.236

如果出现如下的提示信息,表示免密登录成功:

  1. 完成免密认证的配置后,在源主机上安装inotify-tools。

yum –y inotify-tools

查看 inotify 的安装位置是 /usr/local/inotify。两个命令inotifywait 和 inotifywatch位于 目录 /usr/local/inotify/bin/ 中

  1. 修改inotify默认参数(inotify默认内核参数值太小)
    查看系统默认参数值:

sysctl -a | grep max_queued_events

结果是:fs.inotify.max_queued_events = 16384

sysctl -a | grep max_user_watches

结果是:fs.epoll.max_user_watches = 378327

sysctl -a | grep max_user_instances

结果是:fs.inotify.max_user_instances = 128

修改参数:

sysctl -w fs.inotify.max_queued_events=“99999999”
sysctl -w fs.inotify.max_user_watches=“99999999”
sysctl -w fs.inotify.max_user_instances=“65535”

参数说明:
max_queued_events:
inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确
max_user_watches:
要同步的文件包含多少目录,可以用:find /Data/xqsj_upload -type d | wc -l 统计这些源目录下的目录数,必须保证max_user_watches值大于统计结果(这里/Data/xqsj_upload为同步的源文件目录)
max_user_instances:
每个用户创建inotify实例最大值。

  1. 在源主机上创建定时同步脚本rsync_inotify.sh,内容如下:
#!/bin/bash
rc_dir=/root/data/
remote_addr=10.40.239.236
dest_dir=/root/backup/
user=rsync_user
export PATH=/usr/local/inotify/bin/:$PATHinotifywait -mrq --timefmt '%Y/%m/%d %H:%M:%S' --format '%T %w%f %e' -e create,delete,close_write,attrib,move $src_dir | while read line
do# $line, for exiample:# 2020/01/27 16:57:14 /root/data/a.txt CREATEfile_path=$(echo $line | awk '{print $3}')# If file exists, that means, the event is "create", "close_write", "attrib" or "move_to"if [ -f $file_path ] || [ -d $file_path ]then# rsync the file,  ${file_path#$src_dir/} is the relative path of the file/usr/bin/rsync -avz --port=873 --delete $file_path $user@$remote_addr:$dest_dir/${file_path#$src_dir/} --password-file=/etc/rsync.passecho " $(date +%F%T): ${file} is rsynced" >> . /rsync.log 2>&1else# IF file does not exist, that means, the event is "delete" or "move_from"# rsync the directory of the filefile_path_parent=$(dirname $file_path)/usr/bin/rsync -avz --port=873 --delete $file_path_parent $user@$remote_addr:$dest_dir/${file_path_parent#$src_dir/} --password-file=/etc/rsync.passecho "$(date +%F%T): $file_path is rsynced" >> /tmp/rsync.log 2>&1fi
done

本文这个文件的路径是/root/rsync_inotify.sh,修改它的权限使用:

chmod 744 rsync_inotify.sh

  1. 在后台运行这个脚本:

./rsync_inotify.sh

上面的脚本中,

inotifywait -mrq --timefmt ‘%Y/%m/%d %H:%M:%S’ --format ‘%T %w%f %e’ -e create,delete,close_write,attrib,move $src_dir

这条命令的作用是实时监控目录及其内容的变更,并将变更的时间,变更的文件和目录的路径以及变更的事件类型打印出来。下文会讲解命令中的参数的含义。
打印的信息示例如下:
2020/01/27 16:57:14 /root/data/a.txt CLOSE_WRITE
表示结束了/root/data/a.txt 的写

| while read line
do
 file_path=$(echo $line | awk ‘{print $3}’)
  if [ -f $file_path ] || [ -d $file_path ]
  then
   /usr/bin/rsync -avz --port=873 --delete $file_path $user@$remote_addr:$dest_dir/${file_path#$src_dir/} --password-file=/etc/rsync.pass
   echo " $(date +%F%T): ${file} is rsynced" >> . /rsync.log 2>&1
 else
   file_path_parent=$(dirname $file_path)
   /usr/bin/rsync -avz --port=873 --delete $file_path_parent $user@$remote_addr:$dest_dir/${file_path_parent#$src_dir/} --password-file=/etc/rsync.pass echo “$(date +%F%T): $file_path is rsynced” >> /tmp/rsync.log 2>&1
  fi
done

则根据 inotify 打印的信息,得到发生变更的文件和目录,并rsync它们。如果其中的某个文件或目录存在,则说明发生在其上的事件是"create", “close_write”, “attrib” 或者 “move_to”(移入),程序会同步该文件。如果某个文件或目录不存在,则说明发生在其上的事件是"delete" or “move_from”(移出),则程序会同步该文件的上层目录。

inotifywait 参数说明

参数名称 参数说明
-m,–monitor 始终保持事件监听状态
-r,–recursive 递归查询目录
-q,–quiet 只打印监控事件的信息
-excludei 排除文件或目录时,不区分大小写
-t,–timeout 超时时间
–timefmt 指定时间输出格式
–format 指定时间输出格式
-e,–event 后面指定删、增、改等事件

inotifywait events事件说明

事件名称 事件说明
access 读取文件或目录内容
modify 修改文件或目录内容
attrib 文件或目录的属性改变
close_write 修改真实文件内容
close_nowrite
close
open 文件或目录被打开
moved_to 文件或目录移动到
moved_from 文件或目录从移动
move 移动文件或目录移动到监视目录
create 在监视目录下创建文件或目录
delete 删除监视目录下的文件或目录
delete_self
unmount 卸载文件系统

我们只需要监控的create,delete,close_write,attrib,move 这些参数,他们都和文件的变更有关。modify表示文件或目录内容的修改,它只有在保存后才有意义。如果修改被保存,则事件close_write会产生,因此不需要监控事件modify。 事件moved_to和move_from 属于move的子类型,也不需要监控。

  1. 将脚本的执行添加到开机启动项中。

echo ‘sh /root/rsync_inotify.sh &’ >>/etc/rc.local

2.2.2 后台进程模式

  1. 在源主机和目标主机都安装 rsync 和 xinetd:

yum install rsync
yum install xinetd

  1. 在目标主机新建或编辑 /etc/rsyncd.conf,配置rsync相关参数:

# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example:

pid file = /var/run/rsyncd.pid lock
file = /var/run/rsync.lock log
file = /var/log/rsyncd.log

[backup]
path = /root/backup/
comment = backup
uid = root
gid = root
use chroot = no
max connections = 200
timeout = 300
port = 873
read only = false
list = false
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
hosts allow = 10.40.239.234/24
secrets file = /etc/rsync.pass
auth users = rsync_user

注意:这种方法需要在 /etc/rsyncd.conf 中 将 uid,gid配置为目标主机中备份目录的所属用户和用户组,不能配置为nobody和其他用户。

  1. 在目标主机创建密码文件:

vi /etc/rsync.pass

写入源主机用户的用户名和密码,格式是用户:密码

rsync_user:!QAZ2wsx

  1. 设置文件权限,即rsync.pass认证文件是600权限!

chmod 600 /etc/rsync.pass

  1. 在目标主机上重启rsync

systemctl restart rsyncd

  1. 在目标主机上新建或编辑 /etc/xinetd.d/rsync,配置rsync相关参数:

# default: off # description: The rsync server is a good addition
to an ftp server, as it \ # allows crc checksumming etc.
service rsync {
disable = no #由默认的yes改为no,设置开机启动rsync
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}

  1. 重新启动xinetd:

systemctl restart xinetd

  1. 在源主机,创建一个密码文件,内容只有密码,和源主机的密码文件中的密码相同。这里,我设置的密码文件的位置是 /etc/rsync.pass,内容是 !QAZ2wsx。并修改其权限:

chmod 600 /etc/rsync.pass

  1. 在目标主机和源主机关闭防火墙和selinux,这是必要的。

systemctl stop firewalld
setenforce 0

  1. 在源主机上安装inotify-tools:

yum –y inotify-tools

查看 inotify 的安装位置是 /usr/local/inotify。两个命令inotifywait 和 inotifywatch位于 目录 /usr/local/inotify/bin/ 中

  1. 修改inotify默认参数(inotify默认内核参数值太小)
    查看系统默认参数值:

sysctl -a | grep max_queued_events

结果是: fs.inotify.max_queued_events = 16384

sysctl -a | grep max_user_watches

结果是: fs.epoll.max_user_watches = 378327

sysctl -a | grep max_user_instances

结果是: fs.inotify.max_user_instances = 128

修改参数:

sysctl -w fs.inotify.max_queued_events=“99999999” sysctl -w
fs.inotify.max_user_watches=“99999999” sysctl -w
fs.inotify.max_user_instances=“65535”

参数说明:
max_queued_events:
inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确
max_user_watches:
要同步的文件包含多少目录,可以用:find /Data/xqsj_upload -type d | wc -l 统计这些源目录下的目录数,必须保证max_user_watches值大于统计结果(这里/Data/xqsj_upload为同步的源文件目录)
max_user_instances:
每个用户创建inotify实例最大值。

  1. 在源主机上创建定时同步脚本rsync_inotify.sh,内容如下:
#!/bin/bash
rc_dir=/root/data/
remote_addr=10.40.239.236
dest_dir=/root/backup/
user=rsync_user
export PATH=/usr/local/inotify/bin/:$PATHinotifywait -mrq --timefmt '%Y/%m/%d %H:%M:%S' --format '%T %w%f %e' -e create,delete,close_write,attrib,move $src_dir | while read line
do# $line, for exiample:# 2020/01/27 16:57:14 /root/data/a.txt CREATEfile_path=$(echo $line | awk '{print $3}')# If file exists, that means, the event is "create", "close_write", "attrib" or "move_to"if [ -f $file_path ] || [ -d $file_path ]then# rsync the file,  ${file_path#$src_dir/} is the relative path of the file/usr/bin/rsync -avz --port=873 --delete $file_path $user@$remote_addr::backup /${file_path#$src_dir/} --password-file=/etc/rsync.passecho " $(date +%F%T): ${file} is rsynced" >> . /rsync.log 2>&1else# IF file does not exist, that means, the event is "delete" or "move_from"# rsync the directory of the filefile_path_parent=$(dirname $file_path)/usr/bin/rsync -avz --port=873 --delete $file_path_parent $user@$remote_addr::backup/${file_path_parent#$src_dir/} --password-file=/etc/rsync.passecho "$(date +%F%T): $file_path is rsynced" >> /tmp/rsync.log 2>&1fi
done

本文这个文件的路径是/root/rsync_inotify.sh,修改它的权限

chmod 744 rsync_inotify.sh

  1. 在后台运行这个脚本:

./rsync_inotify.sh

  1. 将脚本的执行添加到开机启动项中。

echo ‘sh /root/rsync_inotify.sh &’ >>/etc/rc.local

参考

[1] rsync. rsyncd.conf.2018-01-28
[2]百度百科. rsync
[3] 十五十六. rsync搭建部署和配置文件详解. 2018-05-29
[4] wc1695040842. rsync+inotify实时同步文件. 2019-06-03
[5] 王晓冬. Rsync故障排查整理. 2017-03-14

附:Rsyncd.conf 配置参数

模块参数
主要是定义服务器哪个目录要被同步。其格式必须为“[module]”形式,这个名字就是在rsync 客户端看到的名字,其实有点象Samba服务器提供的共享名。而服务器真正同步的数据是通过 path 来指定的。我们可以根据自己的需要,来指定多个模块,模块中可以定义以下参数:
comment
给模块指定一个描述,该描述连同模块名在客户连接得到模块列表时显示给客户。默认没有描述定义。

path
指定该模块的供备份的目录树路径,该参数是必须指定的。

use chroot
如果" use chroot" 指定为true,那么rsync在传输文件以前首先chroot到path参数所指定的目录下。这样做的原因是实现额外的安全防护,但是缺点是需要以roots权限,并且不能备份指向外部的符号连接所指向的目录文件。默认情况下chroot值为true。

uid
该选项指定当该模块传输文件时,守护进程应该具有的uid,配合gid选项使用可以确定哪些可以访问怎么样的文件权限,默认值是" nobody" 。

gid
该选项指定当该模块传输文件时,守护进程应该具有的gid。默认值为" nobody" 。

max connections
指定该模块的最大并发连接数量以保护服务器,超过限制的连接请求将被告知随后再试。默认值是0,也就是没有限制。

list
该选项设定当客户请求可以使用的模块列表时,该模块是否应该被列出。如果设置该选项为false,可以创建隐藏的模块。默认值是true。

read only
该选项设定是否允许客户上载文件。如果为true那么任何上载请求都会失败,如果为false并且服务器目录读写权限允许那么上载是允许的。默认值为true。

exclude
用来指定多个由空格隔开的多个文件或目录(相对路径),并将其添加到exclude列表中。这等同于在客户端命令中使用–exclude来指定模式,一个模块只能指定一个exclude选项。但是需要注意的一点是该选项有一定的安全性问题,客户很有可能绕过exclude列表,如果希望确保特定的文件不能被访问,那就最好结合uid/gid选项一起使用。

exclude from
指定一个包含exclude模式的定义的文件名,服务器从该文件中读取exclude列表定义。

include
用来指定不排除符合要求的文件或目录。这等同于在客户端命令中使用–include来指定模式,结合include和exclude可以定义复杂的exclude/include规则。

include from
指定一个包含include模式的定义的文件名,服务器从该文件中读取include列表定义。

auth users
该选项指定由空格或逗号分隔的用户名列表,只有这些用户才允许连接该模块。这里的用户和系统用户没有任何关系。如果" auth users" 被设置,那么客户端发出对该模块的连接请求以后会被rsync请求challenged进行验证身份这里使用的challenge/response认证协议。用户的名和密码以明文方式存放在" secrets file" 选项指定的文件中。默认情况下无需密码就可以连接模块(也就是匿名方式)。

secrets file
该选项指定一个包含定义用户名:密码对的文件。只有在" auth users" 被定义时,该文件才有作用。文件每行包含一个username:passwd对。一般来说密码最好不要超过8个字符。没有默认的secures file名,需要限式指定一个(例如:/etc/rsyncd.passwd)。注意:该文件的权限一定要是600,否则客户端将不能连接服务器。

strict modes
该选项指定是否监测密码文件的权限,如果该选项值为true那么密码文件只能被rsync服务器运行身份的用户访问,其他任何用户不可以访问该文件。默认值为true。

hosts allow
该选项指定哪些IP的客户允许连接该模块。客户模式定义可以是以下形式:
单个IP地址,例如:192.167.0.1

整个网段,例如:192.168.0.0/24,也可以是192.168.0.0/255.255.255.0
多个IP或网段需要用空格隔开,“*”则表示所有,默认是允许所有主机连接。

hosts deny
指定不允许连接rsync服务器的机器,可以使用hosts allow的定义方式来进行定义。默认是没有hosts deny定义。

ignore errors
指定rsyncd在判断是否运行传输时的删除操作时忽略server上的IO错误,一般来说rsync在出现IO错误时将将跳过–delete操作,以防止因为暂时的资源不足或其它IO错误导致的严重问题。

ignore nonreadable
指定rysnc服务器完全忽略那些用户没有访问权限的文件。这对于在需要备份的目录中有些文件是不应该被备份者得到的情况是有意义的。

lock file
指定支持max connections参数的锁文件,默认值是/var/run/rsyncd.lock。

transfer logging
使rsync服务器使用ftp格式的文件来记录下载和上载操作在自己单独的日志中。

log format
通过该选项用户在使用transfer logging可以自己定制日志文件的字段。其格式是一个包含格式定义符的字符串,可以使用的格式定义符如下所示:
%h 远程主机名
%a 远程IP地址
%l 文件长度字符数
%p 该次rsync会话的进程id
%o 操作类型:" send" 或" recv"
%f 文件名
%P 模块路径
%m 模块名
%t 当前时间
%u 认证的用户名(匿名时是null)
%b 实际传输的字节数
%c 当发送文件时,该字段记录该文件的校验码
默认log格式为:" %o %h [%a] %m (%u) %f %l" ,一般来说,在每行的头上会添加" %t [%p] " 。在源代码中同时发布有一个叫rsyncstats的perl脚本程序来统计这种格式的日志文件。

timeout
通过该选项可以覆盖客户指定的IP超时时间。通过该选项可以确保rsync服务器不会永远等待一个崩溃的客户端。超时单位为秒钟,0表示没有超时定义,这也是默认值。对于匿名rsync服务器来说,一个理想的数字是600。

refuse options
通过该选项可以定义一些不允许客户对该模块使用的命令参数列表。这里必须使用命令全名,而不能是简称。但发生拒绝某个命令的情况时服务器将报告错误信息然后退出。如果要防止使用压缩,应该是:" dont compress = *" 。

dont compress
用来指定那些不进行压缩处理再传输的文件,默认值是*.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz

Linux平台上文件同步——rsync+inotify之实时同步相关推荐

  1. 运维之道 | Linux rsync 文件同步、Inotify远程实时同步

    Linux rsync 文件同步服务器 与传统的cp.scp.tar备份方式相比,rsync具有安全性高.备份迅速.支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的 ...

  2. rsync + inotify 实现实时同步

    rsync + inotify 实现实时同步 rsync 简介 Rsync是一款开源的.快速的.多功能的.可实现全量及增量的本地或远程数据同步备份的优秀工具.并且可以不进行改变原有数据的属性信息,实现 ...

  3. rsync+inotify实现实时同步案例详解

    rsync+inotify实现实时同步案例详解 转自:http://chocolee.blog.51cto.com/8158455/1400596 随着应用系统规模的不断扩大,对数据的安全性和可靠性也 ...

  4. Rsync+inotify实现实时同步

    如果有一天去接受面试:面试官问你一个1t的文件需要进行传输,问题用何种方法.假如你答了scp或者ftp.那么你挂的几率是很大的 一.inotify介绍 inotify-tools有两个命令 //ino ...

  5. rsync + inotify 数据实时同步

    一.rsync介绍 rsync英文全称为Remote synchronization,从软件的名称就可以看出来,Rsync具有可是本地和远程两台主机之间的数据快速复制同步镜像.远程备份的功能,这个功能 ...

  6. Linux平台上文件同步——rsync+inotify之定时同步

    1 前言 1.1 概述 本文介绍使用rsync和 inotify-tools,实现linux 上的本地定时同步和远程定时同步的方法. 1.2 实验环境 服务器两台 操作系统: CentOS-7.4 软 ...

  7. 【Rsync + inotify】 实时同步远程服务器目录文件

    文章目录 服务端 安装.配置 rsync 客户端 安装rsync 使用rsysnc 实现 实时同步 如何排错??? 服务端 安装.配置 rsync centos: # yum install rsyn ...

  8. rsync+inotify实现实时同步案例--转

    转自:http://chocolee.blog.51cto.com/8158455/1400596 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐 ...

  9. rsync+inotify实现实时同步案例

    随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输.如果文件数 ...

最新文章

  1. 【 Vivado 】通过IP Integrator进行设计示例
  2. html 浮动脱离文档流,CSS标准文档流与脱离文档流
  3. 【CV】使用OpenCV搭建违章停车检测系统
  4. 七十五、SpringBoot 的数据缓存cache(二)
  5. 机器学习系列(一)感知器分类算法
  6. 使用MapReduce将HDFS数据导入Mysql
  7. 程序员怎么看待C语言?最伟大?最落后?
  8. 框架简述 带你认识 Mybatis
  9. 三个等于号===和两个等于号==的区别
  10. java 同步块原理_Java同步代码块和同步方法原理与应用案例详解
  11. Iphone获取本地ip地址
  12. 基于协同过滤,NMF和Baseline的推荐算法
  13. 脏数据-数据量纲差异
  14. linuxt gogs搭建
  15. 用Bolt引擎实现换肤指南
  16. wps页眉偶数页不同怎么设置_请问在wps文档中如何设置奇偶页不同的页眉
  17. 阿里云商标智能注册申请图文教程(亲踩坑)
  18. liunx中文件夹不能删除怎么操作
  19. 万豪 数据泄露 sql注入_如何防止数据库泄漏和注入
  20. 安卓眼球追踪_iPhone 11 Pro 可配合 Eyeware Beam 眼球追踪玩 PC 大屏游戏

热门文章

  1. jsp中文传值到java乱码_jsp传递参数中文乱码解决办法
  2. 微信小程序---验证码倒计时
  3. 安装MinGW和MSYS
  4. RADARE2+FRIDA=R2FRIDA Best Dynamic Debugging Tool
  5. 计算机如何连接网络扫描仪,如何添加局域网网络扫描仪
  6. python人机对战_【人机对战】用python打造经典井字游戏
  7. 有趣的SQL DIGEST
  8. Hadoop学习笔记——入门教程(虚拟机安装LinuxHadoop环境搭建配置)
  9. dubbo核心源码流程分析
  10. 本地项目关联远程 git 仓库