五步輕鬆搞定Linux下的文件同步

rsync文件同步配置

server端:主機名s170 mysql

client端:主機名s156 sql

目錄:/opt/test centos

用戶:root ssh

目標:配置rsync使得client端能夠把server的/opt/test目錄中的文件同步(備份)過來。 ide

 

操做步驟一:確認rsync是否安裝

server端和client端: 測試

 

[root@s156 ~]# yum list rsync 
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * extras: centos.ustc.edu.cn
 * updates: centos.ustc.edu.cn
Installed Packages
rsync.i386                                                                  3.0.6-4.el5_7.1                                                                   installed
[root@s156 ~]# ui

 

若是沒有安裝,則使用下面的命令安裝 加密

yum install -y rsync spa

 

 

操做步驟二:啓動rsync服務

server端: 命令行

 

[root@s170 mysql]# chkconfig rsync --list 
rsync           關閉
[root@s170 mysql]# chkconfig rsync on 
[root@s170 mysql]# chkconfig rsync --list 
rsync           啓用
[root@s170 mysql]#

 

操做步驟三:設置ssh免登陸

server端:

 

檢查server端的.ssh目錄

[root@s170 ~]# cd .ssh/ 
[root@s170 .ssh]# ls -a
.  ..  known_hosts

 

此步的目的是爲了後面確認~/.ssh目錄中文件的變化。

 

 

client端:

 

[root@s156 ~]# ssh-keygen -d 
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:
0b:9d:3e:64:77:17:6a:d8:60:1e:ab:47:42:d1:8e:51 root@s156
[root@s156 ~]#


其中,/root/.ssh/id_dsa 是私鑰,/root/.ssh/id_dsa.pub 是公鑰。

 

 

如今,將公鑰複製到server端。

[root@s156 .ssh]# ssh-copy-id -i ~/.ssh/id_dsa.pub root@s170 
21
ssh: connect to host s170 port 22: Connection refused

因爲s170機器的ssh服務端口已修改,使用默認的22端口訪問失敗。
注意:下面的命令行中指定端口的方法。
 

[root@s156 .ssh]# ssh-copy-id -i ~/.ssh/id_dsa.pub "-p 21702 root@s170" 
21
root@s170's password: 
Now try logging into the machine, with "ssh '-p 21702 root@s170'", and check in:

  .ssh/authorized_keys

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

[root@s156 .ssh]#

 

server端:

 

[root@s170 .ssh]# ls -a 
.  ..  authorized_keys   known_hosts
[root@s170 .ssh]# cat authorized_keys 
ssh-dss AAAAB3NzaC1kc3MAAACBAKUqlhhT8mEj39aKgqgziI/TqYEtaz5epS/gWVTZBmtBiY3hgYK1F+/2ItwmKI85ouIuHN4y0axzsPZv27gkYAt+Qz2AZ+QQ4IthN8cNlPQzZ1dcqtoHzFAJYpgB/HcGh2RUl/mpkHXbllOIpr281nyr+Y9LyC/qF0031A0WegyzAAAAFQDroRvSeqS9mpsJmqm0wZpNbFOSGwAAAIA4j9tGmKQ/40fpfJZ+mC5GyW+nbuL9vYHqCujX7mLku7apCq7MXqXR/s2Q4av2T68J6qR2p84obDnqbCN+H3d+7JFblLjST9WSNGMXEiPZB9lTWFV38n6exJ96rSKfeBkdOUO49MYcNX/mMpypmIHqGfpvuXbQKqWFQH7WLxVAYwAAAIBWTyQiWkkLNZkRzKjjcORtYLHss+QfqvbygXNiZSuijZ+mXyjZ9A5CxU781FsU/NmD+i6qtSr5XVyRRuigIJGWAYZ8IcD2Ct18+WPzapgO6/Ptn9I2a+6dV44MbGTtn4Rax0UaSHzWHsg+G3Kvwh9x5gcvmuoSGeamVpmcsEHjZg== root@s156
[root@s170 .ssh]#

 

