日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
詳解rsync基本使用方法

rsync是可以實(shí)現(xiàn)增量備份的工具。配合任務(wù)計(jì)劃,rsync能實(shí)現(xiàn)定時(shí)或間隔同步,配合inotify或sersync,可以實(shí)現(xiàn)觸發(fā)式的實(shí)時(shí)同步,下面為大家詳細(xì)講解一下rsync基本使用方法。

一、rsync命令的用法:

基本格式:rsync [選項(xiàng)] 原始位置 目標(biāo)位置 常用選項(xiàng):

-a 歸檔模式,遞歸并保留對(duì)象屬性,等同于 -rlptgoD -v 顯示同步過(guò)程的詳細(xì)(verbose)信息 -z 在傳輸文件時(shí)進(jìn)行壓縮(compress) -H 保留硬鏈接文件 -A 保留ACL屬性 –delete 刪除目標(biāo)位置有而原始位置沒(méi)有的文件 -r 遞歸模式,包含目錄及子目錄中所有文件 -l 對(duì)于軟鏈接文件仍然復(fù)制為軟鏈接文件 -p 保留文件的權(quán)限標(biāo)記 -t 保留文件的時(shí)間標(biāo)記 -g 保留文件的屬組標(biāo)記(僅超級(jí)用戶(hù)使用) -o 保留文件的屬主標(biāo)記(僅超級(jí)用戶(hù)使用) -D 保留設(shè)備文件及其他特殊文件

二、配置rsync

在配置rsync前,先來(lái)做個(gè)小測(cè)試:

服務(wù)端

