1. 程式人生 > >Linux下的資料備份工具rsync

Linux下的資料備份工具rsync

rsync簡介:

rsync命令( Remote Sync)是一個遠端資料同步工具,可通過LAN/WAN快速同步多臺主機間的檔案。rsync使用所謂的“rsync演算法”來使本地和遠端兩個主機之間的檔案達到同步,這個演算法只傳送兩個檔案的不同部分,而不是每次都整份傳送,因此速度相當快。

它的特性如下:

可以映象儲存整個目錄樹和檔案系統。

可以很容易做到保持原來檔案的許可權、時間、軟硬連結等等。

無須特殊許可權即可安裝。

快速:第一次同步時 rsync 會複製全部內容,但在下一次只傳輸修改過的檔案。rsync 在傳輸資料的過程中可以實行壓縮及解壓縮操作,因此可以使用更少的頻寬。

安全:可以使用scp、ssh等方式來傳輸檔案,當然也可以通過直接的socket連線。

支援匿名傳輸,以方便進行網站鏡象。

rsync語法:

語法

rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [[email protected]]host:DEST
rsync [OPTION]... [[email protected]]HOST:SRC DEST
rsync [OPTION]... [[email protected]]HOST::SRC DEST
rsync [OPTION]...
SRC [[email protected]]HOST::DEST rsync [OPTION]... rsync://[[email protected]]HOST[:PORT]/SRC [DEST]

對應於以上六種命令格式,rsync有六種不同的工作模式:

  1. 拷貝本地檔案。當SRC和DES路徑資訊都不包含有單個冒號”:”分隔符時就啟動這種工作模式。如:rsync -a /data /backup
  2. 使用一個遠端shell程式(如rshssh)來實現將本地機器的內容拷貝到遠端機器。當DST路徑地址包含單個冒號”:”分隔符時啟動該模式。如:rsync -avz *.c foo:src
  3. 使用一個遠端shell程式(如rsh、ssh)來實現將遠端機器的內容拷貝到本地機器。當SRC地址路徑包含單個冒號”:”分隔符時啟動該模式。如:rsync -avz foo:src/bar /data
  4. 從遠端rsync伺服器中拷貝檔案到本地機。當SRC路徑資訊包含”::”分隔符時啟動該模式。如:rsync -av [email protected]::www /databack
  5. 從本地機器拷貝檔案到遠端rsync伺服器中。當DST路徑資訊包含”::”分隔符時啟動該模式。如:rsync -av /databack [email protected]::www
  6. 列遠端機的檔案列表。這類似於rsync傳輸,不過只要在命令中省略掉本地機資訊即可。如:rsync -v rsync://192.168.78.192/www

選項

