Rsync远程同步


rsync简介:

rsync是一款优秀的、快速的、多平台的本地或远程数据镜像同步备份工具。适用于Unix/Linux/Windows等多种平台。

在同步备份时,默认情况下,rsync通过其独特的quick check算法,仅同步大小或者最后修改时间发生变化的文件或目录(也可根据权限,属主等变化同步,需指定参数)甚至是只同步一个文件里有变化的内容部分,所以,可以实现快速的同步数据的功能。

rsync和cp,scp的区别:

cp,scp工具拷贝每次均为完整拷贝,而rsync除了完整备份,还具备增量拷贝的功能,因此,从性能及效率上更胜一筹。

rsync工作方式:   
  rsync大致使用三种主要的方式来传输数据:
  1.本地数据传输
  2.通过rcp,ssh等通道传输
  3.以守护进程的方式传输数据

本地数据传输
语法:
  rsync [OPTION] SRC... [DEST]
SRC:源文件
DEST:目标文件
option参数说明:    #一般使用-avz就可以
-a:归档模式,递归并保留对象属性,等同于 -rlptgoD
-r:递归模式,包含目录及子目录中所有文件
-l:对于符号链接文件仍然复制为符号链接文件
-p:保留文件的权限标记
-t:保留文件的时间标记
-g:保留文件的属组标记(仅超级用户使用)
-o:保留文件的属主标记(仅超级用户使用)
-D:保留设备文件及其他特殊文件
-v:显示同步过程的详细(verbose)信息
-z:在传输文件时进行压缩(compress)
-H:保留硬连接文件
-A:保留ACL属性信息
--delete:删除目标位置有而原始位置没有的文件
--checksum:根据对象的校验和来决定是否跳过文件

演示:将/etc 目录下的文件拷贝到/tmp 目录下
 [root@localhost ~]# rsync -avz -P /etc /tmp/  #第一次拷贝的时候时间比较长,输出信息也比较多
第二次拷贝非常快,因为/etc目录下没有数据更新
 [root@localhost ~]# rsync -avz -P /etc /tmp/
building file list ...
2141 files to consider

sent 61983 bytes  received 20 bytes  124006.00 bytes/sec
total size is 88435897  speedup is 1426.32
 
注意:rsync -avz -P /etc/ /tmp/      仅同步etc目录里的内容,etc本身不同步 
      rsync -avz -P /etc /tmp/      把etc和etc里面的内容全部考到tmp目录下
 
通过远程shell进行数据传输:
语法:
拉取:rsync [option] [USER@]HOST:SRC...  [DEST]
推送:rsync [option] SRC... [USER@]HOST:DEST
[USER@]HOST:为Rsync同步的远程的连接用户和主机地址
SRC:为源,即拷贝的分区、文件或目录等,和HOST之间用一个冒号连接;
[DEST]为目的分区、文件或目录等


演示:要事先做好ssh通道
将本地的etc目录推送到tmp目录下,由于有ssh通道,所以没有要密码
[root@localhost ~]# rsync -avz -P /etc -e 'ssh -p 22' root@192.168.0.108:/tmp/

将远端的etc目录备份到本地的tmp目录下
[root@localhost ~]# rsync -avz -P -e 'ssh -p 22' root@192.168.0.108:/etc /tmp/

守护进程的方式:
[root@localhost ~]# vim /etc/rsyncd.conf
uid = root
gid = root
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
[www]                    #模块,读取数据的时候需要用这个名
path= /var/www/html/    #远端备份的路径,抓取也是从这里抓,如果用户上传uid和gid用户需要用上传权限,如果度也要有读取权限
ignore errors            #忽略错误
read only = false        #只读为假就是可以上传数据,如果是true那么就不能上传数据了
list = false 
hosts allow = 192.168.0.0/24
hosts deny = 0.0.0.0/32
auth users = rbackup                #认证的用户
secrets file = /etc/rsync.password  #存放用户和密码文件

创建存放认证用户的文件
[root@localhost ~]# vim /etc/rsync.password
rbackup:RedHat
[root@localhost ~]# chmod 600 /etc/rsync.password  #权限必须是600
[root@localhost ~]# ls /etc/rsync.password -l
-rw------- 1 root root 15 12-10 10:18 /etc/rsync.password

启动服务
[root@localhost ~]# rsync --daemon    #--daemon表示以守护进程的方式启动
[root@localhost ~]# ps -ef | grep rsync | grep -v grep
root      7915    1  0 10:20 ?        00:00:00 rsync --daemon
[root@localhost ~]# netstat -lnt | grep 873
tcp        0      0 0.0.0.0:873                0.0.0.0:*                  LISTEN     
tcp        0      0 :::873                      :::*                        LISTEN

设置开机自动启动
[root@localhost ~]# echo "/usr/bin/rsync --daemon" >> /etc/rc.d/rc.local
[root@localhost ~]# tail -1 /etc/rc.d/rc.local
/usr/bin/rsync --daemon

