日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

rsync的基本操作

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

2019獨角獸企業(yè)重金招聘Python工程師標準>>>

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

一、rsync命令的用法:

基本格式:rsync [選項] 原始位置 目標位置
常用選項:

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

二、配置rsync

在配置rsync前,先來做個小測試:

服務端

#在服務端網(wǎng)站首頁寫入一些內(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ǎng)站首頁的內(nèi)容 [root@localhost Desktop]# curl 192.168.142.132 Hello World! Hello Jaking!

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

三、基于SSH的同步源

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

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

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

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

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

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

4.在服務端設置比較安全的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 #由同步的過程可以看出,index.html沒有被上傳,由此可知rsync使用的同步機制是增量備份的機制。

在服務端查看:

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

四、基于rsync的同步源

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

使用SSH的同步源需要創(chuàng)建用戶,對于服務器來說,存在過多的用戶不是一件好事。而用基于rsync的同步源則不需要創(chuàng)建用戶,指定的用戶只需寫在配置文件里即可,這樣的用戶是虛擬用戶。

1.修改配置文件:

服務端

[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不支持復雜密碼,盡量設簡單一點。 [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權(quán)限,否則同步數(shù)據(jù)將出錯!

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

下行同步已完成,接下來我將演示上行同步:

服務端

#在執(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 #設置ACL權(quán)限 [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/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

服務端

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

五、配置免密碼驗證

1、基于SSH的同步源

通過秘鑰對實現(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用戶在服務端 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.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 #被刪除的文件又從服務端同步過來了

2、基于rsync的同步源

通過系統(tǒng)變量實現(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 #被刪除的文件又從服務端同步過來了

原文來自:?https://www.linuxprobe.com/rsync-basic-operation.html

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

總結(jié)

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

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