linux下的數據備份工具 rsync(remote sync 遠程同步)linux
名詞解釋:安全
sync(Synchronize,即「同步」)爲UNIX操做系統的標準系統調用,功能爲將內核文件系統緩衝區的全部數據(也即預約將經過低級I/O系統調用寫入存儲介質的數據)寫入存儲介質(如硬盤)。
bash
sync是一個linux同步命令,含義爲迫使緩衝塊數據當即寫盤並更新超級塊。在linux系統中,爲了加快數據的讀取速度,默認狀況下,某些數據將不會直接寫入硬盤,而是先暫存內存中,若是一個數據被重複寫,這樣速度必定快,但存在一個問題,萬一從新啓動,或者是關機,或者是不正常斷電的狀況下,因爲數據還沒來得及存入硬盤,會形成數據更新不正常,這時須要命令sync進行數據的寫入,即#sync,在內存中還沒有更新的的數據會寫入硬盤中。因此在關機或者開機以前最好多執行這個幾回,以確保數據寫入硬盤。服務器
Rsync不只能夠遠程同步數據(相似於scp),固然還能夠本地同步數據(相似於cp),但不一樣於cp或scp的一點是,rsync不像cp/scp同樣會覆蓋之前的數據(若是數據已經存在),它會先判斷已經存在的數據和新數據有什麼不一樣,只有不一樣時纔會把不一樣的部分覆蓋掉。
dom
scp 用來遠程拷貝數據,經過ssh協議通訊。它的語法很簡單,相似於cp, 惟一不一樣的是,源地址或者目標地址須要使用遠程主機的ip或者hostname. 例如要把本地的數據拷貝到遠程一臺主機(192.168.0.111)的/data/目錄下,能夠這樣實現: scp /dir/filename root@192.168.0.111:/data/ 其中filename 能夠是目錄也能夠是文件。或者也能夠把遠程的文件拷貝到本地: scp root@192.168.0.111:/data/filename /data/ssh
示例,scp拷貝root目錄下面的文件到遠程主機192.168.20.10的data目錄下;tcp
[root@yong ~]# scp /root/iptables.sh root@192.168.20.10:/data The authenticity of host '192.168.20.10 (192.168.20.10)' can't be established. RSA key fingerprint is 84:47:af:bf:11:69:43:aa:bc:fe:9b:d6:08:b4:c4:1a. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.20.10' (RSA) to the list of known hosts.root@192.168.20.10's password: iptables.sh 100% 254 0.3KB/s 00:00
首次鏈接會提示是否要繼續鏈接,咱們輸入yes繼續,當創建鏈接後,須要輸入遠程主機root密碼。
ide
示例,scp拷貝目錄須要加-r參數,拷貝的同時能夠更改目錄名;工具
[root@yong rsync]# scp -r /root/rsync/test1/ 192.168.20.10:/data/a/ root@192.168.20.10's password: 1 100% 0 0.0KB/s 00:00 1.txt 100% 0 0.0KB/s 00:00 2 100% 0 0.0KB/s 00:00 2.txt 100% 0 0.0KB/s 00:00 aa 100% 0 0.0KB/s 00:00 3 100% 0 0.0KB/s 00:00
安裝rysnc的命令:yum install -y rsync性能
示例一,拷貝當前目錄下test.txt 到/tmp目錄下,拷貝的同時也能夠更改文件名;
[root@yong ~]# rsync -av test.txt /tmp/a.txt sending incremental file list test.txt sent 279 bytes received 31 bytes 620.00 bytes/sec total size is 203 speedup is 0.65 [root@yong ~]# ls /tmp/a.txt /tmp/a.txt
示例二,拷貝當前目錄test.txt 到遠程主機192.168.20.10 /data目錄下,須要輸入遠程主機的密碼;
[root@yong ~]# rsync -av test.txt 192.168.20.10:/data/ root@192.168.20.10's password: sending incremental file list test.txt sent 279 bytes received 31 bytes 88.57 bytes/sec total size is 203 speedup is 0.65
示例三,從遠程主機192.168.20.10 /data目錄下拷貝httpd文件到本地當前目錄下;
[root@yong ~]# rsync -av 192.168.20.10:/data/httpd ./ root@192.168.20.10's password: receiving incremental file list httpd sent 30 bytes received 7538228 bytes 886853.88 bytes/sec total size is 7537230 speedup is 1.00
1. rsync 命令格式
rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [USER@]HOST:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST
rsync [OPTION]... [USER@]HOST::SRC DEST
rsync [OPTION]... SRC [USER@]HOST::DEST
上面第一個例子即爲第一種格式,第二個例子即爲第二種格式,但不一樣的是,user@host 若是不加默認指的是root用戶;第三個例子即爲第三種格式是從遠程目錄同步數據到本地。第四種以及第五種格式使用了兩個冒號,這種方式和前面的方式的不一樣在於驗證方式不一樣。
2. rsync經常使用選項
-a 歸檔模式,表示以遞歸方式傳輸文件,並保持全部屬性,等同於-rlptgoD, -a選項後面能夠跟一個 --no-OPTION 這個表示關閉-rlptgoD中的某一個,例如-a--no-l 等同於-rptgoD
-r 對子目錄以遞歸模式處理,主要是針對目錄來講的,若是單獨傳一個文件不須要加-r,可是傳輸的是目錄必須加-r選項
-v 打印一些信息出來,好比速率,文件數量等
-l 保留軟鏈結
-L 向對待常規文件同樣處理軟連接,若是是SRC中有軟連接文件,則加上該選項後將會把軟連接指向的目標文件拷貝到DST
-p 保持文件權限
-o 保持文件屬主信息
-g 保持文件屬組信息
-D 保持設備文件信息
-t 保持文件時間信息
--delete 刪除那些DST中SRC沒有的文件
--exclude=PATTERN 指定排除不須要傳輸的文件,等號後面跟文件名,能夠是萬用字符模式(如*.txt)
--progress 在同步的過程當中能夠看到同步的過程狀態,好比統計要同步的文件數量、同步的文件傳輸速度等等
-u 加上這個選項後將會把DEST目標文件中比SRC源文件還新的文件排除掉,不會覆蓋
最經常使用的選項有 -a -v --delete --exclude
示例:建立實驗環境
[root@yong ~]# mkdir rsync [root@yong ~]# cd rsync/ [root@yong rsync]# mkdir test1 [root@yong rsync]# cd test1 [root@yong test1]# touch 1 2 3 [root@yong test1]# ln -s /root/123.txt ./123.txt [root@yong test1]# ls -l -rw-r--r-- 1 root root 0 Apr 22 16:02 1 lrwxrwxrwx 1 root root 13 Apr 22 16:03 123.txt -> /root/123.txt -rw-r--r-- 1 root root 0 Apr 22 16:02 2 -rw-r--r-- 1 root root 0 Apr 22 16:02 3 [root@yong test1]# cd ..
實驗目的,拷貝test1目錄到test2目錄,實際上rsync新創建了一個test2目錄並把test1目錄放在test2目錄下面,這不是咱們想要的結果;
[root@yong rsync]# rsync -a test1 test2 [root@yong rsync]# ls test2 test1 [root@yong rsync]# ls test1 test2 [root@yong rsync]# ls test2/test1/ 1 123.txt 2 3
爲了不上面的操做,在同步目錄的時候在目錄後面加 /
[root@yong rsync]# rsync -a test1/ test2/ [root@yong rsync]# ls test1 test2 [root@yong rsync]# ls test2/ 1 123.txt 2 3
-a選項還能夠與--no-OPTION一塊兒使用,意思爲不理會選項的文件;以下例,跳過123.txt軟連接文件,不作拷貝;
[root@yong rsync]# rsync -av --no-l test1/ test2/ sending incremental file list created directory test2 ./ 1 skipping non-regular file "123.txt" 2 3 sent 200 bytes received 72 bytes 544.00 bytes/sec total size is 13 speedup is 0.05 [root@yong rsync]# ls test1 test2 [root@yong rsync]# ls -l test2/ total 0 -rw-r--r-- 1 root root 0 Apr 22 16:02 1 -rw-r--r-- 1 root root 0 Apr 22 16:02 2 -rw-r--r-- 1 root root 0 Apr 22 16:02 3
加-L選項,拷貝的時候會拷貝軟連接文件對應的源文件到目標目錄裏面;
[root@yong rsync]# rsync -avL test1/ test2/ sending incremental file list created directory test2 ./ 1 123.txt 2 3 sent 231 bytes received 91 bytes 644.00 bytes/sec total size is 0 speedup is 0.00
[root@yong rsync]# ls -l test2/ total 0 -rw-r--r-- 1 root root 0 Apr 22 16:02 1 -rw-r--r-- 1 root root 0 Apr 22 16:03 123.txt -rw-r--r-- 1 root root 0 Apr 22 16:02 2 -rw-r--r-- 1 root root 0 Apr 22 16:02 3
test1和test2目錄下的1文件,建立的時間是同樣的;touch test2/1以後建立時間晚了一些。同步的時候不加-u選項,發現同步後的時間仍是test1/1的建立時間;
[root@yong rsync]# ll test1/1 test2/1 -rw-r--r-- 1 root root 0 Apr 22 16:02 test1/1 -rw-r--r-- 1 root root 0 Apr 22 16:02 test2/1 [root@yong rsync]# touch test2/1 [root@yong rsync]# ll test1/1 test2/1 -rw-r--r-- 1 root root 0 Apr 22 16:02 test1/1 -rw-r--r-- 1 root root 0 Apr 22 16:31 test2/1 [root@yong rsync]# rsync -av test1/1 test2/1 sending incremental file list 1 sent 65 bytes received 31 bytes 192.00 bytes/sec total size is 0 speedup is 0.00 [root@yong rsync]# ll test1/1 test2/1 -rw-r--r-- 1 root root 0 Apr 22 16:02 test1/1 -rw-r--r-- 1 root root 0 Apr 22 16:02 test2/1
加-u選項,若是目標文件比源文件新,那麼會忽略掉該文件,不作同步;
[root@yong rsync]# touch test2/1 [root@yong rsync]# ll test2/1 -rw-r--r-- 1 root root 0 Apr 22 16:37 test2/1 [root@yong rsync]# rsync -avu test1/1 test2/1 sending incremental file list sent 26 bytes received 12 bytes 76.00 bytes/sec total size is 0 speedup is 0.00 [root@yong rsync]# ll test2/1 test1/1 -rw-r--r-- 1 root root 0 Apr 22 16:02 test1/1 -rw-r--r-- 1 root root 0 Apr 22 16:37 test2/1
刪除test1/123.txt,同步以後test2目錄下的123.txt不會刪除;
加--delete選項,同步以後test2目錄下的123.txt文件也會刪除;具體做用是刪除目標目錄比源目錄多出來的文件;
[root@yong rsync]# rm -f test1/123.txt [root@yong rsync]# ls test1/ 1 2 3 [root@yong rsync]# rsync -av test1/ test2/ sending incremental file list ./ 1 sent 94 bytes received 34 bytes 256.00 bytes/sec total size is 0 speedup is 0.00 [root@yong rsync]# ls test2/ 1 123.txt 2 3
[root@yong rsync]# rsync -av --delete test1/ test2/ sending incremental file list deleting 123.txt sent 52 bytes received 12 bytes 128.00 bytes/sec total size is 0 speedup is 0.00 [root@yong rsync]# ls test2/ 1 2 3
--exclude選項的做用是,同步的過程當中排除文件;選項後面的文件不會同步拷貝到目標目錄下;
[root@yong rsync]# touch test1/aa [root@yong rsync]# rsync -a --exclude="aa" test1/ test2/ [root@yong rsync]# ls test1/ 1 2 3 aa [root@yong rsync]# ls test2/ 1 2 3
--progress選項的做用是顯示同步過程的詳細信息;--exclude選項後面也可使用通配符 *
[root@yong rsync]# touch test1/1.txt test1/2.txt [root@yong rsync]# rsync -a --progress --exclude="*.txt" test1/ test2/ sending incremental file list ./ aa 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/5) sent 109 bytes received 34 bytes 286.00 bytes/sec total size is 0 speedup is 0.00 [root@yong rsync]# ls test2/ 1 2 3 aa [root@yong rsync]# ls test1/ 1 1.txt 2 2.txt 3 aa
3. rsync選項總結
rsync -av dir1/ dir2/ #其中dir2/目錄能夠不存在,記得同步目錄時必定要在末尾加上/
-a 會把軟鏈接原本來本的拷貝過去;
-v 可視化顯示同步信息,如傳輸的文件大小,傳輸速度,發送和接收的大小;
rsync -avL test1/ test2/ 加-L會拷貝軟鏈接文件對應的源文件到目標目錄裏面;
touch test2/1.txt; rsync -avu test1/ test2/ -u 選項的做用是,若是目標文件比源文件新,那麼會忽略掉該文件
rsync -av --delete test1/ test2/ #這樣會把test2/目錄比test1/目錄多出來的文件刪除掉
rsync -a --exclude=「2.txt」 test1/ test2/ #在同步的過程當中,會忽略掉2.txt這個文件
rsync -a --progress --exclude=「*.txt」 test1/ test2/ #--progress 顯示同步過程的詳細信息,--exclude後面也可使用通配符*
4. rsync應用實例 - ssh方式訪問
第一種方式:本地拷貝到遠程linux主機,須要輸入遠程主機的密碼;
[root@yong rsync]# rsync -avL test1/ root@192.168.20.10:/tmp/test2/ root@192.168.20.10's password: sending incremental file list created directory /tmp/test2 ./ 1 1.txt 2 2.txt 3 aa sent 336 bytes received 129 bytes 132.86 bytes/sec total size is 0 speedup is 0.00
第二種方式:從遠程linux主機同步到本地機器,也須要輸入遠程主機的密碼;
[root@yong rsync]# rsync -avL root@192.168.20.10:/tmp/test2/ ./test3/ root@192.168.20.10's password: receiving incremental file list created directory ./test3 ./ 1 1.txt 2 2.txt 3 aa sent 128 bytes received 329 bytes 130.57 bytes/sec total size is 0 speedup is 0.00
經過建立祕鑰對,讓兩臺機器產生信任關係同步數據時不用輸入密碼
具體步驟以下:
在A機器當前用戶家目錄建立.ssh目錄,執行ssh-keygen命令,提示輸入密碼的時候直接回車,默認爲空密碼,最後生成公鑰id_rsa.pub和私鑰id_rsa文件;
[root@yong ~]# mkdir .ssh [root@yong ~]# ssh-keygen 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: ab:0e:83:11:69:69:34:d2:34:87:b8:07:40:36:e4:a3 root@yong.com The key's randomart p_w_picpath is: +--[ RSA 2048]----+ |*O=.. | |=o+* | | =* | |ooo. | |E.. S | | o . | | . o . | | o . | | .o | +-----------------+ [ root@yong ~]# ls .ssh/ id_rsa id_rsa.pub
[root@yong ~]# cat .ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArLHpwzyzfPtjihx90/vdl9HqtLfmpuNDGYL+UWOcFAAul6PVz81fb/0RSAAPSu1Q3UdBWXlTqPUH4JucwaxYW+obW/zmteuZRaGf06iY01cj/Nr74ML0792cvfjCU87FyEH+ZcvNhgRU+iTo+ES0kkLEuHV1x3JJLOhgYzIU0RtuU3CRiFxPHo92jNjpOs66YW3TbOX0AEB47WoRmKThiQVwoU7Lwqpl/N7vQHtdj9vPtsVZsguKlCB5a2YoxRpnbdn8a5jdzVKuy+hBrdfP/1NkmVU9mBTV/f0b+is5uHmQqNVAQW5fIi7QdVWG8HuyQliOXZUxoxGPCAjCuNy+dw== root@yong.com
拷貝A機器的公鑰內容,複製到B機器的/root/.ssh/authorized_keys文件中(若是B機器沒有.ssh目錄和authorized_keys文件須要建立)
[root@localhost ~]# mkdir .ssh [root@localhost ~]# chmod 700 .ssh/ [root@localhost ~]# touch .ssh/authorized_keys [root@localhost ~]# vi .ssh/authorized_keys
返回到A機器,執行命令ssh root@192.168.20.10 (B機器的ip地址)不用輸入密碼便可訪問B機器了。
[root@yong ~]# ssh root@192.168.20.10 The authenticity of host '192.168.20.10 (192.168.20.10)' can't be established. RSA key fingerprint is 84:47:af:bf:11:69:43:aa:bc:fe:9b:d6:08:b4:c4:1a. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.20.10' (RSA) to the list of known hosts. Last login: Wed Apr 22 19:35:40 2015 from 192.168.20.1 [root@localhost ~]# hostname localhost.localdomain
A機器執行exit退出,執行同步命令,此次不用輸入密碼便可同步。
[root@localhost ~]# exit logout Connection to 192.168.20.10 closed. [root@yong ~]# rsync -av rsync/test1/ root@192.168.20.10:/tmp/test3/ sending incremental file list created directory /tmp/test3 ./ 1 1.txt 2 2.txt 3 aa sent 336 bytes received 129 bytes 310.00 bytes/sec total size is 0 speedup is 0.00
若是ssh端口不是22,那麼須要指定一個端口號,命令爲:rsync -av "--rsh=ssh -p port」 /dir1/ root@192.168.20.10:/tmp/dir2/ 或者 rsync -av -e "ssh -p port" /dir1/ root@192.168.20.10:/tmp/dir2/
更改B機器的ssh端口號爲222,在A機器使用telnet 檢測B機器22端口,提示鏈接拒絕,固然也不能同步數據,須要指定端口號 "--rsh=ssh -p 222" 能夠同步數據,更改端口號以後更加安全,防止他人經過端口***。
更改ssh端口號,編輯/etc/ssh/sshd_config文件,找到PORT 22這一行更改便可,保存退出後,重啓sshd服務生效。
[root@localhost ~]# vi /etc/ssh/sshd_config [root@localhost ~]# service sshd restart 中止 sshd: [肯定] 正在啓動 sshd:
[root@yong ~]# telnet 192.168.20.10 22 Trying 192.168.20.10... telnet: connect to address 192.168.20.10: Connection refused
[root@yong ~]# rsync -av rsync/test1/ root@192.168.20.10:/tmp/test2/ ssh: connect to host 192.168.20.10 port 22: Connection refused rsync: connection unexpectedly closed (0 bytes received so far) [sender] rsync error: error in rsync protocol data stream (code 12) at io.c(600) [sender=3.0.6]
[root@yong ~]# rsync -av "--rsh=ssh -p 222" rsync/test1/ root@192.168.20.10:/tmp/test2/ sending incremental file list created directory /tmp/test2 ./ 1 1.txt 2 2.txt 3 aa sent 336 bytes received 129 bytes 310.00 bytes/sec total size is 0 speedup is 0.00
5. rsync應用實例 - 服務器客戶端C/S模式
這種方式能夠理解成這樣,在遠程主機上創建一個rsync的服務器,在服務器上配置好rsync的各類應用,而後本機做爲rsync的一個客戶端去鏈接遠程的rsync服務器。安全性能會大大提升,也能夠自定義配置不少項目,比較靈活。平常環境推薦使用。
須要新建配置文件 /etc/rsyncd.conf ,內容以下,#號後的內容不用寫進去;
#port=873 #監聽端口默認爲873,也能夠是別的端口
log file=/var/log/rsync.log #指定日誌
pid file=/var/run/rsyncd.pid #指定pid
#address=192.168.20.10 #能夠定義綁定的ip
以上部分爲全局配置部分,如下爲模塊內的設置
[test] #爲模塊名,自定義
path=/tmp/rsync # 指定該模塊對應在哪一個目錄下
use chroot=true #是否限定在該目錄下,默認爲true,當有軟鏈接時,須要改成fasle
max connections=4 # 指定最大能夠鏈接的客戶端數
read only=no #是否爲只讀,yes爲只讀,不能在對應目錄下寫入文件。
list=true #是否能夠列出模塊名
uid=root #以哪一個用戶的身份來傳輸
gid=root #以哪一個組的身份來傳輸
auth users=test #指定驗證用戶名,能夠不設置
secrets file=/etc/rsyncd.passwd #指定密碼文件,若是設定驗證用戶,這一項必須設置
hosts allow=192.168.0.101 #設置能夠容許訪問的主機,能夠是網段
密碼文件/etc/rsyncd.passwd的內容格式爲:username:password
啓動服務的命令是:rsync --daemon
默認去使用/etc/rsyncd.conf這個配置文件,也能夠指定配置文件 rsync --daemon --config=/etc/rsyncd2.conf
實驗,編輯rsyncd.conf文件,保存退出;
配置文件hosts allow 容許訪問主機設置ip網段,也能夠設置固定ip,多個ip用空格分隔。
[root@localhost rsync]# cat /etc/rsyncd.conf #port=873 log file=/var/log/rsync.log pid file=/var/run/rsyncd.pid #address=192.168.20.10 [test] path=/tmp/rsync use chroot=true max connections=4 read only=no list=true uid=root gid=root #auth users=test #secrets file=/etc/rsyncd.passwd hosts allow=192.168.20.0/24
啓動rsync服務,進程也啓動,默認873端口也監聽;
[root@localhost tmp]# rsync --daemon [root@localhost tmp]# ps aux |grep rsync root 1583 0.0 0.1 6232 644 ? Ss 14:49 0:00 rsync --daemon root 1585 0.0 0.1 5976 740 pts/0 S+ 14:50 0:00 grep rsync [root@localhost tmp]# netstat -nlp |grep rsync tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 1583/rsync tcp 0 0 :::873 :::* LISTEN 15
配置OK後,訪問遠程服務器#rsync -av test@192.168.20.10::test/test1/ /tmp/test1/
中間2個冒號,後面跟模塊名test,後面跟test1目錄,實際就是/tmp/rsync/test1/目錄,模塊對應的目錄裏面。
1)從本地同步數據到服務器端,同步的時候也能夠進行更名;
[root@yong rsync]# rsync -avL --progress test1/ test@192.168.20.10::test/test/ sending incremental file list created directory /test ./ 1 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=8/10) 1.txt 1076 100% 0.00kB/s 0:00:00 (xfer#2, to-check=7/10) 2 0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=6/10) 2.txt 0 100% 0.00kB/s 0:00:00 (xfer#4, to-check=5/10) 3 0 100% 0.00kB/s 0:00:00 (xfer#5, to-check=4/10) aa 0 100% 0.00kB/s 0:00:00 (xfer#6, to-check=3/10) tree 36464 100% 1.93MB/s 0:00:00 (xfer#7, to-check=2/10) a1/ b1/ sent 37992 bytes received 152 bytes 3632.76 bytes/sec total size is 37540 speedup is 0.98
[root@localhost rsync]# ls test/ 1 1.txt 2 2.txt 3 a1 aa b1 tree
2)從服務器同步數據到本地;
[root@yong rsync]# rsync -avL --progress 192.168.20.10::test/abc ./ receiving incremental file list abc 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1) sent 45 bytes received 94 bytes 13.24 bytes/sec total size is 0 speedup is 0.00 [root@yong rsync]# ls abc abc
3)更改服務器配置文件read only=yes 再從本地同步數據到服務器,會報錯提示module模塊只讀,不能寫入數據;從服務器同步到本地不受影響。
[root@localhost rsync]# sed -i 's/read only=no/read only=yes/' /etc/rsyncd.conf [root@localhost rsync]# grep 'read' /etc/rsyncd.conf read only=yes
[root@yong rsync]# rsync -avL --progress test1/ 192.168.20.10::test/abc/ sending incremental file list ERROR: module is read only rsync error: syntax or usage error (code 1) at main.c(866) [receiver=3.0.6] rsync: read error: Connection reset by peer (104) rsync error: error in rsync protocol data stream (code 12) at io.c(759) [sender=3.0.6]
針對配置文件裏面的幾個選項port,use chroot,list,hosts allow 等,你們有興趣的能夠本身作實驗看看。