-v, --verbose 詳細模式輸出。
-q, --quiet 精簡輸出模式。
-c, --checksum 開啟校驗開關,強制對檔案傳輸進行校驗。
-a, --archive 歸檔模式,表示以遞迴方式傳輸檔案,並保持所有檔案屬性,等於-rlptgoD。
-r, --recursive 對子目錄以遞迴模式處理。
-R, --relative 使用相對路徑資訊。
-b, --backup 建立備份,也就是對於目的已經存在有同樣的檔名時,將老的檔案重新命名為~filename。可以使用--suffix選項來指定不同的備份檔案字首。
--backup-dir 將備份檔案(如~filename)存放在在目錄下。
-suffix=SUFFIX 定義備份檔案字首。
-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 保持檔案時間資訊。
-S, --sparse 對稀疏檔案進行特殊處理以節省DST的空間。
-n, --dry-run現實哪些檔案將被傳輸。
-w, --whole-file 拷貝檔案,不進行增量檢測。
-x, --one-file-system 不要跨越檔案系統邊界。
-B, --block-size=SIZE 檢驗演算法使用的塊尺寸,預設是700位元組。
-e, --rsh=command 指定使用rsh、ssh方式進行資料同步。
--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 不跳過那些有同樣的時間和長度的檔案。
--size-only 當決定是否要備份檔案時,僅僅察看檔案大小而不考慮檔案時間。
--modify-window=NUM 決定檔案是否時間相同時使用的時間戳視窗,預設為0-T --temp-dir=DIRDIR中建立臨時檔案。
--compare-dest=DIR 同樣比較DIR中的檔案來決定是否需要備份。
-P 等同於 --partial。
--progress 顯示備份過程。
-z, --compress 對備份的檔案在傳輸時進行壓縮處理。
--exclude=PATTERN 指定排除不需要傳輸的檔案模式。
--include=PATTERN 指定不排除而需要傳輸的檔案模式。
--exclude-from=FILE 排除FILE中指定模式的檔案。
--include-from=FILE 不排除FILE指定模式匹配的檔案。
--version 列印版本資訊。
--address 繫結到特定的地址。
--config=FILE 指定其他的配置檔案,不使用預設的rsyncd.conf檔案。
--port=PORT 指定其他的rsync服務埠。
--blocking-io 對遠端shell使用阻塞IO-stats 給出某些檔案的傳輸狀態。
--progress 在傳輸時現實傳輸過程。
--log-format=formAT 指定日誌檔案格式。
--password-file=FILEFILE中得到密碼。
--bwlimit=KBPS 限制I/O頻寬,KBytes per second。
-h, --help 顯示幫助資訊。

常用的有-a,-V –delete –exclude。

常用選項的例項:

(1)首先建立目錄以及檔案:

[root@localhost ~]# mkdir rsync
[root@localhost ~]# cd rsync
[root@localhost rsync]# mkdir test1
[root@localhost rsync]# cd test1
[root@localhost test1]# touch 1 2 3
[root@localhost test1]# ln -s /root/123.txt ./123.txt
[root@localhost test1]# ls -l
總用量 0
-rw-r--r-- 1 root root  0 76 13:37 1
lrwxrwxrwx 1 root root 13 76 13:57 123.txt -> /root/123.txt
-rw-r--r-- 1 root root  0 76 13:37 2
-rw-r--r-- 1 root root  0 76 13:37 3

(2)使用-a選項:

-a, –archive 歸檔模式,表示以遞迴方式傳輸檔案,並保持所有檔案屬性

[root@localhost rsync]# rsync -a test1 test2
[root@localhost rsync]# ls test2
test1
[root@localhost rsync]# ls test2/test1/
1  123.txt  2  3

這裡有一個問題,就是本來想把test1目錄直接拷貝成test2目錄,可結果rsync卻新建了test2目錄然後把test1放到test2當中。為了避免這樣的情況發生,可以這樣做:

[root@localhost rsync]# rm -rf test2
[root@localhost rsync]# rsync -a test1/ test2/
[root@localhost rsync]# ls -l test2/
總用量 0
-rw-r--r-- 1 root root  0 76 13:37 1
lrwxrwxrwx 1 root root 13 76 13:57 123.txt -> /root/123.txt
-rw-r--r-- 1 root root  0 76 13:37 2
-rw-r--r-- 1 root root  0 76 13:37 3

加一個斜槓就好了,所以建議在使用rsync備份目錄時要養成加斜槓的習慣。在上面講了-a選項等同於-rlptgoD,而且 -a 還可以和 --no-OPTIN
一併使用。下面看看-l選項的作用:

-l, –links 保留軟連結。

-v, –verbose 詳細模式輸出。

