2011-12-10 36 views
0

我開發了一個腳本,我的公司通過創建其DB的轉儲文件來備份我們客戶的數據庫在我們的服務器上。這裏是我的代碼:PHP cron作業無法創建新文件

$result=mysql_query("SELECT * FROM backup_sites"); 
while($row=mysql_fetch_array($result)){ 

    $backup_directory=$row["backup_directory"]; 
    $thishost=$row["host"]; 
    $thisusername=$row["username"]; 
    $thispassword=$row["password"]; 
    $thisdb_name=$row["db_name"]; 

    $link=mysql_connect("$thishost", "$thisusername", "$thispassword"); 
    if($link){ 
     mysql_select_db("$thisdb_name"); 
     $backupFile = $thisdb_name ."-". date("Y-m-d-H-i-s", strtotime ("+1 hour")) . '.sql'; 
     $command = "/usr/bin/mysqldump -h $thishost -u $thisusername -p$thispassword $thisdb_name > site-backups/$backup_directory/$backupFile"; 
     system($command); 
    } 
} 

該腳本會從一個表(backup_sites)所有客戶的數據庫信息,並通過每一個循環來創建客戶端目錄中的備份文件。該腳本在手動執行時效果很好。我遇到的問題是,當我將它設置爲以Cron作業運行時,它不起作用。從cron作業的電子郵件中包含此錯誤的每個數據庫備份嘗試:

sh: site-backups/CLIENT_DIRECTORY/DB_DUMP_FILE.sql: No such file or directory 

所以,不管出於什麼原因,cron作業是無法創建/寫數據庫轉儲文件。我無法弄清楚這一點。當手動執行腳本時,這似乎很奇怪。有人有主意嗎?提前致謝!

Adam

+0

你正在使用什麼cron命令? – 472084

回答

6

考慮使用絕對路徑。 Cron可能有不同的基本目錄。

+0

哇。咄!謝謝你的提示!我不敢相信我沒有想到這一點。我實際上在另一個版本的腳本中使用絕對,但無論出於什麼原因,都沒有在這個腳本中包含它。謝謝! – BWDesign