linux安装rsync在各主机之间同步文件
生活随笔
收集整理的這篇文章主要介紹了
linux安装rsync在各主机之间同步文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
概述
rsync可用于同步本地主機和遠程主機的文件,在搭建集群環境時尤為常用。
此處以3臺虛擬機作為例子,分別192.168.25.132、192.168.25.133、192.168.25.134
安裝rsync
所有主機都要安裝,并且服務都要啟動
yum install rsync # 啟動服務 systemctl start rsyncd.service # 設置開機啟動 systemctl enable rsyncd.servicersync命令
rsync -rvl [傳輸文件或目錄Path] [用戶]@[遠程IP]:[遠程存放目錄]舉個例子:
cd /usr/local #創建個測試文件test.txt touch test.txt #執行rsync rsync -rvl ./test.txt root@192.168.25.133:/usr/local #輸入命令后,會彈出遠程主機密碼,直接輸入即可 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主機,查看文件有傳過來,驗證通過
多主機傳輸
手動執行rsync存在一個問題,如果主機有多臺,一個個執行效率很慢,我們可以寫個shell腳本,只需要傳遞目錄參數,就可以實現批次傳輸。
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'#循環主機 hosts=('192.168.25.133' '192.168.25.134') for host in ${hosts[@]} doecho --------------- cluster$host ----------------rsync -rvl $p $user@$host:$fpdir done保存退出,需要提升權限:
chmod 777 xsync #執行測試,把/usr/local/elasticsearch-7.6.0目錄傳到192.168.25.133、192.168.25.134 ./xsync ../elasticsearch-7.6.0 #然后分別根據提示輸入遠程密碼即可查看結果:
這樣說明驗證通過,每次使用傳遞一個目錄參數就可以了。
總結
以上是生活随笔為你收集整理的linux安装rsync在各主机之间同步文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: systemctl常用命令
- 下一篇: xftp提示过期的解决方法