rsync实现客户端自动与服务器同步数据详细过程(原创)

来源:互联网 发布:淘宝网店数据分析 编辑:程序博客网 时间:2024/05/01 10:37

rsync实现客户端自动与服务器同步数据详细过程(原创)

作者:东风   测试时间:2014年8月8日15:48:25    地点:上海浦东

1、rsync知识,
    rsync:remote synchronize顾名思意就知道它是一款实现远程同步功能的软件,它在同步文件的同时,可以保持原来文件的权限、时间、软硬链接等附加信息。rsync是用 “rsync 算法”提供了一个客户机和远程文件服务器的文件同步的快速方法,而且可以通过ssh方式来传输文件,这样其保密性也非常好,另外它还是免费的软件。

  rsync 包括如下的一些特性:

  能更新整个目录和树和文件系统;
  有选择性的保持符号链链、硬链接、文件属于、权限、设备以及时间等;
  对于安装来说,无任何特殊权限要求;
  对于多个文件来说,内部流水线减少文件等待的延时;
  能用rsh、ssh 或直接端口做为传输入端口;
  支持匿名rsync 同步文件,是理想的镜像工具;

2、安装rsync软件;

    配置好yum源,配置yum源具体过程略过。yum -y install rsync 安装即可。

3、服务器端配置

     rsync需要配置三个文件:

     rsyncd.conf(主配置文件)、 该文件默认不存在,需要手动创建;

     rsyncd.secrets(密码文件)、用户密码文件,文件属性要设置root, 且权限要设为600, 否则无法备份成功!

     rsyncd.motd(rysnc服务器信息) 、可以不用配置,只是提示信息而已。

具体过程如下:

a.创建配置文件

[root@ogg1 ~]# vi /etc/rsyncd.conf  添加一下内容:

pid file = /var/run/rsyncd.pid
port = 873
address = 10.15.72.230
uid = root
gid = root

use chroot = yes
read only = yes

#limit access to private LANs
hosts allow=192.168.56.0/24 10.15.72.0/24
hosts deny=*

max connections = 5
motd file = /etc/rsyncd.motd

#This will give you a separate log file
#log file = /var/log/rsync.log


#This will log every file transferred - up to 85,000+ per user, per sync
#transfer logging = yes

log format = %t %a %m %f %b
syslog facility = local3
timeout = 300

[rootdata]
path = /root
list=yes
ignore errors
auth users = root
secrets file = /etc/rsyncd.secrets
comment = This is root user data
exclude = oracle/
 

详解:我们需要将服务器(ip:10.15.72.230)的/root文件 同步到客户端,但是将/root/oracle 文件夹排除在外,不用同步;

pid file = /var/run/rsyncd.pid    #告诉进程写到 /var/run/rsyncd.pid 文件中;
port = 873                        #指定运行端口,默认是873,您可以自己指定;
address = 10.15.72.230            #指定服务器IP地址
uid = root
gid = root
注:服务器端传输文件时,要发哪个用户和用户组来执行,默认是nobody。 如果用nobody 用户和用户组,可能遇到权限问题,有些文件从服务器上拉不下来。所以我就偷懒,为了方便,用了root 。不过您可以在定义要同步的目录时定义的模块中指定用户来解决权限的问题。
use chroot = yes
注:用chroot,在传输文件之前,服务器守护程序在将chroot 到文件系统中的目录中,这样做的好处是可能保护系统被安装漏洞侵袭的可能。缺点是需要超级用户权限。另外对符号链接文件,将会排除在外。也就是说,你在 rsync服务器上,如果有符号链接,你在备份服务器上运行客户端的同步数据时,只会把符号链接名同步下来,并不会同步符号链接的内容;这个需要自己来尝 试
read only = yes
注:read only 是只读选择,也就是说,不让客户端上传文件到服务器上。还有一个 write only选项,自己尝试是做什么用的吧;

#limit access to private LANs
hosts allow=192.168.56.0/24 10.15.72.0/24  
hosts deny=*
# 在您可以指定单个IP,也可以指定整个网段,能提高安全性。格式是ip 与ip 之间、ip和网段之间、网段和网段之间要用空格隔开;

max connections = 5   注:客户端最多连接数
motd file = /etc/rsyncd.motd
注:motd file 是定义服务器信息的,要自己写 rsyncd.motd 文件内容。当用户登录时会看到这个信息。

#This will give you a separate log file
log file = /var/log/rsync.log  注:rsync 服务器的日志;

#This will log every file transferred - up to 85,000+ per user, per sync
transfer logging = yes      注:这是传输文件的日志