[[email protected] 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

使用-v選項看來就是方便呀,上例告訴我們跳過了非普通檔案123.txt,其實123.txt是一個軟連線檔案,如果不使用-l選項則不理會軟連線檔案的。雖然加上-l選項會把軟連線檔案給拷貝過去,但是軟連線的目標檔案卻沒有拷貝過去,有時候咱們指向拷貝軟連線檔案所指向的目標檔案,那這時候該怎麼辦呢?

(3)使用-L選項:

-L, –copy-links 想對待常規檔案一樣處理軟鏈結。

[[email protected] 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
[[email protected] rsync]# ls -l test2/
總用量 0
-rw-r--r-- 1 root root  0 7月   6 13:37 1
lrwxrwxrwx 1 root root 13 76 13:57 123.txt
-rw-r--r-- 1 root root  0 7月   6 13:37 2
-rw-r--r-- 1 root root  0 7月   6 13:37 3

加上 -L 選項就可以把SRC中軟連線的目標檔案給拷貝到DST.

(4)使用-u選項:

-u, –update 僅僅進行更新,也就是跳過所有已經存在於DST,並且檔案時間晚於要備份的檔案,不覆蓋更新的檔案。

首先檢視一下test1/1 和test2/1的建立時間(肯定是一樣的),然後使用touch修改一下test2/1的建立時間(此時test2/1要比test1/1的建立時間晚了一些),如果不加-u選項的話,會把test2/1的建立時間變成和test1/1的建立時間一樣。

[[email protected] rsync]# ll test1/1 test2/1
-rw-r--r-- 1 root root 0  6月 10 12:58 test1/1
-rw-r--r-- 1 root root 0  6月 10 12:58 test2/1

兩者之間的建立時間是一樣的,下面修改test2/1 的建立時間,然後不加-u同步:

[root@localhost rsync]# touch test2/1
[root@localhost rsync]# ll test2/1
-rw-r--r-- 1 root root 0  610 13:20 test2/1
[root@localhost rsync]# rsync -a test1/1 test2/
[root@localhost rsync]# ll test2/1
-rw-r--r-- 1 root root 0  610 12:58 test2/1

test2/1 的建立時間又變成和test1/1的建立時間一樣了。下面加上 -u 再看看結果是怎麼樣的:

[root@localhost rsync]# touch test2/1
[root@localhost rsync]# ll test2/1
-rw-r--r-- 1 root root 0  610 13:31 test2/1
[root@localhost rsync]# rsync -avu test1/ test2/
sending incremental file list
./
123.txt -> /root/123.txt

sent 100 bytes  received 18 bytes  236.00 bytes/sec
total size is 13  speedup is 0.11
[root@localhost rsync]# ll test2/1
-rw-r--r-- 1 root root 0  610 13:31 test2/1
[root@localhost rsync]# ll test1/1
-rw-r--r-- 1 root root 0  610 12:58 test1/1

加上-u 選項後,不會再把 test1/1 同步為 test2/1 了。

(5)使用–delete選項:

–delete 刪除那些DST中SRC沒有的檔案。

首先刪除test1/123.txt:

[root@localhost rsync]# rm -f test1/123.txt
[root@localhost rsync]# ls test1/
1  2  3

然後把test1/ 目錄 同步到 test2/ 目錄下:

[root@localhost 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@localhost rsync]# ls test2/
1  123.txt  2  3

test2/目錄並沒有刪除掉123.txt, 下面加上 --delete 選項:

[root@localhost 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@localhost rsync]# ls test2/
1  2  3

test2/ 目錄裡的123.txt也被刪除了,這就是 --delete 選項的用處。還有一種情況就是如果在DST增加檔案了,而SRC當中沒有這些檔案,同步時加上 --delete 選項後同樣會刪除新增的檔案:

[root@localhost rsync]# touch test2/4
[root@localhost rsync]# ls test1/
1  2  3
[root@localhost rsync]# ls test2/
1  2  3  4
[root@localhost rsync]# rsync -a --delete test1/ test2/
[root@localhost rsync]# ls test1/
1  2  3
[root@localhost rsync]# ls test2/
1  2  3

(6)使用–exclude選項:

–exclude=PATTERN 指定排除不需要傳輸的檔案模式。

–progress 在傳輸時現實傳輸過程。

[root@localhost rsync]# touch test1/4
[root@localhost rsync]# rsync -a --exclude="4" test1/ test2/
[root@localhost rsync]# ls test1/
1  2  3  4
[root@localhost rsync]# ls test2/
1  2  3

另外還可以使用匹配字元 *

[root@localhost rsync]# touch test1/1.txt test1/2.txt
[root@localhost rsync]# ls test1/
1  1.txt  2  2.txt  3  4
[root@localhost rsync]# rsync -a --progress --exclude="*.txt" test1/ test2/
sending incremental file list
./
4
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/5)

sent 104 bytes  received 34 bytes  276.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost rsync]# ls test2/
1  2  3  4

