天天看点

rsync 数据同步使用

                rsync数据同步使用

使用rsync实现数据同步。

一、基于服务器--客户端方式的数据同步。

       1、server端配置

           查看软件包是否安装

            rsync-3.0.6-5.el6_0.1.x86_64

       2、开启rsync服务

            rsync不是一个独立服务,需要xinetd支持,所以确保系统已经安装了xinetd

  [root@localhost ~]# rpm -qa |grep xinetd

            xinetd-2.3.14-31.el6.x86_64

         3、  启动服务

        [root@localhost ~]# service  xinetd restart

           Stopping xinetd:                                           [  OK  ]

           Starting xinetd:                                           [  OK  ]

       4、    查看是否启动了rsync服务,rsync服务使用的端口是873 

          [root@localhost ~]#  netstat -antlp |grep 873

           tcp        0      0 :::873                      :::*                        LISTEN      7889/xinetd

     5、    因为rsync服务默认没有配置文件,所以需要手动创建配置文件

[root@localhost ~]# vim /etc/rsyncd.conf

motd file = /etc/rsyncd.motd                          #描述文件

#全局配置 

uid = nobody

gid = nobody

use chroot = no                                     #开启的话需要root用户

max connections = 4                                  #最大连接数

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

log file = /var/log/rsync.log

[www]    #模块1,可以定义多个

path = /var/www/html                         #备份的目录

comment = www server 

read only = true                                      #只读

list = false     

hosts allow = 192.168.0.0/24                     #允许的地址 

hosts deny = 0.0.0.0/32     

auth users = zhaoyun                                  #同步时用到的用户 

secrets file = /etc/rsyncd.secrets                    #用户密码文件 

[mysql]    #模块2

path = /var/lib/mysql

comment = mysql data backup

read only = true

list = false

auth users = zhao

secrets file = /etc/rsyncd.secrets

#创建用户密码文件

[root@localhost ~]# echo zhaoyun:123456 > /etc/rsyncd.secrets 

#欢迎信息

[root@localhost ~]# echo "欢迎使用rsync软件同步数据" > /etc/rsyncd.motd

修改配置文件权限,所有配置文件使用600的权限。

[root@localhost ~]# chmod 600 /etc/rsyncd.*

好了,服务端配置完成,重启服务,使配置生效。

 [root@localhost ~]# service  xinetd restart

  Stopping xinetd:                                           [  OK  ]

  Starting xinetd:                                           [  OK  ]

客户端测试:

使用方法1:

[root@localhost-backup ~]#mkdir /backup-www

[root@localhost-backup ~]# rsync -av --progress [email protected]::www /backup-www/

欢迎使用rsync软件同步数据

Password: 输入密码

receiving incremental file list

./

index.html

           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/2)

sent 78 bytes  received 192 bytes  77.14 bytes/sec

total size is 0  speedup is 0.00

[root@localhost-backup backup-www]# ls

可以看到同步了一个文件。 index.html 

使用方法2:

使用自动输入密码的方式

创建密码文件,权限需要改为600,否则不能生效。

[root@localhost-backup backup-www]# echo 123456 >/etc/rsync.pass

[root@localhost-backup backup-www]# rsync -av --progress --password-file=/etc/rsync.pass [email protected]::www /backup-www/

password file must not be other-accessible

continuing without password file               

Password: 

@ERROR: auth failed on module www

rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6]

[root@localhost-backup backup-www]# 

[root@localhost-backup backup-www]# chmod 600 /etc/rsync.pass 

[root@localhost-backup  backup-www]# rsync -av --progress --password-file=/etc/rsync.pass [email protected]::www /backup-www/

sent 78 bytes  received 192 bytes  540.00 bytes/sec

以上的同步做的是,将客户端在服务端没有的文件,就是说只做的是增加文件后的同步。服务端删除文件后不会同步。

如果要和服务端数据保证一致性,就需要使用下面的方式同步了。

测试:

使用方法3:

首先将服务端的index.html文件删除,再在客户端做测试。

[root@localhost-backup backup-www]# rsync -av --delete --password-file=/etc/rsync.pass [email protected]::www /backup-www/

receiving incremental file list 

deleting index.html    这里显示删除掉index.html文件。

sent 56 bytes  received 131 bytes  374.00 bytes/sec

[root@localhost-backup backup-www]# ls    #再查看文件已经没有了,因为在服务端已经删除了。

[root@localhost-backup  backup-www]# 

二、基于ssh同步,不用配置服务端。

 1、    创建ssh信任,不需要密码认证,创建密钥对。

使用语法

rsync -av --delete  [email protected]:/var/www/html /backup-www/

rsync -Rav [email protected]:/var/www/html /backup-www/

rsync -Rav -e 'ssh -p 2208' [email protected]:/var/www/html /backup 

三、使用任务计划实现自动同步

[root@localhost-backup backup-www]# crontab -e

*/10 * * * * rsync -az --delete  [email protected]:/var/www/html /backup-www/

本文转自zhaoyun00 51CTO博客,原文链接:http://blog.51cto.com/zhaoyun/829443

继续阅读