log format = %t %a %m %f %b  注:日志格式
syslog facility = local3 
timeout = 300   注:连接超时

[rootdata]   #需要同步名称
path = /root   #指定文件目录所在位置,这是必须指定的 
list=yes     #list 意思是把rsync 服务器上提供同步数据的目录在服务器上模块是否显示列出来。默认是yes 。
ignore errors   #忽略IO错误 
auth users = root    #认证用户是root  ,是必须在服务器上存在的用户
secrets file = /etc/rsyncd.secrets   #密码存在哪个文件
comment = This is root user data   #注释可以自己定义
exclude = oracle/  #需要排除的文件,不需要同步;

当然,也可以不用添加那么多,有些保持默认即可:

# This line is required by the /etc/init.d/rsyncd script
pid file = /var/run/rsyncd.pid

uid=root
gid=root
read only = true
use chroot = true
transfer logging = true
max connections = 3
log format = %h %o %f %l %b
log file = /var/log/rsyncd.log
hosts allow = 10.15.72.0/24
auth users = root
secrets file =/etc/rsyncd.secrets
motd file=/etc/rsyncd.motd

[rootdata]
path = /root


b.创建密码文件

[root@ogg1 ~]# vi /etc/rsyncd.secrets  添加如下:(用户名:密码)

root:root123

[root@ogg1 ~]#chown root:root  /etc/rsyncd.secrets  

[root@ogg1 ~]#chmod 600 /etc/rsyncd.secrets   

注:将rsyncd.secrets这个密码文件的文件属性设为root拥有, 且权限要设为600, 否则无法备份成功!


c.创建提示文件(只是提示性语句,可有可无)

[root@ogg1 ~]# vi /etc/rsyncd.motd  添加如下:

-------------------------------------------
this is the data of root user to sync.
------------------------------------------


d.启动rsync服务器及防火墙的设置

在这里我使用xinet 方式,如下:

[root@ogg1 ~]# vi /etc/services 添加如下:

  rsync  873/tcp  # rsync 
  rsync  873/udp  # rsync

这一步一般可以不做,通常都有这两行(我的RHEL4和GENTOO默认都有)。修改的目的是让系统知道873端口对应的服务名为rsync。如没有的话就自行加入。

  设定 /etc/xinetd.d/rsync, 简单例子如下:

[root@ogg1 ~]# vi /etc/xinetd.d/rsync ,将disable = no 即可;

  # default: off
  # description: The rsync server is a good addition to am ftp server, as it \
  #       allows crc checksumming etc.
  service rsync
  {
        disable = no
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
  }

注:这里主要是要打开rsync这个daemon, 一旦有rsync client要连接, xinetd会把它转介给 rsyncd(port 873)。然后service xinetd restart, 使上述设定生效.

[root@ogg1 ~]# service xinetd restart
Stopping xinetd:                                           [  OK  ]
Starting xinetd:                                           [  OK  ]

 rsync服务器防火墙设置

  Linux 防火墙是用iptables,所以我们至少在服务器端要让你所定义的rsync 服务器端口通过,客户端上也应该让通过。

  [root@ogg1 ~]#iptables -A INPUT -p tcp -m state --state NEW  -m tcp --dport 873 -j ACCEPT
  [root@ogg1 ~]#iptables -L  查看一下防火墙是不是打开了 873端口

  如果不懂,直接放防火墙关闭即可;

[root@ogg1 ~]# service iptables stop 

[root@ogg1 ~]# chkconfig iptables off

[root@ogg1 ~]# service iptables status
Firewall is stopped.


4、客户端配置

A、语法详解
   在配置完rsync服务器后,就可以从客户端发出rsync命令来实现各种同步的操作。rsync有很多功能选项,下面就对介绍一下常用的选项:

  rsync的命令格式可以为:
  1. rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST 
  2. rsync [OPTION]... [USER@]HOST:SRC DEST 
  3. rsync [OPTION]... SRC [SRC]... DEST 
  4. rsync [OPTION]... [USER@]HOST::SRC [DEST] 
  5. rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST 
  6. rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]

  rsync有六种不同的工作模式:

  1. 拷贝本地文件;当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。
  2.使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包含单个冒号":"分隔符时启动该模式。
  3.使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径包含单个冒号":"分隔符时启动该模式。
  4. 从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含"::"分隔符时启动该模式。
  5. 从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含"::"分隔符时启动该模式。
  6. 列远程机的文件列表。这类似于rsync传输,不过只要在命令中省略掉本地机信息即可。
  -a 以archive模式操作、复制目录、符号连接 相当于-rlptgoD

  rsync中的参数

  -r 是递归 
  -l 是链接文件,意思是拷贝链接文件;-p 表示保持文件原有权限;-t 保持文件原有时间;-g 保持文件原有用户组;-o 保持文件原有属主;-D 相当于块设备文件;
  -z 传输时压缩;
  -P 传输进度;
  -v 传输时的进度等信息,和-P有点关系,自己试试。可以看文档;
  -e ssh的参数建立起加密的连接。
  -u只进行更新,防止本地新文件被重写,注意两者机器的时钟的同时
  --progress是指显示出详细的进度情况
  --delete是指如果服务器端删除了这一文件,那么客户端也相应把文件删除,保持真正的一致
  --password-file=/password/path/file来指定密码文件,这样就可以在脚本中使用而无需交互式地输入验证密码了,这里需要注意的是这份密码文件权限属性要设得只有属主可读。