#在服務(wù)端網(wǎng)站首頁(yè)寫(xiě)入一些內(nèi)容
[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.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 restart
Stopping httpd:                                            [  OK  ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName                                                           [  OK  ] 

客戶(hù)端

#客戶(hù)端能成功訪問(wèn)服務(wù)端網(wǎng)站首頁(yè)的內(nèi)容
[root@localhost Desktop]# curl 192.168.142.132
Hello World!
Hello Jaking!

剛剛的小測(cè)試其實(shí)是基于SSH實(shí)現(xiàn)的,rsync有兩種同步源,一種是基于SSH的同步源,另一種是基于rsync的同步源。

三、基于SSH的同步源

設(shè)置ACL權(quán)限:setfacl -m user:用戶(hù)名:rwx /服務(wù)器目錄 下行同步:rsync -avz 用戶(hù)名@服務(wù)器地址:/服務(wù)器目錄 /本地目錄 上行同步:rsync -avz /本地目錄 用戶(hù)名@服務(wù)器地址:/服務(wù)器目錄

為確保服務(wù)端的數(shù)據(jù)能同步到客戶(hù)端,接下來(lái),我先從SSH的同步源開(kāi)始配置: 在配置前,分別在服務(wù)端和客戶(hù)端上執(zhí)行yum install -y rsync,確保rsync已安裝。

1.在服務(wù)端授權(quán)一個(gè)用戶(hù),也就是創(chuàng)建一個(gè)用戶(hù):

[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.在客戶(hù)端創(chuàng)建ssh目錄,同步服務(wù)端數(shù)據(jù):

[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.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! #客戶(hù)端已成功同步服務(wù)端數(shù)據(jù) 

3.剛剛的同步是下行同步,即從服務(wù)器端把數(shù)據(jù)同步到客戶(hù)端。接下來(lái)我將演示一遍上行同步,即把客戶(hù)端的數(shù)據(jù)同步到服務(wù)端:

#在客戶(hù)端創(chuàng)建新文件,準(zhǔn)備同步到服務(wù)端。
[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] #同步失敗,從報(bào)錯(cuò)結(jié)果可以server用戶(hù)權(quán)限不足,server用戶(hù)對(duì)/var/www/html目錄沒(méi)有寫(xiě)權(quán)限。 

4.在服務(wù)端設(shè)置比較安全的ACL權(quán)限:

[root@localhost html]# setfacl -m user:server:rwx /var/www/html

5.再次在客戶(hù)端執(zhí)行上行同步操作:

[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 sent 131 bytes  received 50 bytes  51.71 bytes/sec total size is 27  speedup is 0.15 #由同步的過(guò)程可以看出,index.html沒(méi)有被上傳,由此可知rsync使用的同步機(jī)制是增量備份的機(jī)制。 

在服務(wù)端查看:

[root@localhost html]# ls
a.txt  b.txt  index.html
#客戶(hù)端數(shù)據(jù)已成功同步到服務(wù)端

四、基于rsync的同步源

/etc/rsyncd_users.db文件權(quán)限必須是600 做上行同步時(shí),nobody需要有寫(xiě)入權(quán)限。 rsync -avz 用戶(hù)名@服務(wù)器地址::共享模塊名 /本地目錄 rsync -avz rsync://用戶(hù)名@服務(wù)器地址/共享模塊名 /本地目錄

使用SSH的同步源需要?jiǎng)?chuàng)建用戶(hù),對(duì)于服務(wù)器來(lái)說(shuō),存在過(guò)多的用戶(hù)不是一件好事。而用基于rsync的同步源則不需要?jiǎng)?chuàng)建用戶(hù),指定的用戶(hù)只需寫(xiě)在配置文件里即可,這樣的用戶(hù)是虛擬用戶(hù)。

1.修改配置文件:

服務(wù)端

[root@localhost html]# vim /etc/rsyncd.conf
#若配置文件不存在則直接創(chuàng)建
[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 = 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.db
wang:123456 #rsync不支持復(fù)雜密碼,盡量設(shè)簡(jiǎn)單一點(diǎn)。
[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 #啟動(dòng)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權(quán)限,否則同步數(shù)據(jù)將出錯(cuò)!

2.執(zhí)行同步操作:

客戶(hù)端

[root@localhost rsync]# rsync -avz wang@192.168.142.132::share /client/rsync
Password:
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]# ls
rsync.txt
#數(shù)據(jù)同步成功
[root@localhost rsync]# pwd
/client/rsync

下行同步已完成,接下來(lái)我將演示上行同步:

服務(wù)端

#在執(zhí)行上行同步前一定要修改模塊權(quán)限和ACL權(quán)限
[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 = 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 #設(shè)置ACL權(quán)限
[root@localhost rsync]# pkill rsync #關(guān)閉rsync
[root@localhost rsync]# rsync --daemon #啟動(dòng)rsync

客戶(hù)端

[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.txt

sent 85 bytes  received 27 bytes  32.00 bytes/sec
total size is 0  speedup is 0.00
#上行同步成功

在服務(wù)端查看:

[root@localhost rsync]# ls
client.txt  rsync.txt
[root@localhost rsync]# pwd
/server/rsync

3.上行同步的另一種格式:

客戶(hù)端

[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.txt

sent 102 bytes  received 27 bytes  28.67 bytes/sec
total size is 0  speedup is 0.00

服務(wù)端

[root@localhost rsync]# ls
client.txt  rsync.txt  test.txt

五、配置免密碼驗(yàn)證

1、基于SSH的同步源

通過(guò)秘鑰對(duì)實(shí)現(xiàn) 客戶(hù)端

[root@localhost ssh]# pwd
/client/ssh
[root@localhost ssh]# ls
a.txt  b.txt  index.html
[root@localhost ssh]# rm -rf *
[root@localhost ssh]# 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:
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用戶(hù)在服務(wù)端
id: server: No such user
[root@localhost ssh]# ssh server@192.168.142.132
[server@localhost ~]$ ifconfig
#成功登錄服務(wù)端
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 ~]$ exit
logout
Connection 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
#現(xiàn)在執(zhí)行同步操作不需要輸入密碼
sent 68 bytes  received 219 bytes  191.33 bytes/sec
total size is 27  speedup is 0.09
[root@localhost ssh]# ls
a.txt  b.txt  index.html
#被刪除的文件又從服務(wù)端同步過(guò)來(lái)了

2、基于rsync的同步源

通過(guò)系統(tǒng)變量實(shí)現(xiàn) RSYNC_PASSWORD 客戶(hù)端

[root@localhost client]# cd rsync/
[root@localhost rsync]# ls
client.txt  rsync.txt  test.txt
[root@localhost rsync]# rm -rf *
[root@localhost rsync]# export RSYNC_PASSWORD=123456 #123456為虛擬用戶(hù)wang的密碼
[root@localhost rsync]# rsync -avz wang@192.168.142.132::share /client/rsync
receiving incremental file list
./
client.txt
rsync.txt
test.txt
#現(xiàn)在執(zhí)行同步操作不需要輸入密碼
sent 115 bytes  received 265 bytes  760.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost rsync]# ls
client.txt  rsync.txt  test.txt
#被刪除的文件又從服務(wù)端同步過(guò)來(lái)了

當(dāng)前標(biāo)題:詳解rsync基本使用方法
本文URL:http://m.5511xx.com/article/dhjgipg.html