一. 通過ssh的方式
前面介紹的rsync 5種方式當中,第二、第三(1個冒號)就屬于通過ssh的方式,這種方式其實就是讓用戶去登錄到遠程機器,然后執行rsync的任務。
[root@localhost rsync]# rsync -avL test1/ www@192.168.0.101:/tmp/test2/
www@192.168.0.101's password:
sending incremental file list
created directory /tmp/test2
./
1
1.txt
2
2.txt
3
4
sent 327 bytes received 129 bytes 182.40 bytes/sec
total size is 0 speedup is 0.00
這種方式就是前面介紹的第二種方式了,是通過ssh拷貝的數據,需要輸入192.168.0.101 那臺機器www 賬戶的密碼。當然也可以使用第三種方式拷貝:
[root@localhost rsync]# rsync -avL www@192.168.0.101:/tmp/test2/ ./test3/
www@192.168.0.101's password:
receiving incremental file list
created directory ./test3
./
1
1.txt
2
2.txt
3
4
sent 128 bytes received 351 bytes 38.32 bytes/sec
total size is 0 speedup is 0.00
以上兩種方式如果寫到腳本里,備份起來就有麻煩了,因為要輸入密碼,腳本本來就是自動的,不可能做到的。但是不代表沒有解決辦法。
那就是通過密鑰驗證,密鑰不設立密碼就ok了。還記得在前面阿銘曾經介紹過通過密鑰登錄遠程主機嗎,下面要講的內容就是那些東西了。
在操作之前我們先講明主機信息: 192.168.0.10 (主機名Aming-1)和 192.168.0.101 (主機名Aming)需要從Aming-1上拷貝數據到Aming上。
首先確認一下Aming-1上是否有這個文件 /root/.ssh/id_rsa.pub:
[root@Aming-1 ~]# ssh-keygen
Generating public/private rsa key pair.
阿銘之前生成過密鑰對,所以這個文件已經存在了,如果你的Linux不存在這個文件,請按照如下方法生成:
[root@Aming-1 ~]# 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 root@Aming-1
在這個過程中會有一些交互的過程,它首先提示要輸入這個密鑰的密碼,出于安全考慮應該定義個密碼,但是我們的目的就是為了自動化同步數據。
所以這里不輸入任何密碼,直接按回車,即密碼為空。最后則生成了私鑰(/root/.ssh/id_rsa)和公鑰文件(/root/.ssh/id_rsa.pub)
把公鑰文件的內容拷貝到目標機器上:
[root@Aming-1 ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA5SPyJ/kliGTAMUan/GCN325VS8jMxvOn4uQoLU/NqBpCI3MrmvSucv6EAzxx1J2uOssW08el06LG+cUwXmm5mkqDRBV6C9qNnR/bVV5vr3QsUwbKPr7fdyJvruQWWR7cSL+mjP0SYmG2Qy2JcM3hl1IZArzC6yeUnq2Gwbax8LgbZE3XfRfOYdimwyh5Tfft7yLYipWc37k+oRUWkI3mW7PalsOlfQhxrLD/lS891y6RdSbGxMJWPoV0KMFbVh+uJgyAXpeuWl+F+/iuQPzb6w3h4pWI31bvbsE9BU82jSzHYEjpq3SN2MJN2vaLs5a0mVpm9zka/h4ITFB8Uy1iSQ== root@Aming-1
復制主機Aming-1的/root/.ssh/id_rsa.pub文件內容,并粘貼到主機Aming的/home/www/.ssh/authorized_keys中:
[root@Aming ~]# vim /home/www/.ssh/authorized_keys
在這一步也許你會遇到/home/www/.ssh目錄不存在的問題,可以手動創建,并修改目錄權限為700也可以執行ssh-keygen命令生成這個目錄。
保存/home/www/.ssh/authorized_keys文件后,再到主機Aming-1上執行:
[root@Aming-1 ~]# ssh www@192.168.0.101
Last login: Wed Jun 12 12:24:34 2013 from 192.168.0.10
[www@Aming ~]$
現在不用輸入密碼也可以登錄主機Aming了。下面先從Aming主機退出來,再從主機Aming-1上執行一下rsync命令試試吧。
[root@Aming-1 ~]# rsync -av rsync/test1/ www@192.168.0.101:/tmp/test4/
sending incremental file list
created directory /tmp/test4
./
1
1.txt
2
2.txt
3
4
sent 327 bytes received 129 bytes 912.00 bytes/sec
total size is 0 speedup is 0.00
二. 通過后臺服務的方式
這種方式可以理解成這樣,在遠程主機上建立一個rsync的服務器,在服務器上配置好rsync的各種應用,然后本機作為rsync的一個客戶端去連接遠程的rsync服務器。下面阿銘就介紹一下,如何去配置一臺rsync服務器。
建立并配置rsync的配置文件 /etc/rsyncd.conf
[root@Aming-1 ~]# vim /etc/rsyncd.conf
#port=873
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
#address=192.168.0.10
[test]
path=/root/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.0.101
其中配置文件分為兩部分:全部配置部分和模塊配置部分,全局部分就是幾個參數而已,就像阿銘的rsyncd.conf中port, log file, pid file, address這些都屬于全局配置,而[test]以下部分就是模塊配置部分了。
一個配置文件中可以有多個模塊,模塊名自定義,格式就像阿銘的rsyncd.conf中的這樣。其實模塊中的一些參數例如use chroot, max connections, udi, gid, auth users, secrets file以及hosts allow都可以配置成全局的參數。
當然阿銘給出的參數并不是所有的,你可以通過man rsyncd.conf 獲得更多信息。下面就簡單解釋一下這些參數的意義:
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或者網段,如果是多個,之間用空格隔開
編輯secrets file,保存后要賦予600權限,如果權限不對,不能完成同步
[root@Aming-1 ~]# cat /etc/rsyncd.passwd
test:test123
[root@Aming-1 ~]# chmod 600 /etc/rsyncd.passwd
啟動rsyncd服務
[root@Aming-1 ~]# rsync --daemon --config=/etc/rsyncd.conf
啟動后,可以查看一下日志,并查看端口是否啟動:
[root@Aming-1 ~]# cat /var/log/rsync.log
[root@Aming-1 ~]# netstat -lnp |grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 12066/rsync
tcp 0 0 :::873 :::* LISTEN 12066/rsync
如果想開機啟動,請把 rsync --daemon --confg=/etc/rsyncd.conf 寫入到/etc/rc.d/rc.local文件。
到另一臺機器上測試
[root@Aming ~]# rsync -avL test@192.168.0.10::test/test1/ /tmp/test5/
Password:
receiving incremental file list
created directory /tmp/test5
./
1
1.txt
2
2.txt
3
4
sent 143 bytes received 354 bytes 994.00 bytes/sec
total size is 0 speedup is 0.00
阿銘剛剛提到有一個選項叫做 “use chroot” 默認為true,如果是true,同步的文件中如果有軟連接,則會有問題,首先在主機Aming-1的/root/rsync/test1/ 目錄下創建一個軟連接文件:
[root@Aming-1 ~]# ln -s /root/test.txt rsync/test1/test.txt
[root@Aming-1 ~]# ls -l rsync/test1/test.txt
lrwxrwxrwx 1 root root 14 6月 12 13:24 rsync/test1/test.txt -> /root/test.txt
然后再到主機Aming上,同步:
[root@Aming ~]# rsync -avL test@192.168.0.10::test/test1/ /tmp/test6/
Password:
receiving incremental file list
symlink has no referent: "/test1/test.txt" (in test)
created directory /tmp/test6
./
1
1.txt
2
2.txt
3
4
sent 143 bytes received 419 bytes 1124.00 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(1532) [generator=3.0.6]
可以看到,如果設置 “use chroot” 為true則同步軟連接文件會有問題,下面阿銘把主機Aming-1的rsync配置文件修改一下,把true改為false:
[root@Aming-1 ~]# sed -i 's/use chroot=true/use chroot=false/' /etc/rsyncd.conf
[root@Aming-1 ~]# grep 'use chroot' /etc/rsyncd.conf
use chroot=false
然后再到主機Aming上再次執行同步:
[root@Aming ~]# rsync -avL test@192.168.0.10::test/test1/ /tmp/test7/
Password:
receiving incremental file list
created directory /tmp/test7
./
1
1.txt
2
2.txt
3
4
test.txt
sent 162 bytes received 410 bytes 1144.00 bytes/sec
total size is 0 speedup is 0.00
這樣就沒有任何問題啦,你也許會奇怪,為什么阿銘修改完rsyncd.conf配置文件后,沒有重啟rsyncd服務呢?其實這是rsync的一個特定機制,配置文件時即時生效的,不用重啟服務。
上面的例子中,阿銘都有輸入密碼,這樣同樣也不能寫入腳本中自動執行,其實這種方式也是可以不用手動輸入密碼的,它有兩種實現方式。
第一種,指定密碼文件
在客戶端上,也就是主機Aming上,編輯一個密碼文件:
[root@Aming ~]# vim /etc/pass
加入test用戶的密碼:
[root@Aming ~]# cat /etc/pass
test123
修改密碼文件的權限:
[root@Aming ~]# chmod 600 /etc/pass
在同步的時候,指定一下密碼文件,就可以省去輸入密碼的步驟了:
[root@Aming ~]# rsync -avL test@192.168.0.10::test/test1/ /tmp/test8/ --password-file=/etc/pass
receiving incremental file list
created directory /tmp/test8
./
1
1.txt
2
2.txt
3
4
test.txt
sent 190 bytes received 451 bytes 1282.00 bytes/sec
total size is 0 speedup is 0.00
第二種:在rsync服務器端不指定用戶
在服務端也就是主機Aming-1上修改配置文件rsyncd.conf, 去掉關于認證賬戶的配置項(auth user 和 secrets file這兩行):
sed -i 's/auth users/#auth users/;s/secrets file/#secrets file/' /etc/rsyncd.conf
上面的這個命令是把 “auth users” 和 “secrets file” 兩行的最前面加一個 “#”, 這樣就把這兩行注釋掉,使其失去意義。
在前面阿銘未曾講過sed的這種用法,其實也不難弄明白,只是用分號把兩個替換的子命令塊給替換了而已。然后我們再到客戶端主機Aming上測試:
[root@Aming ~]# rsync -avL 192.168.0.10::test/test1/ /tmp/test9/
receiving incremental file list
created directory /tmp/test9
./
1
1.txt
2
2.txt
3
4
test.txt
sent 162 bytes received 410 bytes 1144.00 bytes/sec
total size is 0 speedup is 0.00
注意,這里不用再加test這個用戶了,默認是以root的身份拷貝的,現在已經不需要輸入密碼了。
-
Linux
+關注
關注
87文章
11345瀏覽量
210406 -
SSH
+關注
關注
0文章
189瀏覽量
16406
原文標題:rsync應用實例
文章出處:【微信號:aming_linux,微信公眾號:阿銘linux】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
評論