Centos6系統利用rsync與ssh實現文件同步

Centos6系統利用rsyncssh實現文件同步

Rsyncremote synchronize)是一個遠程數據同步工具,可通過LAN/WAN快速同步多臺主機間的文件。Rsync使用所謂的“Rsync算法”來使本地和遠程兩個主機之間的文件達到同步,這個算法只傳送兩個文件的不同部分,而不是每次都整份傳送,因此速度相當快。

Rsync本來是用於替代rcp的一個工具,目前由rsync.samba.org維護,所以rsync.conf文件的格式類似於samba的主配置文件。Rsync可以通過rshssh使用,也能以daemon模式去運行,在以daemon方式運行時Rsync server會打開一個873端口,等待客戶端去連接。連接時,Rsync server會檢查口令是否相符,若通過口令查覈,則可以開始進行文件傳輸。第一次連通完成時,會把整份文件傳輸一次,以後則就只需進行增量備份。

Rsync支持大多數的類Unix系統,無論是LinuxSolaris還是BSD上都經過了良好的測試。此外,它在windows平臺下也有相應的版本,如cwRsyncSync2NAS等工具。

 
一、安裝與啓動rsync服務
1)、下載:

10.100.10.10(服務器端)

tar xvf rsync-3.0.9.tar.gz

tar xvf rsync-patches-3.0.9.tar.gz

 
yum list rsync
Loaded plugins: fastestmirror, presto
Loading mirror speeds from cached hostfile

 * base: mirrors.163.com

 * extras: mirrors.163.com

 * updates: mirrors.163.com

Installed Packages

rsync.x86_64                3.0.6-9.el6                                  @base

yum erase –y rsync   卸載系統自帶的rsync
cd rsync-3.0.9
 ./configure --disable-debug --with-rsh=ssh --prefix=/usr/local/rsync

Make && make install

 

ln -sv /usr/local/rsync/bin/rsync /usr/bin/rsync

ln -sv /usr/local/rsync/bin/rsync /usr/sbin/rsync

 
2)爲rsync提供配置文件/etc/rsyncd.conf,內容類似如下內容:
Vim /etc/rsyncd.conf
 

# Section 1: Global settings

uid = root
gid = root
use chroot = no

max connections = 3

strict modes = yesi

port = 873

pid file = /var/run/rsync/rsyncd.pid

log file = /var/log/rsync/rsyncd.log

lock file = /var/log/rsync/rsyncd.lock

# Section 2Directory to be synced
[httpdlog]

path = /var/log/httpd/

ignore errors = yes

hosts allow = x.x.x.0/255.255.255.0 y.y.0.0/255.255.0.0 允許來同步數據的主機,多個用空格隔開

hosts deny = *
list = false

auth users = loguser

secrets file = /etc/rsync.passwd

[tomcatlogs]

path = /appsdata/apps/tomcat/logs

ignore errors = yes

hosts allow = x.x.x.0/255.255.255.0 y.y.0.0/255.255.0.0 允許來同步數據的主機,多個用空格隔開

hosts deny = *
list = false

auth users = loguser

secrets file = /etc/rsync.passwd

[nginxlog]

path = /appsdata/apps/nginx/logs

ignore errors = yes

hosts allow = x.x.x.0/255.255.255.0 y.y.0.0/255.255.0.0 允許來同步數據的主機,多個用空格隔開

hosts deny = *
list = false

auth users = loguser

secrets file = /etc/rsync.passwd

 
 

vim /etc/rsync.passwd

loguser:loguser123
 

其中冒號前的是用戶名,冒號後的是對應用戶的密碼。此文件不能爲其他任意用戶可訪問,因此可用如下命令修改:

# chmod 600 /etc/rsync.passwd
 
3)、啓動rsync服務

mkdir -pv /var/run/rsync/ /var/log/rsync /var/lock/rsync

rsync --daemon –config=/etc/rsyncd.conf netstat -tnlp|grep 873

