linux安装rsync在各主机之间同步文件
生活随笔
收集整理的這篇文章主要介紹了
linux安装rsync在各主机之间同步文件
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
概述
rsync可用于同步本地主機(jī)和遠(yuǎn)程主機(jī)的文件,在搭建集群環(huán)境時(shí)尤為常用。
此處以3臺虛擬機(jī)作為例子,分別192.168.25.132、192.168.25.133、192.168.25.134
安裝rsync
所有主機(jī)都要安裝,并且服務(wù)都要啟動(dòng)
yum install rsync # 啟動(dòng)服務(wù) systemctl start rsyncd.service # 設(shè)置開機(jī)啟動(dòng) systemctl enable rsyncd.servicersync命令
rsync -rvl [傳輸文件或目錄Path] [用戶]@[遠(yuǎn)程IP]:[遠(yuǎn)程存放目錄]舉個(gè)例子:
cd /usr/local #創(chuàng)建個(gè)測試文件test.txt touch test.txt #執(zhí)行rsync rsync -rvl ./test.txt root@192.168.25.133:/usr/local #輸入命令后,會(huì)彈出遠(yuǎn)程主機(jī)密碼,直接輸入即可 root@192.168.25.133's password: sending incremental file list test.txtsent 87 bytes received 35 bytes 22.18 bytes/sec total size is 0 speedup is 0.00切換到192.168.25.133主機(jī),查看文件有傳過來,驗(yàn)證通過
多主機(jī)傳輸
手動(dòng)執(zhí)行rsync存在一個(gè)問題,如果主機(jī)有多臺,一個(gè)個(gè)執(zhí)行效率很慢,我們可以寫個(gè)shell腳本,只需要傳遞目錄參數(shù),就可以實(shí)現(xiàn)批次傳輸。
cd /usr/local/bin touch xsyncxsync腳本如下:
if [ x"$1" = x ]; then echo "no cmd param!"exit 1 fi#相對路徑 p=$1if [ -f "$p" ] thenfpdir="$(cd "$(dirname "$p")"; pwd)"elsefpdir="$(cd $p; pwd)"fi#賬號 user='root'#循環(huán)主機(jī) hosts=('192.168.25.133' '192.168.25.134') for host in ${hosts[@]} doecho --------------- cluster$host ----------------rsync -rvl $p $user@$host:$fpdir done保存退出,需要提升權(quán)限:
chmod 777 xsync #執(zhí)行測試,把/usr/local/elasticsearch-7.6.0目錄傳到192.168.25.133、192.168.25.134 ./xsync ../elasticsearch-7.6.0 #然后分別根據(jù)提示輸入遠(yuǎn)程密碼即可查看結(jié)果:
這樣說明驗(yàn)證通過,每次使用傳遞一個(gè)目錄參數(shù)就可以了。
總結(jié)
以上是生活随笔為你收集整理的linux安装rsync在各主机之间同步文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: systemctl常用命令
- 下一篇: Nginx教程系列二:Linux安装ng