上例中,也連帶著使用了 --progress 選項,這個主要是用來觀察rsync同步過程的狀態的。最後簡單總結一下,平時使用rsync同步資料的時候,使用-a選項基本上就可以達到我們想要的效果了,只是有時候會有個別的需求,會用到 -a --no-OPTION, -u, -L, --delete, --exclude 以及 progress 這些選項。

rsync應用例項:

(1)通過ssh的方式:

[[email protected] rsync]# rsync -avL test1/ 192.168.171.128:/tmp/test2/
sending incremental file list
created directory /tmp/test2
./
1
123.txt
2
3

sent 278 bytes  received 128 bytes  812.00 bytes/sec
total size is 10  speedup is 0.02

這種方式就是前面介紹的第二種方式了,是通過ssh拷貝的資料,需要輸入192.168.171.128 那臺機器root賬戶的密碼。當然也可以使用第三種方式拷貝:

[[email protected] rsync]# rsync -avL [email protected]:/tmp/test2/ /tmp/test3/
receiving incremental file list
created directory /tmp/test3
./
1
123.txt
2
3

sent 103 bytes  received 282 bytes  770.00 bytes/sec
total size is 10  speedup is 0.03

以上兩種方式如果寫到腳本里,備份起來就有麻煩了,因為要輸入密碼,指令碼本來就是自動的,不可能做到的。但是不代表沒有解決辦法。那就是通過金鑰驗證,金鑰不設立密碼就ok了。

上面操作都沒有輸入密碼,因為我已經做好了免認證。免認證登入操作如下:

在操作之前我們先講明主機資訊: 192.168.171.129 (主機名[[email protected] ~])和 192.168.171.128 (主機名[email protected]:)。我在兩臺主機之間做了ssh登入免認證。將對方的公鑰檔案都儲存在自己的主機上。

首先確認一下兩臺主機上是否有這個檔案 /root/.ssh/id_rsa.pub:

[[email protected] ~]# ssh-keygen
Generating public/private rsa key pair.

因為之前生成過金鑰對,所以這個檔案已經存在了,如果您的Linux不存在這個檔案,請按照如下方法生成:

[[email protected] ~]# 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:
3b:74:af:e8:08:ac:99:30:3f:ef:84:7a:a0:a6:3d:89 [email protected]

在這個過程中會有一些互動的過程,它首先提示要輸入這個金鑰的密碼,出於安全考慮應該定義個密碼,但是我們的目的就是為了自動化同步資料,所以這裡不輸入任何密碼,直接按回車,即密碼為空。最後則生成了私鑰(/root/.ssh/id_rsa)和公鑰檔案(/root/.ssh/id_rsa.pub)

把公鑰檔案的內容拷貝到目標機器上:

