Linux 下通過指令碼實現遠程自動備份

來源:互聯網
上載者:User

考慮到在本機上備份資料,一旦該機器硬碟出現故障,資料無法取出。遠程手動備份資料費時費力且 不及時。最好的方法就是通過指令碼實現遠程自動互備。但遠程無論是通過SSH登陸,還是通過scp拷貝檔案 都需要輸入密碼。為了克服這個問題,首先需要實現不要求輸入密碼的SSH登陸,這樣就可以使用rsync,scp ,rexec等命令來做的遠程備份了。

1. 設定無需密碼的ssh登陸,方法如下:

假設A,B兩伺服器,現在需要在A機上用root登陸B機,而不需要輸入密碼,那我們可按照下面的步驟 來做:

1)在A機上產生鑰匙對,執行以下命令:

ssh-keygen -t rsa

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa): /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:

f6:61:a8:27:35:cf:4c:6d:13:22:70:cf:4c:c8:a0:23 root@host1

這樣,在/root/.ssh/路徑下會產生id_rsa,和id_rsa.pub,其中id_rsa是密鑰,id_rsa.pub是公開金鑰。

2)把在A機產生的id_rsa.pub拷貝到B機上,假設拷貝到B機的臨時目錄下,如:

scp /root/.ssh/id_rsa.pub root@218.242.214.20:/tmp

3)用root帳號登陸B機,進入其主目錄,建立authorized_keys檔案,並設定好許可權。

cd ~/.ssh

cat /tmp/id_rsa.pub >>authorized_keys

chmod 400 authorized_keys

rm -f /tmp/id_rsa.pub

4)測試

在A機上轉到root帳號,嘗試登入B機。看看是不是不要密碼.

說明:

authorized_keys檔案的許可權很重要,如果設定為777,那麼登入的時候,還是需要提供密碼的。

記得將臨時目錄下的id_rsa.pub刪除,養成個好習慣。

本方法在Red Hat9.0上測試通過。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.