rsync的基本操作
2019獨角獸企業重金招聘Python工程師標準>>>
| 導讀 | rsync是linux系統下的數據鏡像備份工具。使用快速增量備份工具Remote Sync可以遠程同步,支持本地復制,與其他SSH、rsync主機同步數據。 | 
一、rsync命令的用法:
基本格式:rsync [選項] 原始位置 目標位置
 常用選項:
-a 歸檔模式,遞歸并保留對象屬性,等同于 -rlptgoD
 -v 顯示同步過程的詳細(verbose)信息
 -z 在傳輸文件時進行壓縮(compress)
 -H 保留硬鏈接文件
 -A 保留ACL屬性
 --delete 刪除目標位置有而原始位置沒有的文件
 -r 遞歸模式,包含目錄及子目錄中所有文件
 -l 對于軟鏈接文件仍然復制為軟鏈接文件
 -p 保留文件的權限標記
 -t 保留文件的時間標記
 -g 保留文件的屬組標記(僅超級用戶使用)
 -o 保留文件的屬主標記(僅超級用戶使用)
 -D 保留設備文件及其他特殊文件
二、配置rsync
在配置rsync前,先來做個小測試:
服務端
#在服務端網站首頁寫入一些內容 [root@localhost Desktop]# cd /var/www/html [root@localhost html]# vim index.html [root@localhost html]# cat index.html Hello World! Hello Jaking! [root@localhost html]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:BE:68:3F inet addr:192.168.142.132 Bcast:192.168.142.255 Mask:255.255.255.0inet6 addr: fe80::20c:29ff:febe:683f/64 Scope:LinkUP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:580 errors:0 dropped:0 overruns:0 frame:0TX packets:390 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:57739 (56.3 KiB) TX bytes:41856 (40.8 KiB)lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0inet6 addr: ::1/128 Scope:HostUP LOOPBACK RUNNING MTU:16436 Metric:1RX packets:16 errors:0 dropped:0 overruns:0 frame:0TX packets:16 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:0 RX bytes:960 (960.0 b) TX bytes:960 (960.0 b) [root@localhost rsync]# service httpd restart Stopping httpd: [ OK ] Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName[ OK ]客戶端
#客戶端能成功訪問服務端網站首頁的內容 [root@localhost Desktop]# curl 192.168.142.132 Hello World! Hello Jaking!剛剛的小測試其實是基于SSH實現的,rsync有兩種同步源,一種是基于SSH的同步源,另一種是基于rsync的同步源。
三、基于SSH的同步源
設置ACL權限:setfacl -m user:用戶名:rwx /服務器目錄
下行同步:rsync -avz 用戶名@服務器地址:/服務器目錄 /本地目錄
上行同步:rsync -avz /本地目錄 用戶名@服務器地址:/服務器目錄
為確保服務端的數據能同步到客戶端,接下來,我先從SSH的同步源開始配置:
 在配置前,分別在服務端和客戶端上執行yum install -y rsync,確保rsync已安裝。
1.在服務端授權一個用戶,也就是創建一個用戶:
[root@localhost html]# useradd server [root@localhost html]# passwd server Changing password for user server. New password: BAD PASSWORD: The password is shorter than 8 characters Retype new password: passwd: all authentication tokens updated successfully.2.在客戶端創建ssh目錄,同步服務端數據:
[root@localhost Desktop]# mkdir /client [root@localhost Desktop]# cd /client/ [root@localhost client]# mkdir ssh [root@localhost client]# rsync -avz server@192.168.142.132:/var/www/html/* /client/ssh server@192.168.142.132's password: receiving incremental file list index.htmlsent 68 bytes received 219 bytes 114.80 bytes/sec total size is 27 speedup is 0.0930 bytes received 104 bytes 15.76 bytes/sec total size is 27 speedup is 0.20 [root@localhost client]# cd ssh [root@localhost ssh]# ls index.html [root@localhost ssh]# cat index.html Hello World! Hello Jaking! #客戶端已成功同步服務端數據3.剛剛的同步是下行同步,即從服務器端把數據同步到客戶端。接下來我將演示一遍上行同步,即把客戶端的數據同步到服務端:
#在客戶端創建新文件,準備同步到服務端。 [root@localhost ssh]# touch a.txt b.txt [root@localhost ssh]# ls a.txt b.txt index.html [root@localhost ssh]# rsync -avz /client/ssh/* server@192.168.142.132:/var/www/html server@192.168.142.132's password: sending incremental file list a.txt b.txt rsync: mkstemp "/var/www/html/.a.txt.6JDDzO" failed: Permission denied (13) rsync: mkstemp "/var/www/html/.b.txt.p7hCLz" failed: Permission denied (13)sent 131 bytes received 50 bytes 40.22 bytes/sec total size is 27 speedup is 0.15 rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9] #同步失敗,從報錯結果可以server用戶權限不足,server用戶對/var/www/html目錄沒有寫權限。4.在服務端設置比較安全的ACL權限:
[root@localhost html]# setfacl -m user:server:rwx /var/www/html5.再次在客戶端執行上行同步操作:
[root@localhost ssh]# rsync -avz /client/ssh/* server@192.168.142.132:/var/www/html server@192.168.142.132's password: sending incremental file list a.txt b.txtsent 131 bytes received 50 bytes 51.71 bytes/sec total size is 27 speedup is 0.15 #由同步的過程可以看出,index.html沒有被上傳,由此可知rsync使用的同步機制是增量備份的機制。在服務端查看:
[root@localhost html]# ls a.txt b.txt index.html #客戶端數據已成功同步到服務端四、基于rsync的同步源
/etc/rsyncd_users.db文件權限必須是600
 **做上行同步時,nobody需要有寫入權限。 **