[root@localhost ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDDxysdnnem+g5qA/3St3cubGjyT8D+u0SkpNT8z9F9mgSf8THCaZBh3I8urgJ4XfmIajj+Cz0LFd3UrB0HUt7BO1xUP36W4PbOeGei25W2qhRJIEn/RdwtUHkApWd6I43JdlPJNmy+9JgvXb6eOxUn8UgsBn13D0mOtBuE5uWQOcKDYi4DvqD5xVGhBNVvOvSS1Puvu9xygbf5H2l4fwih57hr7YTki/5DcMvdH0YkGbmOjLBHkqQZg6ZjzGuCGtIEh4yGghKoV1oPaYoCcXNKmQo48FjomUyyTrRxkRH4pAq6ckml22lxCe0av7Qu9yK2phUW+3Bo0AT8fSoJQ5EJ root@localhost

複製主機localhost的/root/.ssh/id_rsa.pub檔案內容,並貼上到主機Ubuntu的 ~/.ssh/authorized_keys中:

[root@ubuntu ~]# vim .ssh/authorized_keys

在這一步也許您會遇到.ssh目錄不存在的問題,可以手動建立,並修改目錄許可權為700也可以執行ssh-keygen命令生成這個目錄。儲存.ssh/authorized_keys檔案後,就可以在localhost主機上不用輸入密碼就能ssh登入Ubuntu主機。然後可以用相同的方法把Ubuntu的公鑰複製到localhost主機上。

然後可進行如下rsync操作:

[[email protected] ~]# rsync -av rsync/test1/ 192.168.171.128:/tmp/test4/
sending incremental file list
created directory /tmp/test2
./
1
123.txt
2
3

sent 278 bytes  received 128 bytes  812.00 bytes/sec
total size is 10  speedup is 0.02

(2) 通過後臺服務的方式

這種方式可以理解成這樣,在遠端主機上建立一個rsync的伺服器,在伺服器上配置好rsync的各種應用,然後本機作為rsync的一個客戶端去連線遠端的rsync伺服器。配置rsync伺服器的步驟:

在實驗中我將192.168.171.128,主機名Ubuntu作為rsync伺服器。192.168.171.129作為客戶端。

  1. 建立並配置rsync的配置檔案 /etc/rsyncd.conf
[email protected]:/# cat /etc/rsyncd.conf 
port=873
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.171.128

[root]
path=/root/rsync
use chroot=true
max connections=4
read only=no
list=true
uid=root
gid=root
auth users=root
secrets file=/etc/rsyncd.passwd
hosts allow=192.168.171.129

其中配置檔案分為兩部分:全部配置部分和模組配置部分,全域性部分就是幾個引數而已,就像rsyncd.conf中port, log file, pid file, address這些都屬於全域性配置,而[root]以下部分就是模組配置部分了。一個配置檔案中可以有多個模組,模組名自定義。其實模組中的一些引數例如use chroot, max connections, udi, gid, auth users, secrets file以及hosts allow都可以配置成全域性的引數。下面就簡單解釋一下這些引數的意義:

port 指定在哪個埠啟動rsyncd服務,預設是873

log file 指定日誌檔案

pid file 指定pid檔案,這個檔案的作用涉及到服務的啟動以及停止等程序管理操作

address 指定啟動rsyncd服務的IP,假如你的機器有多個IP,就可以指定其中一個啟動rsyncd服務,預設是在全部IP上啟動

[test] 指定模組名,自定義

path 指定資料存放的路徑

use chroot true|false 預設是true,意思是在傳輸檔案以前首先chroot到path引數所指定的目錄下。這樣做的原因是實現額外的安全防護,但是缺點是需要以roots許可權,並且不能備份指向外部的符號連線所指向的目錄檔案。預設情況下chroot值為true,如果你的資料當中有軟連線檔案的話建議設定成false。

max connections 指定最大的連線數,預設是0即沒有限制

read only ture|false 如果為true則不能上傳到該模組指定的路徑下

list 指定當使用者查詢該伺服器上的可用模組時,該模組是否被列出,設定為true則列出,false則隱藏

uid/gid 指定傳輸檔案時,以哪個使用者/組的身份傳輸

auth users 指定傳輸時要使用的使用者名稱

secrets file 指定密碼檔案,該引數連同上面的引數如果不指定則不使用密碼驗證,注意該密碼檔案的許可權一定要是600

hosts allow 指定被允許連線該模組的主機,可以是IP或者網段,如果是多個,之間用空格隔開

  1. 編輯secrets file,儲存後要賦予600許可權,如果許可權不對,不能完成同步
root@ubuntu:/# cat /etc/rsyncd.passwd 
root:test123
root@ubuntu:/# chmod 600 /etc/rsyncd.passwd 
  1. 啟動rsyncd服務
root@ubuntu:~# rsync --daemon --config=/etc/rsyncd.conf

啟動後,可以檢視一下日誌,並檢視埠是否啟動:

root@ubuntu:~# cat /var/log/rsync.log 
2018/07/06 16:44:15 [4178] rsyncd version 3.1.1 starting, listening on port 873
root@ubuntu:~# netstat -lnp | grep 873
tcp        0      0 192.168.171.128:873     0.0.0.0:*               LISTEN      4178/rsync  

如果想開機啟動,請把 rsync --daemon --confg=/etc/rsyncd.conf 寫入到/etc/rc.d/rc.local檔案。

  1. 到另一臺機器上測試
[[email protected] ~]# rsync -avL [email protected]::root/test/ /tmp/
Password: 
receiving incremental file list
./
1
2
3

sent 84 bytes  received 200 bytes  63.11 bytes/sec
total size is 0  speedup is 0.00

剛剛提到有一個選項叫做 “use chroot” 預設為true,如果是true,同步的檔案中如果有軟連線,則會有問題,首先在主機Ubuntu的/root/rsync/test/ 目錄下建立一個軟連線檔案:

root@ubuntu:~/rsync/test# ln -s /root/test.txt test.txt
root@ubuntu:~/rsync/test# ls
1  2  3  test.txt
root@ubuntu:~/rsync/test# ll test.txt 
lrwxrwxrwx 1 root root 14 76 17:01 test.txt -> /root/test.txt

然後再到主機localhost上,同步:

[[email protected] ~]# rsync -avL [email protected]::root/test/ /tmp/
Password: 
receiving incremental file list
symlink has no referent: "/test/test.txt" (in root)
./

sent 27 bytes  received 146 bytes  49.43 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1650) [generator=3.1.2]

