數據同步

數據同步在不少地方都會用到,再次和你們分享一下利用ssh和rsync來進行同步的過程。 

配置SSH

這裏配置的SSH服務器指的修改SSH的默認端口,以及配置SSH的密鑰認證。這裏以配置centos5.5系統爲例。
首先須要兩臺服務器,一臺備份目標服務器也就是rsync的目標服務器,一臺源服務器。
配置ssh服務,爲了不每次同的時候輸入密碼,要配置ssh的密鑰和公鑰。
Centos linux通常默認的就安裝ssh服務,而且已經啓動。默認就可使用(這一點debain和Centos有所不一樣)。
先來看一看ssh密鑰登陸的一個簡單解釋:
SSH 告訴遠程 Server 端的 sshd ,它想使用 RSA 認證協議…遠程的 sshd 會生成一個隨機數,並用咱們先前拷貝過去的公鑰對這個隨機數進行加密。而後, sshd 把加密了的隨機數發回給正在 本地服務器上運行的 ssh 。接下來,咱們的 ssh 用 專用密鑰對這個隨機數進行解密後,再把它發回給遠程服務器,相似於聲明一下:「瞧,我真的有匹配的專用密鑰;我能成功的對您的消息進行解密!」最後, 遠程 sshd 得出結論,既然人家真的有該專用密鑰,就應當讓人家登陸。所以,咱們有匹配的專用密鑰這一事實受權咱們訪問遠程服務器 。
製做公鑰和私鑰
root@jadeshow ~]# ssh-keygen -t dsa    

Generating public/private dsa key pair.

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

Enter passphrase (empty for no passphrase):    

Enter same passphrase again:    

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

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

The key fingerprint is:

90:cc:bc:e3:99:28:95:60:89:22:c5:07:be:51:32:c5 root@jadeshow.com

[root@jadeshow ~]# ifconfig    

eth0            Link encap:Ethernet    HWaddr 00:0C:29:4B:9F:D3    

                    inet addr:192.168.1.107    Bcast:255.255.255.255    Mask:255.255.255.0

                    inet6 addr: fe80::20c:29ff:fe4b:9fd3/64 Scope:Link

                    UP BROADCAST RUNNING MULTICAST    MTU:1500    Metric:1

                    RX packets:582 errors:0 dropped:0 overruns:0 frame:0

                    TX packets:311 errors:0 dropped:0 overruns:0 carrier:0

                    collisions:0 txqueuelen:1000    

                    RX bytes:65280 (63.7 KiB)    TX bytes:40116 (39.1 KiB)

                    Interrupt:59 Base address:0x2000    
拷貝公鑰到目標服務器,
目標服務器基本信息:
 

           
           
           
           
[root@web ~]# ip a|grep eth0

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000

inet 192.168.1.106/24 brd 255.255.255.255 scope global eth0

[root@web~]#scp root@192.168.1.107:/root/.ssh/id_dsa.pub /root/.ssh/authorized_keys

id_dsa.pub                                                                        100%    607         0.6KB/s     00:00        

[root@web ~]# ls ~/.ssh/

authorized_keys    id_rsa    known_hosts

切換到源服務器,驗證免登陸設置是否成功。
 
            
[root@jadeshow ~]# ssh 192.168.1.106    

Last login: Sun Jun 20 22:30:54 2010 from jadeshow

[root@jadeshow ~]# ssh 192.168.1.106

Last login: Sun Jun 20 22:31:29 2010 from jadeshow

[root@web ~]# ifconfig    

eth0            Link encap:Ethernet    HWaddr 00:0C:29:00:F0:ED    

                    inet addr:192.168.1.106    Bcast:255.255.255.255    Mask:255.255.255.0
此處免驗證登陸成功,表現形式爲不須要輸入密碼。咱們在看下須要輸入密碼登陸的。
 
            
[root@web ~]# ssh 192.168.1.107        

root@192.168.1.107's password:     此處須要輸入密碼

Last login: Sun Jun 20 22:34:37 2010 from 192.168.1.106

[root@jadeshow ~]#
此外還能夠經過修改open-ssh server的配置文件指定特定端口 短線時常 等等,可經過查看man sshd_config來肯定。

配置rsync

rsync的用法是 rsync [選項]  源 目標 
假設我要把主機名爲jadeshow下root的家目錄同步到主機名爲web的家目錄下的backup目錄。

           
           
           
           
root@jadeshow .ssh]# rsync -av    ~/ -e ssh 192.168.1.106:~/backup

#-v 詳細輸出模式 -a, --archive 歸檔模式,表示以遞歸方式傳輸文件,並保持全部文件屬性    -e 調用 ssh

building file list ... done

./

.bash_history

.bash_logout

.bash_profile

.bashrc

.cshrc

.tcshrc

.viminfo

anaconda-ks.cfg

install.log

install.log.syslog

.gconf/

.gconf/apps/

.gconf/apps/%gconf.xml

.gconf/apps/gnome-session/

.gconf/apps/gnome-session/%gconf.xml

.gconf/apps/gnome-session/options/

.gconf/apps/gnome-session/options/%gconf.xml

.gconfd/

.gconfd/saved_state

.ssh/

.ssh/id_dsa

.ssh/id_dsa.pub

.ssh/id_rsa

.ssh/known_hosts

backup/

python/

python/fulijisuanqi.py

python/python.py

sent 35361 bytes    received 514 bytes    23916.67 bytes/sec

total size is 33739    speedup is 0.94

 
             
人工對比源和目標的文件數目

                
                
                
                
[root@jadeshow ~]# ls

anaconda-ks.cfg    backup    install.log    install.log.syslog    python

[root@web ~]# ls backup/

anaconda-ks.cfg    backup    install.log    install.log.syslog    python

總結

本次的的文已經到這裏就結束了,不過通常在生產環境中 備份的文件通常會被按時間順序打成包,而且利用腳本和crontab定時任務來實現特定文件的定時備份。
(菜鳥寫博,老鳥飛過。歡迎拍磚,深刻交流!)

多吃東西多喝水, 

少玩遊戲少睡覺! -------珏石頭
相關文章
相關標籤/搜索