rsync -avz 用戶名@服務器地址::共享模塊名 /本地目錄
rsync -avz rsync://用戶名@服務器地址/共享模塊名 /本地目錄
使用SSH的同步源需要創建用戶,對于服務器來說,存在過多的用戶不是一件好事。而用基于rsync的同步源則不需要創建用戶,指定的用戶只需寫在配置文件里即可,這樣的用戶是虛擬用戶。
1.修改配置文件:
服務端
[root@localhost html]# vim /etc/rsyncd.conf #若配置文件不存在則直接創建 [root@localhost html]# cat /etc/rsyncd.conf address = 192.168.142.132 port 873 pid file = /var/run/rsyncd.pid log file = /var/log/rsyncd.log[share]comment = softpath = /server/rsyncread only = yesdont compress = *.gz *.bz2 *.zipauth users = wangsecrets file = /etc/rsyncd_users.db [root@localhost html]# vim /etc/rsyncd_users.db [root@localhost html]# cat /etc/rsyncd_users.db wang:123456 #rsync不支持復雜密碼,盡量設簡單一點。 [root@localhost html]# vim /etc/xinetd.d/rsync [root@localhost html]# cat /etc/xinetd.d/rsync # default: off # description: The rsync server is a good addition to an ftp server, as it \ # allows crc checksumming etc. service rsync {disable = yesflags = IPv6socket_type = streamwait = nouser = rootserver = /usr/bin/rsyncserver_args = --daemonlog_on_failure += USERID }[root@localhost html]# rsync --daemon #啟動rsync [root@localhost html]# netstat -pantu | grep 873 tcp 0 0 192.168.142.132:873 0.0.0.0:* LISTEN 6779/rsync [root@localhost html]# mkdir -p /server/rsync [root@localhost html]# cd !$ cd /server/rsync [root@localhost rsync]# touch rsync.txt [root@localhost rsync]# ls rsync.txt [root@localhost rsync]# chmod 600 /etc/rsyncd_users.db #一定要給密碼文件賦予600權限,否則同步數據將出錯!2.執行同步操作:
客戶端
[root@localhost rsync]# rsync -avz wang@192.168.142.132::share /client/rsync Password: receiving incremental file list ./ rsync.txtsent 77 bytes received 151 bytes 50.67 bytes/sec total size is 0 speedup is 0.00 [root@localhost rsync]# ls rsync.txt #數據同步成功 [root@localhost rsync]# pwd /client/rsync下行同步已完成,接下來我將演示上行同步:
服務端
#在執行上行同步前一定要修改模塊權限和ACL權限 [root@localhost rsync]# vim /etc/rsyncd.conf [root@localhost rsync]# cat /etc/rsyncd.conf address = 192.168.142.132 port 873 pid file = /var/run/rsyncd.pid log file = /var/log/rsyncd.log[share]comment = softpath = /server/rsyncread only = no #這里一定要改為nodont compress = *.gz *.bz2 *.zipauth users = wangsecrets file = /etc/rsyncd_users.db [root@localhost rsync]# setfacl -m u:nobody:rwx /srver/rsync #設置ACL權限 [root@localhost rsync]# pkill rsync #關閉rsync [root@localhost rsync]# rsync --daemon #啟動rsync客戶端
[root@localhost rsync]# touch client.txt [root@localhost rsync]# rsync -avz /client/rsync/* wang@192.168.142.132::share Password: sending incremental file list client.txtsent 85 bytes received 27 bytes 32.00 bytes/sec total size is 0 speedup is 0.00 #上行同步成功在服務端查看:
[root@localhost rsync]# ls client.txt rsync.txt [root@localhost rsync]# pwd /server/rsync3.上行同步的另一種格式:
客戶端
[root@localhost rsync]# ls client.txt rsync.txt [root@localhost rsync]# touch test.txt [root@localhost rsync]# rsync -avz /client/rsync/* rsync://wang@192.168.142.132/share Password: sending incremental file list test.txtsent 102 bytes received 27 bytes 28.67 bytes/sec total size is 0 speedup is 0.00服務端
[root@localhost rsync]# ls client.txt rsync.txt test.txt五、配置免密碼驗證
1、基于SSH的同步源
通過秘鑰對實現
 客戶端
2、基于rsync的同步源
通過系統變量實現
 RSYNC_PASSWORD
 客戶端
原文來自:?https://www.linuxprobe.com/rsync-basic-operation.html
轉載于:https://my.oschina.net/ssdlinux/blog/2994283
總結
以上是生活随笔為你收集整理的rsync的基本操作的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 张勇谈组织架构调整:领导者要善于“从后排
- 下一篇: 前端之选择器-37
