Windows XP Windows 7 Windows 2003 Windows Vista Windows教程綜合 Linux 系統教程
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟件教程
 Windows教程網 >> Windows教程綜合 >> 系統常見問題解答 >> Linux如何通過腳本實現遠程自動備份

Linux如何通過腳本實現遠程自動備份

日期:2017/4/18 11:47:19      編輯:系統常見問題解答

  為了防止數據丟失我們常常做的就是在電腦上備份這些數據,不過雖說備份了數據比較安全,但是萬一數據所在的電腦發生問題怎麼辦呢,考慮到在本機上備份數據,一旦該機器硬盤出現故障,數據無法取出。遠程手動備份數據費時費力且不及時。最好的方法就是通過腳本實現遠程自動互備。但遠程無論是通過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 [email protected]:/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上測試通過。

Copyright © Windows教程網 All Rights Reserved