可以看到,如果設定 “use chroot” 為true則同步軟連線檔案會有問題,下面把主機Ubuntu的rsync配置檔案修改一下,把true改為false:

[email protected]:~# cat /etc/rsyncd.conf 
port=873
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.171.128

[root]
path=/root/rsync
use chroot=false
max connections=4
read only=no
list=true
uid=root
gid=root
auth users=root
secrets file=/etc/rsyncd.passwd
hosts allow=192.168.171.0/24

然後再到主機localhost上再次執行同步:

[[email protected] ~]# rsync -avL [email protected]::root/test/ /tmp/
Password: 
receiving incremental file list
test.txt

sent 43 bytes  received 1,755 bytes  513.71 bytes/sec
total size is 1,600  speedup is 0.89

因為rsync有一個特定機制,配置檔案時即時生效的,所以修改完配置檔案後不用重啟服務。

上面的例子中,都需要輸入密碼,這樣同樣也不能寫入指令碼中自動執行,其實這種方式也是可以不用手動輸入密碼的,它有兩種實現方式。

第一種,指定密碼檔案

在客戶端上,也就是主機localhost上,編輯一個密碼檔案:加入root的密碼

[root@localhost ~]# vim /etc/pass
[root@localhost ~]# cat /etc/pass
test123

修改密碼檔案的許可權:

[root@localhost ~]# chmod 600 /etc/pass

在同步的時候,指定一下密碼檔案,就可以省去輸入密碼的步驟了:

[root@localhost ~]# rsync -avL root@192.168.171.128::root/test/ /tmp/ --password-file=/etc/pass
receiving incremental file list
./

sent 27 bytes  received 115 bytes  94.67 bytes/sec
total size is 1,600  speedup is 11.27

第二種:在rsync伺服器端不指定使用者

在服務端也就是主機Ubuntu上修改配置檔案rsyncd.conf, 去掉關於認證賬戶的配置項(auth user 和 secrets file這兩行):

