Ssh

scp 在 cron 中不起作用

  • August 19, 2013

是的,我知道它很經典。我已經用Google搜尋了它,但它仍然不起作用。我有以下腳本:

#First go to SVN repo folder
cd $svnrepos


# Just make sure we have write access to backup-folder
if [ -d "$bakdest" ] && [ -w "$bakdest" ] ; then
 # Now $repo has folder names = project names
 for repo in *; do
   # do svn dump for each project
   echo "Taking backup/svndump for: $repo"
   echo "Executing : svnadmin dump $repo > $bakdest/$repo-$bakdate.svn.dump \n"
   # Now finally execute the backup
   svnadmin dump $repo > $bakdest/$repo-$bakdate.svn.dump
   # You can go an extra mile by applying tar-gz compression to svn-dumps

   # We also would like to save the dump to remote place/usb
     # USB/other directory exists, copy the dump there
     echo "Going to copy $repo dump to $baktousb directory...\n"
     /usr/bin/scp -v $bakdest/$repo-$bakdate.svn.dump $baktousb/$repo-$bakdate.svn.dump
 done
else
 echo "Unable to continue SVN backup process."
 echo "$bakdest is *NOT* a directory or you do not have write permission."
fi


# End of backup script
echo "\n\n================================="
echo " - Backup Complete, THANK YOU :-]"

在 shell 中一切正常,但是當它作為 cron 作業執行時,它根本不會 scp(但會創建備份)。是的,我有一個空的釋義。無法理解為什麼它不起作用。

問題是您可能ssh-agent在互動式環境中執行,但不在 cron 中執行,並且您的 ssh 密鑰文件名與預設文件名不同。

要解決此問題,您可以在 scp 命令行中顯式指定 ssh 密鑰,即,scp -i $SSH_KEY_FILENAME或為您的主機指定適當的~/.ssh/config條目,即:

Host backuphost
   IdentityFile SSH_KEY_FILENAME

要測試您的腳本,您可以嘗試執行它來env -i /path/to/your/script重置您的環境並模仿 cron 環境。

引用自:https://unix.stackexchange.com/questions/59293