rsync 简介node
Rsync(remote synchronize)是一个远程数据同步工具,可经过LAN/WAN快速同步多台主机间的文件,也可使用 Rsync 同步本地硬盘中的不一样目录。 Rsync 是用于取代rcp的一个工具,Rsync使用所谓的 “Rsync quick check算法” 来使本地和远程两个主机之间的文件达到同步。
这个算法只传送两个文件的不一样部分(同步大小或者最后修改时间发生变化的文件或目录,也能够根据权限、属主等属性的变化),而不是每次都整份传送(即增量),所以速度至关快。 Rsync支持大多数的类Unix系统,不管是Linux、Solaris仍是BSD上都通过了良好的测试。此外,它在windows平台下也有相应的版本,比较知名的有cwRsync和Sync2NAS。 Rsync 的初始做者是 Andrew Tridgell 和 Paul Mackerras,它当前由 http://rsync.samba.org维护。
Rsync的基本特色以下: 1. 能够镜像保存整个目录树和文件系统; 2. 能够很容易作到保持原来文件的权限、时间、软硬连接等; 3. 无须特殊权限便可安装; 4. 优化的流程,文件传输效率高; 5. 可使用rcp、ssh等方式来传输文件,固然也能够经过直接的socket链接; 6. 支持匿名传输,以方便进行网站镜像。
在使用 rsync 进行远程同步时,可使用两种方式:
客户端命令方式:远程 Shell 方式(建议使用 ssh,用户验证由 ssh 负责)
网络服务方式:C/S 方式(即客户链接远程 rsync 服务器,用户验证由 rsync 服务器负责)。
不管本地同步目录仍是远程同步数据,首次运行时将会把所有文件拷贝一次,之后再运行时将只拷贝有变化的文件(对于新文件)或文件的变化部分(对于原有文件)。 rsync 在首次复制时没有速度优点,速度不如 tar,所以当数据量很大时您能够考虑先使用 tar 进行首次复制,而后再使用 rsync 进行数据同步。
应用场景:
实时同步
定时同步
inotify+rsync实时同步
优缺点:
实现增量同步,支持socket(daemon),实现集中备份
大量小文件同步时,时间比较长,有时候进程中止。并发若是大于200个文件(10-100k),同步就会有延时
同步大文件,有时会中断,未完整同步前,是隐藏文件
NAME
rsync -- a fast, versatile, remote (and local) file-copying tool
SYNOPSIS(三种工做模式)
第一种 Local: rsync [OPTION...] SRC... [DEST]
#rsync -avz /etc/hosts /tmp/ =====>相似cp
#rsync -avz --delete /null/ /tmp/ =====>相似rm /null是建立的空目录,若是有内容,即同步过去
第二种 Access via remote shell:
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
#rsync -avzP -e 'ssh -p 22' /tmp/ root@192.168.0.85:/tmp
#rsync -avzP -e 'ssh -p 22' root@192.168.0.85:/tmp/ /tmp/ ===>目录后斜线指的是其中内容,不包含自己
第三种 Access via rsync daemon:
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
#rsync -avz rsync://rsync_backup@192.168.0.85/backup /tmp/ --password-file=/etc/rsync.password
Push: rsync [OPTION...] SRC... [USER@]HOST DEST
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
Rsync客户端命令的同步参数选项其余参数。请见man rsync
生产经常使用avz ==vzrtopgDl (或者-vzrtoppg)
-v, --verbose increase verbosity 详细模式输出,进度
-z, --compress compress file data during the transfer传输时进行压缩
-r, --recursive recurse into directories 对子目录以递归模式
-a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
-o, --owner preserve owner (super-user only)保持文件属主信息
-p, --perms preserve permissions 保持文件权限
-D same as --devices --specials保持设备文件信息
-e, --rsh=COMMAND specify the remote shell to use使用信道协议,指定替代rsh的shell程序
-P --progress show progress during transfer
-l, --links copy symlinks as symlinks 保持软连接
-t --times 保持文件时间信息
--exclude=PATTERN exclude files matching PATTERN --exclude={文件1,文件2}
或者在服务端配置文件rsyncd.conf加入排除
--exclude-from=FILE read exclude patterns from FILE
--bwlimit=KBPS limit I/O bandwidth; KBytes per second
rsync服务端配置算法
root@node85 ~]# rpm -qa | grep rsync #默认安装 rsync-3.0.6-12.el6.x86_64 [root@node85 ~]# cat /etc/rsyncd.conf #编辑rsyncd配置文件 # Distributed under the terms of the GNU General Public License v2 # Minimal configuration file for rsync daemon # See rsync(1) and rsyncd.conf(5) man pages for help # This line is required by the /etc/init.d/rsyncd script uid = rsync gid = rsync use chroot = no max connections = 5 timeout = 600 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=192.168.0.0/255.255.255.0 hosts deny=* auth users = rsyncd_backup secrets file = /etc/rsyncd.password ############################# [backup] comment = backup server by gtms 2016-05-07 path = /backup
全局参数 在文件中 [module] 以外的全部配置行都是全局参数。固然也能够在全局参数部分定义模块参数,这时该参数的值就是全部模块的默认值。 address 在独立运行时,用于指定的服务器运行的 IP 地址。由 xinetd 运行时将忽略此参数,使用命令行上的-address 选项替代。 默认本地全部IP port 指定 rsync 守护进程监听的端口号。 由 xinetd 运行时将忽略此参数,使用命令行上的-port 选项替代。 默认873 motd file 指定一个消息文件,当客户链接服务器时该文件的内容显示给客户。 默认无 pid file rsync 的守护进程将其 PID 写入指定的文件。 默认无 log file 指定 rsyncd 守护进程的日志文件,而不将日志发送给syslog。 默认无 syslog facility 指定 rsync 发送日志消息给 syslog 时的消息级别。 socket options 指定自定义 TCP 选项。 默认无 模块参数 模块参数主要用于定义 rsync 服务器哪一个目录要被同步。模块声明的格式必须为 [module] 形式,这个名字就是在 rsync 客户端看到的名字,相似于 Samba 服务器提供的共享名。 而服务器真正同步的数据是经过 path 来指定的。能够根据本身的须要,来指定多个模块,模块中能够定义如下参数: a. 基本模块参数 path 指定当前模块在 rsync 服务器上的同步路径,该参数是必须指定的。 默认无 comment 给模块指定一个描述,该描述连同模块名在客户链接获得模块列表时显示给客户。 默认无 b. 模块控制参数 use chroot 若为 true,则 rsync 在传输文件以前首先 chroot 到 path 参数所指定的目录下。这样作的缘由是实现额外的安全防御,可是缺点是须要 root 权限,而且不能备份指向 path 外部的符号链接所指向的目录文件。 默认true uid 指定该模块以指定的 UID 传输文件。 nobody gid 指定该模块以指定的 GID 传输文件。 nobody max connections 指定该模块的最大并发链接数量以保护服务器,超过限制的链接请求将被告知随后再试。 默认没有限制 lock file 指定支持 max connections 参数的锁文件。 /var/run/rsyncd.lock list 指定当客户请求列出可使用的模块列表时,该模块是否应该被列出。若是设置该选项为 false,能够建立隐藏的模块。 默认true read only 指定是否容许客户上传文件。若为 true 则不容许上传;若为 false 而且服务器目录也具备读写权限则容许上传。 默认true write only 指定是否容许客户下载文件。若为 true 则不容许下载;若为 false 而且服务器目录也具备读权限则容许下载。 默认false ignore errors 指定在 rsync 服务器上运行 delete 操做时是否忽略 I/O 错误。通常来讲 rsync 在出现 I/O 错误时将将跳过 -delete 操做,以防止由于暂时的资源不足或其它 I/O 错误致使的严重问题。 默认true ignore nonreadable 指定 rysnc 服务器彻底忽略那些用户没有访问权限的文件。这对于在须要备份的目录中有些不该该被备份者得到的文件时是有意义的。 默认false timeout 该选项能够覆盖客户指定的 IP 超时时间。从而确保 rsync 服务器不会永远等待一个崩溃的客户端。对于匿名 rsync 服务器来讲,理想的数字是 600(单位为秒)。 默认0 未限制 dont compress 用来指定那些在传输以前不进行压缩处理的文件。该选项能够定义一些不容许客户对该模块使用的命令选项列表。必须使用选项全名,而不能是简称。当发生拒绝某个选项的状况时,服务器将报告错误信息而后退出。例如,要防止使用压缩,应该是:”dont compress = *”。 *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz c. 模块文件筛选参数 参数 说明 默认值 exclude 指定多个由空格隔开的多个文件或目录(相对路径),并将其添加到 exclude 列表中。这等同于在客户端命令中使用 –exclude 来指定模式。 空 exclude from 指定一个包含 exclude 规则定义的文件名,服务器从该文件中读取 exclude 列表定义。 空 include 指定多个由空格隔开的多个文件或目录(相对路径),并将其添加到 include 列表中。这等同于在客户端命令中使用 –include 来指定模式 。 空 include from 指定一个包含 include 规则定义的文件名,服务器从该文件中读取 include 列表定义。 空 一个模块只能指定一个exclude 参数、一个include 参数。 结合 include 和 exclude 能够定义复杂的exclude/include 规则 。 这几个参数分别与相应的rsync 客户命令选项等价,惟一不一样的是它们做用在服务器端。 关于如何书写规则文件的内容请参考http://www.howtocn.org/rsync:use_rsync。 d. 模块用户认证参数 参数 说明 默认值 auth users 指定由空格或逗号分隔的用户名列表,只有这些用户才容许链接该模块。这里的用户和系统用户没有任何关系。用户名和口令以明文方式存放在 secrets file 参数指定的文件中。 (匿名方式) secrets file 指定一个 rsync 认证口令文件。只有在 auth users 被定义时,该文件才起做用。 空 strict modes 指定是否监测口令文件的权限。若为 true 则口令文件只能被 rsync 服务器运行身份的用户访问,其余任何用户不能够访问该文件。 true rsync 认证口令文件的权限必定是 600,不然客户端将不能链接服务器。 rsync 认证口令文件中每一行指定一个 用户名:口令 对,格式为: username:passwd 通常来讲口令最好不要超过8个字符。若您只配置匿名访问的 rsync 服务器,则无需设置上述参数。 e. 模块访问控制参数 参数 说明 默认值 hosts allow 用一个主机列表指定哪些主机客户容许链接该模块。不匹配主机列表的主机将被拒绝。 * hosts deny 用一个主机列表指定哪些主机客户不容许链接该模块。 空 客户主机列表定义能够是如下形式: 单个IP地址。例如:192.168.0.1 整个网段。例如:192.168.0.0/24,192.168.0.0/255.255.255.0 可解析的单个主机名。例如:centos,centos.bsmart.cn 域内的全部主机。例如:*.bsmart.cn “*”则表示全部。 多个列表项要用空格间隔。 f. 模块日志参数 参数 说明 默认值 transfer logging 使 rsync 服务器将传输操做记录到传输日志文件。 false log format 指定传输日志文件的字段。 ”%o %h [%a] %m (%u) %f %l” 设置了”log file”参数时,在日志每行的开始会添加”%t [%p]“。 可使用的日志格式定义符以下所示: %a - 远程IP地址 %h - 远程主机名 %l - 文件长度字符数 %p - 该次 rsync 会话的 PID %o - 操做类型:”send” 或 “recv” %f - 文件名 %P - 模块路径 %m - 模块名 %t - 当前时间 %u - 认证的用户名(匿名时是 null) %b - 实际传输的字节数 %c - 当发送文件时,记录该文件的校验码
[root@node85 ~]# useradd rsync -s /sbin/nologin -M
[root@node85 ~]# mkdir /backup
[root@node85 ~]# chown -R rsync /backup/
[root@node85 ~]# echo "rsyncd_backup:rootabcd" >>/etc/rsyncd.password
[root@node85 ~]# chmod 600 /etc/rsyncd.password
[root@node85 ~]# rsync --daemon
[root@node85 ~]# lsof -i:873
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsync 27269 root 3u IPv4 33755 0t0 TCP *:rsync (LISTEN)
rsync 27269 root 5u IPv6 33756 0t0 TCP *:rsync (LISTEN)
[root@node85 ~]# ps -ef | grep rsync
root 27269 1 0 05:55 ? 00:00:00 rsync --daemon
root 27272 2256 0 05:56 pts/0 00:00:00 grep rsyncshell
rsync客户端配置express
[root@node84 ~]# cat /etc/rsyncd.passwd
rootabcd
[root@node84 ~]# chmod 600 /etc/rsyncd.passwd
push方式
[root@node84 ~]# rsync -avz /tmp/ rsyncd_backup@192.168.0.85::backup --password-file=/etc/rsyncd.password
sending incremental file list
./
yum.log
.ICE-unix/
ssh-hgfbdL1711/
ssh-hgfbdL1711/agent.1711
sent 170 bytes received 41 bytes 46.89 bytes/sec
total size is 0 speedup is 0.00
[root@node85 ~]# ls /backup/ #从server上查看
ssh-hgfbdL1711 yum.log
[root@node84 tmp]# rm -rf *
pull方式
[root@node84 tmp]# rsync -avz rsyncd_backup@192.168.0.85::backup /tmp/ --password-file=/etc/rsyncd.password
receiving incremental file list
./
yum.log
.ICE-unix/
ssh-hgfbdL1711/
ssh-hgfbdL1711/agent.1711
sent 98 bytes received 259 bytes 64.91 bytes/sec
total size is 0 speedup is 0.00
[root@node84 tmp]# ls
ssh-hgfbdL1711 yum.log
关键参数 --deletewindows
rsync -avz --delete rsyncd_backup@192.168.0.85::backup /tmp/ --password-file=/etc/rsyncd.password 操做结果,远端有啥,本地有啥,远端没有的,本地也会被删除 所以,须要慎用
rsync进程查杀centos
#kill -9 强制杀,不要轻易使用 #pkill rsync 狠杀进程,不要轻易使用 #killall rsync 平滑杀,屡次执行(会提示没有进程被杀死) #kill pid不要轻易使用 以下2种平滑: #kill -HUP `cat /var/run/rsyncd.pid` #kill -USR2 `cat /var/run/rsyncd.pid`
配置实用inotify工具实现实时同步安全
检查确认环境支持
root@node84 ~]# ls -l /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Jan 30 07:34 max_queued_events (优化调大)设置inotifywait或inotifywatch命令能够监控的文件数量(单程)
-rw-r--r-- 1 root root 0 Jan 30 07:34 max_user_instances (优化调大)设置每一个用户能够运行的inotifywait或inotifywatch命令的进程数
-rw-r--r-- 1 root root 0 Jan 30 07:34 max_user_watches 设置inotify实例事件(event)队列可容纳的事件数量
编译安装inotify-tools
tar -xf inotify-tools-3.13.tar.gz
cd inotify-tools-3.13
./configure --prefix=/usr/local/inotify-tools-3.13
make && make install
ln -s /usr/local/inotify-tools-3.13/ /usr/local/inotify-tools
root@node84 ~]# ls -l /usr/local/inotify-tools/bin/
total 80
-rwxr-xr-x 1 root root 38606 Jan 30 07:38 inotifywait 在被监控的文件或目录上等待特定文件系统事件(open close delete等)发生,执行后处于阻塞状态,适合在shell脚本中使用
-rwxr-xr-x 1 root root 40377 Jan 30 07:38 inotifywatch 收集被监视的文件系统使用度统计数据,指文件系统事件发生的次数统计
inotifywait参数介绍
[root@node84 ~]# /usr/local/inotify-tools/bin/inotifywait --help
inotifywait 3.13
Wait for a particular event on a file or set of files.
Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
Options:
-h|--help Show this help text.
@<file> Exclude the specified file from being watched.
--exclude <pattern>
Exclude all events on files matching the
extended regular expression <pattern>.
--excludei <pattern>
Like --exclude but case insensitive.
-m|--monitor Keep listening for events forever. Without
this option, inotifywait will exit after one
event is received.
-r|--recursive Watch directories recursively.
--fromfile <file>
Read files to watch from <file> or `-' for stdin.
-q|--quiet Print less (only print events).
-qq Print nothing (not even events).
--format <fmt> Print using a specified printf-like format
string; read the man page for more details.
--timefmt <fmt> strftime-compatible format string for use with
%T in --format string.
-c|--csv Print events in CSV format.
-t|--timeout <seconds>
When listening for a single event, time out after
waiting for an event for <seconds> seconds.
If <seconds> is 0, inotifywait will never time out.
-e|--event <event1> [ -e|--event <event2> ... ]
Listen for specific event(s). If omitted, all events are
listened for.
Exit status:
0 - An event you asked to watch for was received.
1 - An event you did not ask to watch for was received
(usually delete_self or unmount), or some error occurred.
2 - The --timeout option was given and no events occurred
in the specified interval of time.
Events:
access file or directory contents were read
modify file or directory contents were written
attrib file or directory attributes changed
close_write file or directory closed, after being opened in
writeable mode
close_nowrite file or directory closed, after being opened in
read-only mode
close file or directory closed, regardless of read/write mode
open file or directory opened
moved_to file or directory moved to watched directory
moved_from file or directory moved from watched directory
move file or directory moved to or from watched directory
create file or directory created within watched directory
delete file or directory deleted within watched directory
delete_self file or directory was deleted
unmount file system containing file or directory unmounted
[root@node84 ~]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create,delete /tmp/
30/01/17 07:57 /tmp/a
30/01/17 07:57 /tmp/a
=>对/tmp目录进行文件建立及删除的监控,测试建立文件a,而后删除,能够监控到事件
同步脚本
#!/bin/bash
inotify=/usr/local/inotify-tools/bin/inotifywait
$inotify -mrq --format '%w%f' -e create,close_write,delete /nfs84 \
|while read file
do
rsync -az --delete /nfs84 rsyncd_backup@192.168.0.85::backup --password-file=/etc/rsyncd.password
done
ps: /dir /形式推送不影响目标目录其余文件,推送整个目录
/dir 形式,使用--delete,即彻底被rsync影响