BWDesign
BWDesign

Reputation: 640

PHP cron job can't create new files

I have developed a script that my company uses to back up our client's DBs on our server by creating a dump file of their DBs. Here is my code:

$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);   
    }
}

The script gets all the client's db information from a table (backup_sites) and loops through each one to create the backup file in that client's directory. This script works great when it is manually executed. The problem that I am having is that it does not work when I set it to run as a Cron job. The email from the Cron job contains this error for each DB backup attempt:

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

So for whatever reason, the Cron job is unable to create/write the DB dump file. I can't figure this one out. It just seems strange that the script works perfectly when executed manually. Anyone have any ideas? Thanks in advance!

Adam

Upvotes: 1

Views: 2195

Answers (1)

Udo Held
Udo Held

Reputation: 12548

Consider using absolute pathes. Cron may have a different base directory.

Upvotes: 7

Related Questions