您好,登錄后才能下訂單哦!
這篇文章主要講解了“rsync基本使用方法有哪些”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“rsync基本使用方法有哪些”吧!
rsync是可以實現增量備份的工具。配合任務計劃,rsync能實現定時或間隔同步,配合inotify或sersync,可以實現觸發式的實時同步。
基本格式:rsync [選項] 原始位置 目標位置 常用選項:
-a 歸檔模式,遞歸并保留對象屬性,等同于 -rlptgoD
-v 顯示同步過程的詳細(verbose)信息
-z 在傳輸文件時進行壓縮(compress)
-H 保留硬鏈接文件
-A 保留ACL屬性 –delete 刪除目標位置有而原始位置沒有的文件
-r 遞歸模式,包含目錄及子目錄中所有文件
-l 對于軟鏈接文件仍然復制為軟鏈接文件
-p 保留文件的權限標記
-t 保留文件的時間標記
-g 保留文件的屬組標記(僅超級用戶使用)
-o 保留文件的屬主標記(僅超級用戶使用)
-D 保留設備文件及其他特殊文件
在配置rsync前,先來做個小測試:
服務端
#在服務端網站首頁寫入一些內容[root@localhost Desktop]# cd /var/www/html[root@localhost html]# vim index.html[root@localhost html]# cat index.htmlHello World! Hello Jaking! [root@localhost html]# ifconfigeth0 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.0 inet6 addr: fe80::20c:29ff:febe:683f/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:580 errors:0 dropped:0 overruns:0 frame:0 TX packets:390 errors:0 dropped:0 overruns:0 carrier:0 collisions: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.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:16 errors:0 dropped:0 overruns:0 frame:0 TX packets:16 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:960 (960.0 b) TX bytes:960 (960.0 b) [root@localhost rsync]# service httpd restartStopping 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.132Hello World! Hello Jaking!
剛剛的小測試其實是基于SSH實現的,rsync有兩種同步源,一種是基于SSH的同步源,另一種是基于rsync的同步源。
設置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 serverChanging 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/sshserver@192.168.142.132's password: receiving incremental file list index.html sent 68 bytes received 219 bytes 114.80 bytes/sec total size is 27 speedup is 0.09 30 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]# lsa.txt b.txt index.html [root@localhost ssh]# rsync -avz /client/ssh/* server@192.168.142.132:/var/www/htmlserver@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/html
5.再次在客戶端執行上行同步操作:
[root@localhost ssh]# rsync -avz /client/ssh/* server@192.168.142.132:/var/www/htmlserver@192.168.142.132's password: sending incremental file list a.txt b.txt sent 131 bytes received 50 bytes 51.71 bytes/sec total size is 27 speedup is 0.15 #由同步的過程可以看出,index.html沒有被上傳,由此可知rsync使用的同步機制是增量備份的機制。
在服務端查看:
[root@localhost html]# lsa.txt b.txt index.html#客戶端數據已成功同步到服務端
/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.confaddress = 192.168.142.132 port 873 pid file = /var/run/rsyncd.pidlog file = /var/log/rsyncd.log [share] comment = soft path = /server/rsync read only = yes dont compress = *.gz *.bz2 *.zip auth users = wang secrets file = /etc/rsyncd_users.db [root@localhost html]# vim /etc/rsyncd_users.db[root@localhost html]# cat /etc/rsyncd_users.dbwang: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 = yes flags = IPv6 socket_type = stream wait = no user = root server = /usr/bin/rsync server_args = --daemon log_on_failure += USERID } [root@localhost html]# rsync --daemon #啟動rsync[root@localhost html]# netstat -pantu | grep 873tcp 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]# lsrsync.txt [root@localhost rsync]# chmod 600 /etc/rsyncd_users.db #一定要給密碼文件賦予600權限,否則同步數據將出錯!
2.執行同步操作:
客戶端
[root@localhost rsync]# rsync -avz wang@192.168.142.132::share /client/rsyncPassword: receiving incremental file list ./ rsync.txt sent 77 bytes received 151 bytes 50.67 bytes/sec total size is 0 speedup is 0.00 [root@localhost rsync]# lsrsync.txt#數據同步成功[root@localhost rsync]# pwd/client/rsync
下行同步已完成,接下來我將演示上行同步:
服務端
#在執行上行同步前一定要修改模塊權限和ACL權限[root@localhost rsync]# vim /etc/rsyncd.conf[root@localhost rsync]# cat /etc/rsyncd.confaddress = 192.168.142.132 port 873 pid file = /var/run/rsyncd.pidlog file = /var/log/rsyncd.log [share] comment = soft path = /server/rsync read only = no #這里一定要改為no dont compress = *.gz *.bz2 *.zip auth users = wang secrets 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::sharePassword: sending incremental file list client.txt sent 85 bytes received 27 bytes 32.00 bytes/sec total size is 0 speedup is 0.00#上行同步成功
在服務端查看:
[root@localhost rsync]# lsclient.txt rsync.txt [root@localhost rsync]# pwd/server/rsync
3.上行同步的另一種格式:
客戶端
[root@localhost rsync]# lsclient.txt rsync.txt [root@localhost rsync]# touch test.txt[root@localhost rsync]# rsync -avz /client/rsync/* rsync://wang@192.168.142.132/sharePassword: sending incremental file list test.txt sent 102 bytes received 27 bytes 28.67 bytes/sec total size is 0 speedup is 0.00
服務端
[root@localhost rsync]# lsclient.txt rsync.txt test.txt
1、基于SSH的同步源
通過秘鑰對實現 客戶端
[root@localhost ssh]# pwd/client/ssh [root@localhost ssh]# lsa.txt b.txt index.html [root@localhost ssh]# rm -rf *[root@localhost ssh]# ssh-keygenGenerating 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: 3d:fe:c8:0e:2c:b7:90:b0:f4:0d:31:af:b4:d3:9e:87 root@localhost.localdomain The key's randomart image is: +--[ RSA 2048]----+ | | | | | o | | + . | | o o S o | | . = O . . | | . O *.. | | *E=.o | | +o+ . | +-----------------+ [root@localhost ssh]# [root@localhost ssh]# ssh-copy-id server@192.168.142.132 server@192.168.142.132's password: Now try logging into the machine, with "ssh 'server@192.168.142.132'", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting. [root@localhost ssh]# id server #server用戶在服務端id: server: No such user [root@localhost ssh]# ssh server@192.168.142.132[server@localhost ~]$ 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.0 inet6 addr: fe80::20c:29ff:febe:683f/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:935 errors:0 dropped:0 overruns:0 frame:0 TX packets:660 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:112043 (109.4 KiB) TX bytes:89842 (87.7 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:16 errors:0 dropped:0 overruns:0 frame:0 TX packets:16 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:960 (960.0 b) TX bytes:960 (960.0 b) [server@localhost ~]$ exitlogoutConnection to 192.168.142.132 closed. [root@localhost ssh]# ls[root@localhost ssh]# pwd/client/ssh [root@localhost ssh]# rsync -avz server@192.168.142.132:/var/www/html/* /client/ssh/receiving incremental file list a.txt b.txt index.html#現在執行同步操作不需要輸入密碼sent 68 bytes received 219 bytes 191.33 bytes/sec total size is 27 speedup is 0.09 [root@localhost ssh]# lsa.txt b.txt index.html#被刪除的文件又從服務端同步過來了
2、基于rsync的同步源
通過系統變量實現 RSYNC_PASSWORD 客戶端
[root@localhost client]# cd rsync/[root@localhost rsync]# lsclient.txt rsync.txt test.txt [root@localhost rsync]# rm -rf *[root@localhost rsync]# export RSYNC_PASSWORD=123456 #123456為虛擬用戶wang的密碼[root@localhost rsync]# rsync -avz wang@192.168.142.132::share /client/rsyncreceiving incremental file list ./ client.txt rsync.txt test.txt#現在執行同步操作不需要輸入密碼sent 115 bytes received 265 bytes 760.00 bytes/sec total size is 0 speedup is 0.00 [root@localhost rsync]# lsclient.txt rsync.txt test.txt#被刪除的文件又從服務端同步過來了
感謝各位的閱讀,以上就是“rsync基本使用方法有哪些”的內容了,經過本文的學習后,相信大家對rsync基本使用方法有哪些這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。