在Linux下使用rsync的6個(gè)實(shí)例

導(dǎo)讀對(duì)于各種組織和公司,數(shù)據(jù)對(duì)他們是最重要的,即使對(duì)于電子商務(wù),數(shù)據(jù)也是同樣重要的。Rsync是一款通過網(wǎng)絡(luò)備份重要數(shù)據(jù)的工具/軟件。

Rsync同樣是一個(gè)在類Unix和Window系統(tǒng)上通過網(wǎng)絡(luò)在系統(tǒng)間同步文件夾和文件的網(wǎng)絡(luò)協(xié)議。Rsync可以復(fù)制或者顯示目錄并復(fù)制文件。Rsync默認(rèn)監(jiān)聽TCP 873端口,通過遠(yuǎn)程shell如rsh和ssh復(fù)制文件。Rsync必須在遠(yuǎn)程和本地系統(tǒng)上都安裝。

rsync的主要好處是:

速度:最初會(huì)在本地和遠(yuǎn)程之間拷貝所有內(nèi)容。下次,只會(huì)傳輸發(fā)生改變的塊或者字節(jié)。

安全:傳輸可以通過ssh協(xié)議加密數(shù)據(jù)。

低帶寬:rsync可以在兩端壓縮和解壓數(shù)據(jù)塊。

語法:

#rsysnc [options] source path destination path

示例: 1 - 啟用壓縮

[root@localhost /]# rsync -zvr /home/aloft/ /backuphomedir

building file list ... done

bash_logout

bash_profile

bashrc

sent 472 bytes received 86 bytes 1116.00 bytes/sec

total size is 324 speedup is 0.58

上面的rsync命令使用了-z來啟用壓縮,-v是可視化,-r是遞歸。上面在本地的/home/aloft/和/backuphomedir之間同步。

示例: 2 - 保留文件和文件夾的屬性

[root@localhost /]# rsync -azvr /home/aloft/ /backuphomedir

building file list ... done

/

bash_logout

bash_profile

bashrc

sent 514 bytes received 92 bytes 1212.00 bytes/sec

total size is 324 speedup is 0.53

上面我們使用了-a選項(xiàng),它保留了所有人和所屬組、時(shí)間戳、軟鏈接、權(quán)限,并以遞歸模式運(yùn)行。

示例: 3 - 同步本地到遠(yuǎn)程主機(jī)

root@localhost /]# rsync -avz /home/aloft/ azmath@192.168.1.4:192.168.1.4:/share/rsysnctest/

Password:

building file list ... done

/

bash_logout

bash_profile

bashrc

sent 514 bytes received 92 bytes 1212.00 bytes/sec

total size is 324 speedup is 0.53

上面的命令允許你在本地和遠(yuǎn)程機(jī)器之間同步。你可以看到,在同步文件到另一個(gè)系統(tǒng)時(shí)提示你輸入密碼。在做遠(yuǎn)程同步時(shí),你需要指定遠(yuǎn)程系統(tǒng)的用戶名和IP或者主機(jī)名。

示例: 4 - 遠(yuǎn)程同步到本地

[root@localhost /]# rsync -avz azmath@192.168.1.4:192.168.1.4:/share/rsysnctest/ /home/aloft/

Password:

building file list ... done

/

bash_logout

bash_profile

bashrc

sent 514 bytes received 92 bytes 1212.00 bytes/sec

total size is 324 speedup is 0.53

上面的命令同步遠(yuǎn)程文件到本地。

示例: 5 - 找出文件間的不同

[root@localhost backuphomedir]# rsync -avzi /backuphomedir /home/aloft/

building file list ... done

cd+++++++ backuphomedir/

>f+++++++ backuphomedir/.bash_logout

>f+++++++ backuphomedir/.bash_profile

>f+++++++ backuphomedir/.bashrc

>f+++++++ backuphomedir/abc

>f+++++++ backuphomedir/xyz

sent 650 bytes received 136 bytes 1572.00 bytes/sec

total size is 324 speedup is 0.41

示例: 6 - 備份

rsync命令可以用來備份linux。

你可以在cron中使用rsync安排備份。

0 0 * * * /usr/local/sbin/bkpscript &> /dev/null

vi /usr/local/sbin/bkpscript

rsync -avz -e ‘ssh -p2093′ /home/test/ root@192.168.1.150:/oracle/data/

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容