tcp        0      0 0.0.0.0:873     0.0.0.0:*         LISTEN      26635/rsync        

tcp        0      0 :::873          :::*             LISTEN      26635/rsync 

 
二、配置雙機互信(root用戶):

1)、開啓root用戶ssh認證權限

首先由於要運行的rsyncroot用戶,有通過ssh做互信的權限。

vim /etc/ssh/sshd_config

PermitRootLogin yes 將其註釋去掉

 
2)、在 10.10上:
cd /root
mkdir .ssh chmod 755 .ssh

ssh-keygen -t rsa

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:

b5:02:63:6f:93:30:a0:f2:a6:e6:e0:a1:05:f3:23:79 root@idc010vm010

The key's randomart image is:

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

|    .            |

|   . .           |

|. .   *   .      |

| o   . * o .     |

|o o     S .      |

| B     . o       |

|=oE              |
|*+..             |
|.o               |
+-----------------+

cat .ssh/id_rsa.pub >> authorized_keys

scp authorized_keys [email protected]:/root/.ssh 要在12服務器mkdir .ssh 之後進行,

輸入12機器的root用戶密碼即可。
 
3)、在10.12 上:
mkdir .ssh
chmod 750 .ssh

ssh-keygen -t rsa

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:

5b:fd:ab:b1:bd:10:5a:8d:2d:f1:77:78:4b:1f:8b:2b root@idc010vm012

The key's randomart image is:

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

|                 |
|                 |

|            .    |

|           . * . |
|        S . * =o+|
|         o o +oo*|
|        . . o..o.|
|           E =.. |
|            +o+. |
+-----------------+

cat .ssh/id_rsa.pub >> authorized_keys

scp authorized_keys [email protected]:/root/.ssh 輸入10機器的root用戶密碼即可。

 
4)、結果實現:
[root@idc010vm012 ~]# ssh 10.100.10.10

Warning: Permanently added '10.100.10.10' (RSA) to the list of known hosts.

Last login: Wed Sep 12 15:17:03 2012 from idc010vm012.idc.lecast

[root@idc010vm010 ~]# 
 
三、配置雙機互信(普通用戶):
1)、10.10
useradd loguser
passwd loguser
 
su loguser
mkdir .ssh chmod 755 .ssh

ssh-keygen –t rsa

Generating public/private rsa key pair.

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

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /home/loguser/.ssh/id_rsa.

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

The key fingerprint is:

5d:3c:e7:af:8c:5a:60:13:17:a9:87:e8:98:8b:9e:ed loguser@idc010vm010

The key's randomart image is:

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

|            ..   |

|           ...   |

|         ..o= . |

|        ..o+.+   |

|       +S =. . |

|      o .. o   . |

|     . .    .   .|

|    .o.    . o . |

|   .o.E   ... o |

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

cp /home/loguser/.ssh/id_rsa.pub /home/loguser/authorized_keys

scp /home/loguser/authorized_keys [email protected]:/home/loguser/.ssh/

輸入密碼

