例子
• rsync -av /etc/passwd /tmp/1.txt
• rsync -av /tmp/1.txt 192.168.188.128:/tmp/2.txtlinux
rsync格式
• rsync [OPTION] … SRC DEST
• rsync [OPTION] … SRC [user@]host:DEST
• rsync [OPTION] … [user@]host:SRC DEST
• rsync [OPTION] … SRC [user@]host::DEST
• rsync [OPTION] … [user@]host::SRC DESTvim
• -a 包含-rtplgoD • -r 同步目錄時要加上,相似cp時的-r選項 • -v 同步時顯示一些信息,讓咱們知道同步的過程 • -l 保留軟鏈接 • -L 加上該選項後,同步軟連接時會把源文件給同步 • -p 保持文件的權限屬性 • -o 保持文件的屬主 • -g 保持文件的屬組 • -D 保持設備文件信息 • -t 保持文件的時間屬性 • --delete 刪除DEST中SRC沒有的文件 • --exclude 過濾指定文件,如--exclude 「logs」會把文件名包含logs的文件或者目錄過濾掉,不一樣步 • -P 顯示同步過程,好比速率,比-v更加詳細 • -u 加上該選項後,若是DEST中的文件比SRC新,則不一樣步 • -z 傳輸時壓縮 • -e 指定端口安全
rsync = remote(遠程) sync(同步)
使用以前須要安裝rsync包bash
yum install -y rsync [root@linux-128 ~]# yum install -y rsync
需求:我要將A目錄下的文件拷貝到B目錄下(本機操做) 通常咱們想到的是使用命令cp來拷貝,可是另一個需求是:每一小時拷貝一次,若是文件比較大或者多,認爲去操做就比較繁瑣了,因此咱們使用rsync命令來作 例子:服務器
[root@linux-128 tmp]# rsync -av /etc/passwd /tmp/rsync.txt sending incremental file list passwd sent 1424 bytes received 31 bytes 2910.00 bytes/sec total size is 1350 speedup is 0.93
需求2:將A機器/tmp/rsync.txt文件拷貝到B機器192.168.88.129/tmp/目錄下ssh
拷貝的時候發下錯誤,是由於B機器上沒有安裝rsync,因此須要先安裝一下socket
[root@linux-128 tmp]# rsync -av /tmp/rsync.txt root@192.168.88.129:/tmp/ sending incremental file list rsync.txt sent 1427 bytes received 31 bytes 2916.00 bytes/sec total size is 1350 speedup is 0.93
備註:這裏命令也能夠寫成
rsync -av /tmp/rsync.txt 192.168.88.129:/tmp/ \若是B機器和A機器的當前用戶同樣,就能夠不加root@,若是A機器當前用戶是user1,你命令裏面沒有加B機器的用戶名@,它就會報錯
咱們在B機器/tmp/目錄下就能夠看到了ui
[root@linux-129 ~]# ls /tmp/ rsync.txt systemd-private-441df47e85a6457486521b5a6973cce1-vmtoolsd.service-b8vozP
• rsync -av test1/ 192.168.133.132:/tmp/test2/ \A-B
• rsync -av 192.168.133.132:/tmp/test2/ /root/test1 \B-A命令行
若是對方機器sshd端口不是22呢,因此咱們就要指定對方機器的shhd端口了
• rsync -av -e "ssh -p 1122" test1/ 192.168.133.132:/tmp/test2/日誌
• 要編輯配置文件/etc/rsyncd.conf • 啓動服務rsync --daemon • 格式:rsync -av test1/ 192.168.133.130::module/dir/ 若是要用服務的方式同步,須要先啓動rsync服務,而後編輯配置文件 vim /etc/rsyncd.conf 這裏有個rsyncd.conf 樣例:咱們直接負責進去,修改一下配置
port=873 log file=/var/log/rsync.log pid file=/var/run/rsyncd.pid address=192.168.133.130 [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.133.132 1.1.1.1 2.2.2.2 192.168.133.0/24
[root@linux-129 ~]# rsync -av /tmp/1.txt 192.168.88.129::test/test.txt rsync: failed to connect to 192.168.88.129 (192.168.88.129): Connection refused (111) rsync error: error in socket IO (code 10) at clientserver.c(122) [sender=3.0.9]
這裏報錯了:沒有路由到遠程=機器上去
[root@linux-129 ~]# ping 192.168.88.128 \\先 查看是IP是否通暢 PING 192.168.88.128 (192.168.88.128) 56(84) bytes of data. 64 bytes from 192.168.88.128: icmp_seq=1 ttl=64 time=2.66 ms 64 bytes from 192.168.88.128: icmp_seq=2 ttl=64 time=0.927 ms
[root@linux-129 ~]# telnet 192.168.88.129 837 \\用命令telnet來查看端口是否通的 -bash: telnet: 未找到命令 [root@linux-129 ~]# yum install -y telnet [root@linux-129 ~]# telnet 192.168.88.128 837 Trying 192.168.88.128... telnet: connect to address 192.168.88.128: No route to host \\說明端口不通
遇到這種問題咱們要檢測一下是否是iptables的問題
[root@linux-129 ~]# systemctl stop firewalld \\關閉將服務端和客戶端的防火牆先關閉 [root@linux-129 ~]# telnet 192.168.88.128 873 Trying 192.168.88.128... Connected to 192.168.88.128. \\這就表示通了 Escape character is '^]'. @RSYNCD: 30.0 ^] \\按ctrl+] 而後輸入quit退出 telnet> quit Connection closed.
[root@linux-129 ~]# rsync -avP /tmp/1.txt 192.168.88.128::test/test.txt Password: @ERROR: auth failed on module test rsync error: error starting client-server protocol (code 5) at main.c(1516) [sender=3.0.9]
這裏有報錯了,提示密碼錯誤;由於咱們在修改rsync配置文件的時候,有定義密碼文件,咱們將它先註釋掉。
[root@linux-129 ~]# rsync -avP /tmp/1.txt 192.168.88.128::test/test.txt sending incremental file list 1.txt 1372 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1) sent 1441 bytes received 27 bytes 978.67 bytes/sec total size is 1372 speedup is 0.93
這是從客戶端同步數據到客戶端
咱們在從服務端將數據同步回來
[root@linux-129 ~]# rsync -avP 192.168.88.128::test/test.txt /tmp/test.txt receiving incremental file list test.txt 1372 100% 1.31MB/s 0:00:00 (xfer#1, to-check=0/1) sent 45 bytes received 1475 bytes 3040.00 bytes/sec total size is 1372 speedup is 0.90
若是服務端rsync的端口是8730,客戶端是沒有開放這個端口,因此咱們要指定端口 --port
[root@linux-129 ~]# rsync -avP --port 8730 192.168.88.128::test/test.txt /tmp/test.txt receiving incremental file list sent 26 bytes received 60 bytes 172.00 bytes/sec total size is 1372 speedup is 15.95
ssh服務指定端口是--e "ssh -p 1122"
• port:指定在哪一個端口啓動rsyncd服務,默認是873端口。
• log file:指定日誌文件。
• pid file:指定pid文件,這個文件的做用涉及服務的啓動、中止等進程管理操做。
• address:指定啓動rsyncd服務的IP。假如你的機器有多個IP,就能夠指定由其中一個啓動rsyncd服務,若是不指定該參數,默認是在所有IP上啓動。
• []:指定模塊名,裏面內容自定義。
• path:指定同步數據存放的路徑。
• use chroot true|false:表示在傳輸文件前首先chroot到path參數所指定的目錄下。這樣作的緣由是實現額外的安全防禦,但缺點是須要以roots權限,而且不能備份指向外部的符號鏈接所指向的目錄文件。好比你要同步的文件裏面有軟鏈接,軟鏈接的源地址在其餘目錄下,若是use chroot的值是true,那麼你在同步的時候加上-L 想要將軟鏈接的源文件同步過去的時候,它是會報錯的同步失敗,不加-L,它能同步成功,只是同步過的軟連接是失效的。默認狀況下chroot值爲true,若是你的數據當中有軟鏈接文件,阿銘建議你設置成false。 • max connections:指定最大的鏈接數,默認是0,即沒有限制。若是有不少客戶端都在重服務器上同步數據,給服務器帶來壓力,咱們就能夠設置這個最大鏈接數。
• read only ture|false:read只讀,若是爲true,則不能上傳到該模塊指定的路徑下。
• list:表示當用戶查詢該服務器上的可用模塊時,該模塊是否被列出,設定爲true則列出,false則隱藏。這裏可用模塊就是咱們定義的[test];咱們在::後面不加內容,它就會列出模塊來,別人就知道這個模塊,他會上傳一些木馬竊取信息,因此爲了安全,設置成false
• uid/gid:指定傳輸文件時以哪一個用戶/組的身份傳輸。
• auth users:指定傳輸時要使用的用戶名。
• secrets file:指定密碼文件,該參數連同上面的參數若是不指定,則不使用密碼驗證。注意該密碼文件的權限必定要是600。格式:用戶名:密碼
• hosts allow:表示被容許鏈接該模塊的主機,能夠是IP或者網段,若是是多個,中間用空格隔開。192.168.133.132 1.1.1.1 2.2.2.2 192.168.133.0/24
• 當設置了auth users和secrets file後,咱們須要在服務端的/etc/rsyncd.passwd 文件寫入用戶名和密碼格式(test:123123)客戶端連服務端也須要用用戶名密碼了,
[root@linux-128 ~]# vim /etc/rsyncd.passwd [root@linux-128 ~]# chmod 600 !$ \\設置權限,必定要設置權限 chmod 600 /etc/rsyncd.passwd [root@linux-129 ~]# rsync -avP --port 8730 test@192.168.88.128::test/test.txt /tmp/test.txt Password: receiving incremental file list sent 54 bytes received 101 bytes 34.44 bytes/sec total size is 1372 speedup is 8.85
若不想在命令行中帶上密碼,能夠在客戶端上建立一個一個密碼文件/etc/pass,這個密碼文件格式和服務端設置的密碼文件格式不同,它只須要寫入和服務端密碼同樣就行(123123)。
[root@linux-129 ~]# vim /tmp/pass [root@linux-129 ~]# chmod 600 !$ \\設置權限,必定要設置權限 chmod 600 /tmp/pass [root@linux-129 ~]# rsync -avP --port 8730 --password-file=/tmp/pass test@192.168.88.128::test/test.txt /tmp/test.txt receiving incremental file list sent 54 bytes received 101 bytes 310.00 bytes/sec total size is 1372 speedup is 8.85
• rsync -avL test@192.168.133.130::test/test1/ /tmp/test8/ --password-file=/etc/pass
• 其中/etc/pass內容就是一個密碼,權限要改成600
總結:rsync遠程同步分爲經過ssh同步,和服務的方式同步 ssh方式同步:一個冒號 服務方式同步:: 2個冒號