注:非對稱加密方式下,公鑰是能夠公開的,而私鑰必須妥善保管!

 

 

client端:

 

測試一下免登陸ssh。

[root@s156 .ssh]# ssh -p21702 xxx.170 
Last login: Sun May 20 11:14:50 2012 from 180.168.215.167
[root@s170 ~]# 
[root@s170 ~]# ls 
anaconda-ks.cfg  backup  install.log  install.log.syslog  setup
[root@s170 ~]# exit 
logout

Connection to s170 closed.
[root@s156 .ssh]#

 

操做步驟四:使用rsync同步文件

server端:

建立測試文件

[root@s170 .ssh]# cd /opt/ 
[root@s170 opt]# mkdir test 
[root@s170 opt]# cd test 
[root@s170 test]# ls 
[root@s170 test]# cat >1.txt 
hello sync
[root@s170 test]# 
[root@s170 test]# ls -l 
總計 4
-rw-r--r-- 1 root root 11 05-20 17:10 1.txt

 


client端:

 

執行同步

 

[root@s156 .ssh]# cd /opt/ 
[root@s156 opt]# mkdir test 
[root@s156 opt]# cd test 
[root@s156 test]# ls 
[root@s156 test]# rsync -avR -e ssh s170:/opt/test /     
ssh: connect to host s170 port 22: Connection refused
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: error in rsync protocol data stream (code 12) at io.c(600) [receiver=3.0.6]
[root@s156 test]# rsync -avR -e "ssh -p 21702" s170:/opt/test /              
receiving incremental file list
opt/
opt/test/
opt/test/1.txt

sent 38 bytes  received 142 bytes  360.00 bytes/sec
total size is 11  speedup is 0.06
[root@s156 test]# ls -l 
總計 4
-rw-r--r-- 1 root root 11 05-20 17:10 1.txt

 

server端:


又在server端建立了3.txt文件。

[root@s170 test]# cat >3.txt 
ddd

ddd
[root@s170 test]# 
[root@s170 test]# ls -l 
總計 8
-rw-r--r-- 1 root root 11 05-20 17:10 1.txt
-rw-r--r-- 1 root root  9 05-20 17:21 3.txt
[root@s170 test]#

 

client端:

 

注意:此次rsync的參數少了-R,對比一下。 
[root@s156 test]# rsync -av -e "ssh -p 21702" s170:/opt/test / 
receiving incremental file list
test/
test/3.txt

sent 34 bytes  received 132 bytes  332.00 bytes/sec
total size is 20  speedup is 0.12
[root@s156 test]# ls -l 
總計 8
-rw-r--r-- 1 root root 11 05-20 17:10 1.txt

怪了,沒有3.txt。

[root@s156 test]# updatedb

[root@s156 test]# locate 3.txt 
/test/3.txt
[root@s156 test]#

若是沒有-R參數,要像下面這樣寫。

[root@s156 test]# rsync -av -e "ssh -p 21702" s170:/opt/test /opt 
receiving incremental file list
test/
test/3.txt

sent 34 bytes  received 132 bytes  332.00 bytes/sec
total size is 20  speedup is 0.12
[root@s156 test]# ls 
1.txt  3.txt
[root@s156 test]#

 

操做步驟五:使用crontab按期執行同步

使用 crontab 執行同步的最小時間粒度爲 1分鐘,也就是說最長可能要1分鐘才能將新增的文件同步。

若是是主備冗餘的方式,這問題也不大。

 

client端:

 

crontab -e

* * * * * rsync -avR -e "ssh -p 21702" s170:/opt/test /

 

如今來測試crontab設置是否有效

 

server端:

 

[root@s170 test]# cat >c.txt
hello world
[root@s170 test]#


client端:

 

[root@s156 test]# ls -l  總計 12 -rw-r--r-- 1 root root 11 05-20 17:10 1.txt -rw-r--r-- 1 root root  9 05-20 17:21 3.txt -rw-r--r-- 1 root root 12 05-20 18:22 c.txt [root@s156 test]#

相關文章
相關標籤/搜索