chmod –R 600 .ssh/*

 
2)、10.12上:
useradd loguser
passwd loguser
 
su loguser
mkdir .ssh chmod 755 .ssh

ssh-keygen –t rsa

Generating public/private rsa key pair.

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

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /home/loguser/.ssh/id_rsa.

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

The key fingerprint is:

c4:95:5a:5a:89:93:34:45:e3:9f:d2:f4:79:86:89:4c loguser@idc010vm012

The key's randomart image is:

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

|       .o==o     |

|       .++=.     |

|        o*. E    |

|       .o * + + |
|        S . * = o|

|           .   o |

|                 |
|                 |
|                 |
+-----------------+
 

cp /home/loguser/.ssh/id_rsa.pub /home/loguser/authorized_keys

scp /home/loguser/authorized_keys [email protected]:/home/loguser/.ssh/

輸入密碼

chmod –R 600 .ssh/*

 
3)、測試:
[loguser@idc010vm012 ~]$ ssh 10.100.10.10 date

Warning: Permanently added '10.100.10.10' (RSA) to the list of known hosts.

Wed Sep 12 17:11:16 CST 2012
 
4)、拷貝rsync的配置文件:
scp 10.100.10.10:/etc/rsyncd.conf /etc/ 普通用戶是沒有這個權限的,會報錯。

scp 10.100.10.10:/etc/rsyncd.conf ./      放在家目錄下,隨後用root用戶cp/etc下。

Warning: Permanently added '10.100.10.10' (RSA) to the list of known hosts.

rsyncd.conf                   100% 905     0.9KB/s   00:00               

 
創建密碼文件:
echo "loguser123" > /etc/rsync.passwf 客戶端的rsync密碼文件只能存放密碼,而不要用戶名。
 
5)、使用rsync拷貝文件試試:

[loguser@idc010vm012 .ssh]$ rsync -vzrtopg --progress --stats -e ssh /opt/GeoDB.dat [email protected]:/tmp/cmake

Warning: Permanently added '10.100.10.10' (RSA) to the list of known hosts.

sending incremental file list

GeoDB.dat

     4333568 100%   11.52MB/s    0:00:00 (xfer#1, to-check=0/1)

 

Number of files: 1

Number of files transferred: 1

Total file size: 4333568 bytes

Total transferred file size: 4333568 bytes

Literal data: 4333568 bytes

Matched data: 0 bytes

File list size: 27

File list generation time: 0.001 seconds

File list transfer time: 0.000 seconds

Total bytes sent: 554522

Total bytes received: 31

 

sent 554522 bytes received 31 bytes 369702.00 bytes/sec

total size is 4333568 speedup is 7.81

 

 [root@idc010vm010 tmp]# ll cmake

-rw-r--r--. 1 loguser loguser 4333568 Sep 1 17:08 cmake 
 

四、Rsync的常用參數描述:

-v, --verbose 詳細模式輸出
-a, --archive 歸檔模式,表示以遞歸方式傳輸文件,並保持所有文件屬性,等於-rlptgoD
-r, --recursive 對子目錄以遞歸模式處理
-b, --backup 創建備份,也就是對於目的已經存在有同樣的文件名時,將老的文件重新命名爲~filename。可以使用--suffix選項來指定不同的備份文件前綴。

--backup-dir 將備份文件(~filename)存放在在目錄下。

-u, --update 僅僅進行更新,也就是跳過所有已經存在於DST,並且文件時間晚於要備份的文件。(不覆蓋更新的文件)
-l, --links 保留軟鏈結

-L, --copy-links 想對待常規文件一樣處理軟鏈結

--copy-unsafe-links 僅僅拷貝指向SRC路徑目錄樹以外的鏈結

--safe-links 忽略指向SRC路徑目錄樹以外的鏈結

-H, --hard-links 保留硬鏈結

-p, --perms 保持文件權限-o, --owner 保持文件屬主信息
-g, --group 保持文件屬組信息
-D, --devices 保持設備文件信息
-t, --times 保持文件時間信息
-n, --dry-run現實哪些文件將被傳輸

-W, --whole-file 拷貝文件,不進行增量檢測

-x, --one-file-system 不要跨越文件系統邊界

-B, --block-size=SIZE 檢驗算法使用的塊尺寸,默認是700字節

-e, --rsh=COMMAND 指定替代rshshell程序

--rsync-path=PATH 指定遠程服務器上的rsync命令所在路徑信息

-C, --cvs-exclude 使用和CVS一樣的方法自動忽略文件,用來排除那些不希望傳輸的文件

--existing 僅僅更新那些已經存在於DST的文件,而不備份那些新創建的文件

--delete 刪除那些DSTSRC沒有的文件

--delete-excluded 同樣刪除接收端那些被該選項指定排除的文件

--delete-after 傳輸結束以後再刪除

--ignore-errors 即使出現IO錯誤也進行刪除

--max-delete=NUM 最多刪除NUM個文件

--partial 保留那些因故沒有完全傳輸的文件,以是加快隨後的再次傳輸
--force 強制刪除目錄,即使不爲空

--numeric-ids 不將數字的用戶和組ID匹配爲用戶名和組名
--timeout=TIME IP
超時時間,單位爲秒

-I, --ignore-times 不跳過那些有同樣的時間和長度的文件

-z, --compress 對備份的文件在傳輸時進行壓縮處理

--exclude=PATTERN 指定排除不需要傳輸的文件模式

--include=PATTERN 指定不排除而需要傳輸的文件模式

--exclude-from=FILE 排除FILE中指定模式的文件

--include-from=FILE 不排除FILE指定模式匹配的文件

--version 打印版本信息
--config=FILE 指定其他的配置文件,不使用默認的rsyncd.conf文件
--port=PORT 指定其他的rsync服務端口
--stats 給出某些文件的傳輸狀態
--progress 在傳輸時現實傳輸過程

--log-format=formAT 指定日誌文件格式

--password-file=FILE FILE中得到密碼

--bwlimit=KBPS 限制I/O帶寬,KBytes per second

 

另外要注意最後的/ 有沒有是不一樣的:

如:

[loguser@idc010vm010 ~]$ rsync --list-only -e ssh 10.100.10.12:/var/log

Warning: Permanently added '10.100.10.12' (RSA) to the list of known hosts.

drwxr-xr-x        4096 2012/09/12 09:43:30 log
 

[loguser@idc010vm010 ~]$ rsync --list-only -e ssh 10.100.10.12:/var/log/

Warning: Permanently added '10.100.10.12' (RSA) to the list of known hosts.

drwxr-xr-x        4096 2012/09/12 09:43:30 .

-rw-------        5311 2012/07/12 16:17:29 anaconda.ifcfg.log

-rw-------       20736 2012/07/12 16:17:29 anaconda.log

-rw-------       37319 2012/07/12 16:17:29 anaconda.program.log

-rw-------      299403 2012/07/12 16:17:29 anaconda.storage.log

-rw-------       41263 2012/07/12 16:17:29 anaconda.syslog

-rw-------       33926 2012/07/12 16:17:29 anaconda.xlog

-rw-------        4596 2012/07/12 16:17:29 anaconda.yum.log

-rw-r--r--        2546 2012/09/10 18:15:43 boot.log
-rw-------        4224 2012/09/12 16:15:24 btmp
-rw-------       28071 2012/09/12 17:01:01 cron
-rw-------       21953 2012/09/09 03:18:01 cron-20120909
-rw-r--r--       18399 2012/09/10 18:15:20 dmesg
-rw-r--r--       18399 2012/09/06 09:32:09 dmesg.old
-rw-r--r--      129544 2012/07/12 16:17:24 dracut.log
-rw-r--r--      146292 2012/09/12 16:55:45 lastlog

-rw-r--r--     3142485 2012/09/11 16:43:10 localhost_access_log.2012-07-23.txt

-rw-r--r--     3142485 2012/09/11 16:06:36 localhost_access_log.2012-09-11.txt

 
五、rsync同步的模式
1)、增量備份

rsync -av /src_dir/ user@IP_Addres:/dest_dir/

        A-->B 同步本地到遠端

rsync -av user@IP_Addres:/src_dir/ /dest_dir/

        B-->A 同步遠端到本地

2)、鏡像備份
rsync -av --delete /src_dir/ user@IP_Addres:/dest_dir/

        A-->B 同步本地到遠端

rsync -av --delete user@IP_Addres:/src_dir/ /dest_dir/

        B-->A 同步遠端到本地

在日常的使用中,可以將rsync同步的事件寫在crontab 任務裏或者先寫至腳本里,然後定期執行,減輕了維護人員手動操作的負擔。

 

 

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