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
motd file = /etc/rsyncd.motd                          #描述文件
    

[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
可以看到同步了一個文件。 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/
歡迎使用rsync軟件同步數據

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/
歡迎使用rsync軟件同步數據

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  540.00 bytes/sec
total size is 0  speedup is 0.00
以上的同步做的是,將客戶端在服務端沒有的文件,就是說只做的是增加文件後的同步。服務端刪除文件後不會同步。
如果要和服務端數據保證一致性,就需要使用下面的方式同步了。
測試:
使用方法3:
首先將服務端的index.html文件刪除,再在客戶端做測試。
[root@localhost-backup backup-www]# ls
index.html
[root@localhost-backup backup-www]# rsync -av --delete --password-file=/etc/rsync.pass [email protected]::www /backup-www/
歡迎使用rsync軟件同步數據

receiving incremental file list
deleting index.html    這裏顯示刪除掉index.html文件。

sent 56 bytes  received 131 bytes  374.00 bytes/sec
total size is 0  speedup is 0.00
[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/



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章