客户端部署
[root@localhost ~]# echo "redhat" > /etc/rsync.password  #仅需密码无须帐号
[root@localhost ~]# chmod 600 /etc/rsync.password        #文件权限600
[root@localhost ~]# cat /etc/rsync.password
redhat

使用守护进程的方式数据传输
拉取:rsync[OPTION][USER@]HOST::SRC [DEST]
      rsync[OPTION] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
推送:rsync[OPTION] SRC... [USER@]HOST::DEST
      rsync[OPTION] SRC... rsync://[USER@]HOST[:PORT]/DEST
     
客户端创建一个文件推送到服务器端
[root@localhost html]# touch a.txt
[root@localhost html]# rsync -avz -P /var/www/html/ rbackup@192.168.0.102::www/ --password-file=/etc/rsync.password
或者
[root@localhost html]# rsync -avz -P /var/www/html/ rsync://rbackup@192.168.0.102:/www --password-file=/etc/rsync.password
building file list ...
2 files to consider
./
a.txt
          0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/2)

sent 100 bytes  received 44 bytes  96.00 bytes/sec
total size is 0  speedup is 0.00

服务器端查看推送成功
[root@localhost html]# ls
a.txt  index.html  index.php

--deleted参数:保证本地数据和远端数据完全一致,本地有啥远端就有啥,如果本地没有远端就删除
客户端执行
[root@localhost ~]# mkdir aaa创建一个空目录
[root@localhost ~]# rsync -avz -P --delete /root/aaa/ rbackup@192.168.0.102::www/ --password-file=/etc/rsync.password
building file list ...
1 file to consider
deleting index.php
deleting index.html
deleting a.txt
./

sent 50 bytes  received 22 bytes  13.09 bytes/sec
total size is 0  speedup is 0.00
服务端查看/var/www/html目录下面什么都没有了
[root@localhost ~]# ll /var/www/html/
总计 0

服务器端多个目录,配置文件写法
[root@localhost ~]# vim /etc/rsyncd.conf
uid = root
gid = root
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
ignore errors
read only = false
list = false
hosts allow = 192.168.0.0/24
hosts deny = 0.0.0.0/32
auth users = rbackup
secrets file = /etc/rsync.password
[www]
path = /www
[bbs]
path = /bbs
[blog]
path = /blog

更改完成重启服务
[root@localhost ~]# pkill rsync && rsync --daemon

rsync+inotify实现触发式自动同步,inotify端创建或者一个文件rsync备份源也自动创建一个一模一样的文件,inotify删除一个文件rsync备份源也自动删除文件。

注意:inotify是部署在rsync服务成功的情况下

演示:
查看当前系统是否支持inotify
[root@localhost ~]# ls -l /proc/sys/fs/inotify/
总计 0
-rw-r--r-- 1 root root 0 12-11 10:36 max_queued_events  #监控事件队列
-rw-r--r-- 1 root root 0 12-11 10:36 max_user_instances  #最多监控实例数
-rw-r--r-- 1 root root 0 12-11 10:36 max_user_watches    #每个实例最多监控文件数

编译安装inotify
tar zxf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify-tools-3.14
make && make install

脚本

host01=10.0.0.191            #定义IP
src=/data0/www/www/          #定义源目录
dst=www                      #目标目录
user=rsync_backup            #备份的用户
rsync_passfile=/etc/rsync.password      #密码文件
inotify_home=/usr/local/inotify-tools-3.14/  #inotify的目录
#judge                                       
if [ ! -e "$src" ] \
|| [ ! -e "${rsync_passfile}" ] \
|| [ ! -e "${inotify_home}/bin/inotifywait" ] \
|| [ ! -e "/usr/bin/rsync" ];
then
  echo "Check File and Folder"              #判断上面的文件是否存在如果不存在就报错并且退出执行
  exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
| while read file        #监控的事件delete,create,attrib监控到之后通过read读取
        do
    cd $src && rsync -aruz -R --delete ./  --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
        done
exit 0

把脚本放到后台执行
[root@localhost ~]# sh inotify.sh &
[1] 8216
[root@localhost ~]# ps -ef | grep inotify
root      8216  7503  0 11:15 pts/2    00:00:00 sh inotify.sh
root      8217  8216  0 11:15 pts/2    00:00:00 /usr/local/inotify-tools-3.14//bin/inotifywait -mrq --timefmt %d/%m/%y %H:%M --format %T %w%f -e close_write,delete,create,attrib /var/www/html
root      8218  8216  0 11:15 pts/2    00:00:00 sh inotify.sh
root      8220  7503  0 11:15 pts/2    00:00:00 grep inotify

这时创建几个文件
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# touch a b c d
备份服务器查看就已经同步了
[root@localhost etc]# cd /var/www/html/
[root@localhost html]# ls
a  a.txt  b  c  d

--------------------------------------分割线 --------------------------------------

Rsync+inotify实现Git数据实时同步备份

Rsync实现文件备份同步详解

Rsync同步两台服务器

CentOS 6.5下Rsync远程同步

Ubuntu Linux下用Rsync进行数据备份和同步配制

Linux使用Rsync客户端与服务端同步目录进行备份

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

本文永久更新链接地址:

相关内容