B、一些例子

  B1、列出rsync 服务器上的所提供的同步内容;

[root@finchina-cs ~]# rsync --list-only root@10.15.72.230::
-----------------------------------------------------------
this is the root user data
-----------------------------------------------------------

rootdata       This is root user data

    B2、列出rsync 服务器上rootdata目录;

[root@finchina-cs ~]# rsync --list-only root@10.15.72.230::rootdata
-----------------------------------------------------------
this is the root user data
-----------------------------------------------------------


Password: (这里输入密码)
drwxr-x---        4096 2014/08/08 10:10:16 .
-rw-------         934 2014/08/08 10:10:16 .ICEauthority
-rw-------        7054 2014/08/08 09:43:58 .bash_history
-rw-r--r--          24 2006/07/13 08:06:42 .bash_logout
-rw-r--r--         191 2006/07/13 08:06:42 .bash_profile
-rw-r--r--         176 2006/07/13 08:06:42 .bashrc
-rw-r--r--         100 2006/07/13 08:06:42 .cshrc
-rw-------          26 2014/07/07 17:12:39 .dmrc

 B3、rsync客户端手动同步数据(将数据同步到客户端的/root/peng目录下)

[root@finchina-cs ~]# rsync -avz --progress --delete --password-file=/etc/rsync/pwd.txt root@10.15.72.230::rootdata /root/peng
-----------------------------------------------------------
this is the root user data
-----------------------------------------------------------

receiving incremental file list
.gconf/apps/puplet/
.gconf/apps/puplet/%gconf.xml
         245 100%  239.26kB/s    0:00:00 (xfer#1, to-check=50/105)

sent 134 bytes  received 3145 bytes  6558.00 bytes/sec
total size is 321580  speedup is 98.07

注:

--progress是指显示出详细的进度情况

--delete是指如果服务器端删除了这一文件,那么客户端也相应把文件删除,保持真正的一致

--password-file=/etc/rsync/pwd.txt来指定密码文,

这里没有提示输入密码,是因为使用参数--password-file参数,并在客户端添加了密码文件:

[root@finchina-cs ~]# mkdir /etc/rsync/

[root@ogg1-client ~]# vi /etc/rsync/pwd.txt 添加如下:

root123

[root@ogg1-client ~]#chown root:root /etc/rsync/pwd.txt

[root@ogg1-client ~]#chmod 600 /etc/rsync/pwd.txt


5、客户端自动与服务器同步数据

5.1 客户端添加如下:

[root@ogg1-client rsync]# vi /etc/rsync/rootdata.sh 添加如下:

#!/bin/sh
#backup the server data: ip:10.15.72.230 hostname:ogg1 trace:/root
#the client trace:ip:10.15.72.253 hostname:ogg1-client  trace:/root/peng

rsync -avz --progress --delete --password-file=/etc/rsync/pwd.txt root@10.15.72.230::rootdata /root/peng

注:/etc/rsync/pwd.txt文件之前步骤已经创建,直接利用(注意属性,权限)。

[root@ogg1-client rsync]# chmod 755 /etc/rsync/rootdata.sh 


5.2 添加执行计划:

[root@ogg1-client rsync]# crontab -l 添加如下:

#backup rootdata
10 4 * * * /etc/rsync/rootdata.sh

注:表示在每天早上4点10分的时候运行同步脚本/etc/rsync/rootdata.sh;

10 4  * * * /etc/rsync/rootdata.sh 表示:

第1列分钟1~5 9 
第2列小时1~2 3(0表示子夜) 
第3列日1~3 1 
第4列月1~1 2 
第5列星期0~6(0表示星期天) 
第6列要运行的命令 

OK! 完结!!!

文章引用:

http://www.cnblogs.com/itech/archive/2009/08/10/1542945.html 

点击打开链接
























0 0
原创粉丝点击