rsync實現兩臺服務器上的實時同步

rsync實現兩臺服務器上的實時同步--delete

服務端:centos-4 192.168.5.128shell

客戶端:centos-3 192.168.5.132vim

 

一、建立{1..10}.txt文件在a目錄下

[root@centos-4 tmp]# rm -rf acentos

[root@centos-4 tmp]# mkdir abash

[root@centos-4 tmp]# cd a服務器

[root@centos-4 a]# touch {1..10}.txtdom

[root@centos-4 a]# lsssh

10.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txtide

 

2、同步到遠端/tmp/a目錄下

由於實在實現不了--password-file的方式同步就作了一個免密碼登陸測試

[root@centos-4 a]# ssh-keygen spa

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

68:05:51:21:2f:e5:b8:a5:2a:99:5f:23:1a:28:f5:9b root@centos-4

The key's randomart p_w_picpath is:

+--[ RSA 2048]----+

|      +o+.       |

|       B         |

|      o =        |

|       B         |

|  .   = S        |

| o + o           |

|o = + o          |

|.  = = .         |

|  . E            |

+-----------------+

[root@centos-4 a]# ssh-copy-id centos-3

root@centos-3's password:

Now try logging into the machine, with "ssh 'centos-3'", and check in:

 

  .ssh/authorized_keys

 

to make sure we haven't added extra keys that you weren't expecting.

 

[root@centos-4 a]#

 

配置完私鑰後.ssh/下會多了一個authorized_keys 文件

[root@centos-3 .ssh]# cat authorized_keys

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA1vNA0sHBa4FFnMINAj6FM+FGInAP2+eIQ1JSIXJeRWOfM0KyLS3Nn5LzxvENBPrjJs1RR2x4Lhnuy0YL0Qy+wZLdcEIufZWcpRjDkLAhvgQjF7Mivd7mFhfPJ/uKUB/7Di4V/GzaW2SaG4ZWafFyB8igsBqnfw4wu0NyCVAjlpSe6tR1jao5MRMp8+6nksYHyHH8wafdGglF67cusk5fk9Kd0c4rSoiTjUX4EsREuE7mRBnK6UsSjvnJSsIHKYzDwEdB9ix+flTd9znPgEukWO85WN0h4wjqM8BAURRLDQnxKa0b9m+TrYsJnjr5xemj/Gr3q905ZWdoIDsupDNsLw== root@centos-4

 

同步兩臺服務器/tmp/a目錄

[root@centos-4 a]# rsync -avpz /tmp/a root@centos-3:/tmp/

sending incremental file list

a/

a/1.txt

a/10.txt

a/2.txt

a/3.txt

a/4.txt

a/5.txt

a/6.txt

a/7.txt

a/8.txt

a/9.txt

 

sent 513 bytes  received 206 bytes  479.33 bytes/sec

total size is 0  speedup is 0.00

 

查看客戶端的centos-3服務器/tmp/a已經同步

[root@centos-3 tmp]# ls

a                pear                                   zabbix_agentd.log

cml              pulse-gvsEPtT6wF1A                     zabbix_agentd.pid

gconfd-root      pulse-ofsu0uqNK8ia                     zabbix_server.log

hsperfdata_root  sess_9262c75f9c8625064dfabb6f1ffe9b1b  zabbix_server.pid

keyring-8nmh6u   sess_9pdslag3gffnplr3oh0mucvrr6

keyring-c6WZng   yum.log

[root@centos-3 tmp]# cd a

[root@centos-3 a]# ls

10.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

[root@centos-3 a]#

 

 

3、刪除1.txt文件

[root@centos-4 a]# ls

10.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

[root@centos-4 a]# rm -rf 1.txt

[root@centos-4 a]# ls

10.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

[root@centos-4 a]#

 

4、後面測試,當本地主機少了一個文件。而遠端主機沒有這個文件,刪除遠端主機的文件。

[root@centos-4 a]# rsync -avpz --delete /tmp/a root@centos-3:/tmp/

sending incremental file list

a/

deleting a/1.txt##顯示刪除了1.txt同步兩臺服務器的目錄

 

sent 150 bytes  received 16 bytes  332.00 bytes/sec

total size is 0  speedup is 0.00

[root@centos-4 a]#

 

檢查客戶端centos-3的目錄已經少了1.txt文件,表明已經同步成功

[root@centos-3 a]# ls

10.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

[root@centos-3 a]#

五、每過1min監聽這些目錄,當發現本地和遠程不一致,就再次同步

寫一個shell的腳本執行同步兩邊的/tmp/a目錄

[root@centos-4 ~]# vim rsync.sh

#!/bin/bash

 

rsync -avpz --delete /tmp/a root@centos-3:/tmp/

 

[root@centos-4 ~]# chmod a+x rsync.sh

測試腳本:

[root@centos-4 ~]# cd /tmp/a

[root@centos-4 a]# ls

10.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

[root@centos-4 a]# rm -rf 2.txt

[root@centos-4 a]# ls

10.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

[root@centos-4 a]# cd

[root@centos-4 ~]# ./rsync.sh

sending incremental file list

a/

deleting a/2.txt##表示腳本已經生效

 

sent 139 bytes  received 16 bytes  310.00 bytes/sec

total size is 0  speedup is 0.00

[root@centos-4 ~]#

這樣作執行腳本是仍是會輸出信息,因此改了一下

[root@centos-4 ~]# vim rsync.sh

#!/bin/bash

 

rsync -avpz --delete /tmp/a root@centos-3:/tmp/  >>  /dev/null

爲了實現實時同步服務端和客戶端的/tmp/a目錄,配置每1min執行一次命令

[root@centos-4 ~]# crontab -e

 

*/1 * * * * rsync -avpz --delete /tmp/a root@centos-3:/tmp/ >> /dev/null

 

 

crontab: installing new crontab

測試1min後是否能夠實時同步目錄,刪除3.txt

[root@centos-4 a]# rm -rf  3.txt/

[root@centos-4 a]# ls

10.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

 

過了一分鐘後查看centos-3/tmp/a目錄下3.txt有沒有刪除,最後同步

[root@centos-3 a]# ls

10.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

[root@centos-3 a]#

相關文章
相關標籤/搜索