[email protected]:~# sed -i 's/auth users/#auth users/;s/secrets file/#secrets file/' /etc/rsyncd.conf
[email protected]:~# cat /etc/rsyncd.conf 
port=873
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.171.128

[root]
path=/root/rsync
use chroot=false
max connections=4
read only=no
list=true
uid=root
gid=root
#auth users=root
#secrets file=/etc/rsyncd.passwd
hosts allow=192.168.171.0/24
[email protected]:~# 

上面的這個命令是把 “auth users” 和 “secrets file” 兩行的最前面加一個 “#”, 這樣就把這兩行註釋掉,使其失去意義。然後我們再到客戶端主機localhost上測試:

[[email protected] ~]# rsync -avL [email protected]::root/test/ /tmp/
receiving incremental file list

sent 20 bytes  received 108 bytes  256.00 bytes/sec
total size is 1,600  speedup is 12.50

注意,這裡不用再加root這個使用者了,預設是以root的身份拷貝的,現在已經不需要輸入密碼了。

相關推薦

Linux資料備份工具rsync

rsync簡介: rsync命令( Remote Sync)是一個遠端資料同步工具,可通過LAN/WAN快速同步多臺主機間的檔案。rsync使用所謂的“rsync演算法”來使本地和遠端兩個主機之間的檔案達到同步,這個演算法只傳送兩個檔案的不同部分,而不是每次都

Linux基礎學習-資料備份工具rsync

資料備份工具rsync 作為一個系統管理員,資料備份是非常重要的,如果沒有做好備份策略,磁碟損壞了,那麼你的資料將全部丟失,所以在日常的維護工作中,一定要時刻牢記給資料做備份. rsync不僅可以可以遠端同步資料(類似於scp),而且可以本地同步資料

Linux 筆記 - 第十三章 Linux 系統日常管理之(四)Linux 數據備份工具 rsync 和網絡配置

方法 target speed cnblogs rsync -av html links 布爾值 單個 博客地址:http://www.moonxy.com 一、前言 sync 命令是一個遠程數據同步工具,可通過 LAN/WAN 快速同步多臺主機間的文件,可以理解為 rem

31.Linux的數據備份工具rsync

傳輸 authent 沒有 tmp 包含 passwd 推出 過濾 send 八周二次課(1月30日) 10.28 rsync工具介紹 10.29/10.30 rsync常用選項 10.31 rsync通過ssh同步 一、rsync工具介紹 Linux系統下有很多數據備份工

Linux的數據備份工具rsync

遠程 glin rsn file passwd dup 判斷 x86_64 linux下 rsync(remote sync): 遠程同步,可以本地同步數據,不會覆蓋以前的數據,而是先判斷已經存在的數據和新數據的差異,只有數據不同時才會把不相同的部分覆蓋。# yum ins

Linux系統管理初步(八)數據備份工具rsync

進行 rsync工具 roo host type -o 常用選項 地址 vpd rsync工具 rsync工具用於同步文件夾數據,他不同於cp命令,他可以處理實時在變更的文件,把源文件目錄和目標數據目錄同步,對於兩個目錄相同的部分數據則不會處理。 一、工具概要 命令寫法研究

LINUXIDEA等工具調試項目時提示:Unable to open debugger port

over sed ubuntu cep 去掉 exc clas dig window 在Ubuntu下調試項目時使用TOMCAT容器,在設置好相應的TOMCAT LOCAL 路徑及相關信息後,點擊調試項目出現: Unable to open debugger port :

Linux文件同步工具-rsync

監聽端口 屬於 auth delete ret 日誌 roo delet sta Linux文件同步工具-rsync 安裝包 yum install -y rsync rsync常用選項 -a:歸檔模式,表示遞歸方式傳輸文件,並保持所有屬性;通-rlptgoD;-r:同

Linux安裝yum工具

Linux安裝yum工具[root@localhost ~]# yum -y install fonts-chinese Loading "rhnplugin" plugin Loading "security" plugin This system is not

linux的壓縮工具

