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)中获取所有客户端的db信息,并循环遍历每个表以在该客户端的目录中创建备份文件。这个脚本在手动执行时效果很好。我遇到的问题是,当我将其设置为作为Cron作业运行时,它不起作用。来自Cron作业的电子邮件包含每次尝试备份DB的错误:

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

因此,无论出于何种原因,Cron作业无法创建/写入DB转储文件。我搞不懂这个。这似乎很奇怪,脚本工作完美时,手动执行。有人有什么想法吗?提前感谢!

亚当

考虑使用绝对路径。Cron可能有不同的基目录

最新更新