日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

rsync的基本操作

發(fā)布時(shí)間:2023/12/20 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 rsync的基本操作 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

導(dǎo)讀rsync是linux系統(tǒng)下的數(shù)據(jù)鏡像備份工具。使用快速增量備份工具Remote Sync可以遠(yuǎn)程同步,支持本地復(fù)制,與其他SSH、rsync主機(jī)同步數(shù)據(jù)。

一、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í)用戶使用)
-o 保留文件的屬主標(biāo)記(僅超級(jí)用戶使用)
-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.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 ]

客戶端

#客戶端能成功訪問(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:用戶名:rwx /服務(wù)器目錄
下行同步:rsync -avz 用戶名@服務(wù)器地址:/服務(wù)器目錄 /本地目錄
上行同步:rsync -avz /本地目錄 用戶名@服務(wù)器地址:/服務(wù)器目錄

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

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

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

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

#在客戶端創(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用戶權(quán)限不足,server用戶對(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.再次在客戶端執(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.txtsent 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 #客戶端數(shù)據(jù)已成功同步到服務(wù)端

四、基于rsync的同步源

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

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

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 = 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不支持復(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 = yesflags = IPv6socket_type = streamwait = nouser = rootserver = /usr/bin/rsyncserver_args = --daemonlog_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í)行同步操作:

客戶端

[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 #數(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 = 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 #設(shè)置ACL權(quán)限 [root@localhost rsync]# pkill rsync #關(guān)閉rsync [root@localhost rsync]# rsync --daemon #啟動(dòng)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 #上行同步成功

在服務(wù)端查看:

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

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

客戶端

[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

服務(wù)端

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

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

1、基于SSH的同步源

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

[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_keysto make sure we haven't added extra keys that you weren't expecting.[root@localhost ssh]# id server #server用戶在服務(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.0inet6 addr: fe80::20c:29ff:febe:683f/64 Scope:LinkUP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:935 errors:0 dropped:0 overruns:0 frame:0TX packets:660 errors:0 dropped:0 overruns:0 carrier:0collisions: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.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)[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
客戶端

[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為虛擬用戶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)了

原文來(lái)自:?https://www.linuxprobe.com/rsync-basic-operation.html

轉(zhuǎn)載于:https://my.oschina.net/ssdlinux/blog/2994283

總結(jié)

以上是生活随笔為你收集整理的rsync的基本操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。