使用 line OS unicode 類型 .gz IT 能夠 GZip壓縮 壓縮工具的介紹 壓縮服務器的經常訪問的文件,如web網頁,壓縮後可節省帶寬資源linux下的打包工具,後綴名在linux中不代表實際意義,但是為了方便區分文件類型,後綴名會以一個規範來命名.zip

Linux批量管理工具pssh使用記錄

time etc hosts ptime 同時 參數 -i ble test pssh是一款開源的軟件,使用python實現,用於批量ssh操作大批量機器;pssh是一個可以在多臺服務器上執行命令的工具,同時支持拷貝文件,是同類工具中很出色的;比起for循環的做法,我更推薦

Linux使用ASF工具Steam掛卡的配置使用

enable hist .com repos 不錯 rpm ech ssi farm Linux下使用ASF工具 Steam 掛卡的配置使用 簡介 既然你都費錢買了遊玩,為甚麽還要花時間玩呢?這款工具便可以不用下載遊玩,主動掛卡,是Steam回血渠道之一。(慈悲包)

Linux的開發工具:vim,gcc,gdb,makefile以及yum語句安裝軟體

Linux下的開發工具:vim,gcc,gdb,makefile以及yum語句安裝軟體 1. vi/vim  vi/vim都是多模式編譯器,vim是vi的升級版本。vim有12個模式,在這我們先說3種模式,命令模式,插入模式,底行模式。 2. vim基本操作: $vim t

Linux各壓縮工具的使用方法

壓縮工具 compress compress +檔案【多個檔案時壓縮每個檔案為單獨的壓縮檔案】 壓縮該檔案,壓縮後該檔案消失,生成一個.z的壓縮檔案 compress -c +檔案 > 檔案.gz壓縮檔案並顯示過程 不會丟失原始檔,利用重定向保留原檔案 uncomp

Linux定時備份文件與ssc平臺出租

mtime 刪除 date fin 保存 命令 exec 一個 打包 一、 編寫腳本編寫一個腳本文件,ssc平臺出租《企鵝21717 93408》使腳本可以執行備份命令。?例如,將文件目錄 /home/backups/balalala 備份到/home目錄下,並壓縮。

linux檔案比較工具diff|cmp使用小結

轉自:http://blog.csdn.net/wangjianno2/article/details/50451737,記錄下便於忘記時查詢。 1.diff diff是Unix系統的一個很重要的工具程式。它用來比較兩個文字檔案的差異,是程式碼版本管理的基石之一。 2.diff使用

LINUX 自動備份mysql資料庫

mysql自動備份分為兩個部分,一個部分是shell指令碼的編寫,另外一個部分是新增自動執行命令到crontab。 1.首先是shell指令碼的編寫,每天自動備份的的做法如下所示,shell指令碼首先用#!/bin/bash指定直譯器的路徑,接下來設定儲存路徑的變數。我們利用mysqldump這

手寫一個Linux的debug工具(一)

寫在前面 學習過Linux程式設計的同學都或多或少用過gdb來除錯自己的程式,但是並沒有很多的資源讓我們學習有關偵錯程式的東西。希望通過這篇文章,你可以瞭解基本的除錯原理,更重要的是可以寫出一個自己的簡易除錯工具。更有趣的是,你可以瞭解如何修改記憶體,來“愚弄

linux使用ab工具對伺服器進行壓力測試

ab 安裝 yum -y install httpd-tools (centos) 安裝完成後使用ab -v 檢視ab版本確認是否安裝成功 選項 選項 含義 -A auth-username:password 對伺服器提供BASIC認證信任。 使

cs231n-pytorch安裝+linux截圖工具安利

一、pytorch安裝(linux) linux下,很簡單.. 點選,根據自己的情況點點點,點選項就OK。這是我的情況,顯示卡趕緊買買買,加速duangduangduang 底下很貼心的出來命令,跑就是了。 然後驗證安裝 沒出錯就OK啦,我是直接裝好的,