Backup script cron job

I wan to set a backup script as a cron job in order to frequently backup my database.
I use this script that i found here:

DIR=`date +%d-%m-%y`
mkdir $DEST

mysqldump -h "mysql_hostname" -u "mysql_user"  -p "mysql_password" "mysql_database_name" > dbbackup.sql



changed the rights of the to 750 (execution rights)
and created a cron job (pick 2 minutes, left hours empty).

Till now, even though the database is small i dont see any backup.


Our cron jobs tool allows you to call a URL on your website. We don’t support bash files here, so your cron job call will fail.

With cron jobs, you need to use PHP.


Thanks for the info.
I tried this but doesn’t look to work. Is there any limitation?

<?php //i take these info from page $result=exec('mysqldump myDBname --host=myDBhost --password=myDBpassword --user=myDBuser --single-transaction > myBackupFile.sql',$output); if(empty($output)){ echo ('everything fine: ' . $output);} else { echo('something is wrong'); }

It prints "everything fine: " without any output, or sql file created. Is the platform capable to create such files?


  • We don’t have mysqldump installed.
  • All functions that try to run system commands are disabled on our hosting - you cannot run system commands on our servers. So even if we did have mysqldump installed, you would not be able to use it.

Technically, yes. phpMyAdmin and Adminer can generate SQL dumps without using MySQL command line tools. But as far as I know, they basically reconstruct the SQL dumps themselves, and I don’t think it’s easy to do that yourself.


Thanks for the info.

I created this php file:

include "db.php";
$tableNames = ['table1', 'table2'];
$backupsFolder = 'backups/';

foreach($tableNames as $tableName){
    $filenameSQL= $backupsFolder . 'database_backup_' . $tableName . '_'. date("Y-m-d_H:i:s").'.sql';
    $query = "SELECT * FROM " . $tableName;

    $stmt = $conn->prepare($query);
    $backupSuccess = $stmt->execute();
    $tableRows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $backupString = "";
    $numOfLines = 0;
    foreach($tableRows as $row){
        $backupString .= implode(',', $row) . "\r\n";
    echo $backupString;
    file_put_contents($filenameSQL, $backupString);

If i run

from the browser, the backup files are successfully created. But, cron job cannot do that…

Why not? A cron job just calls the URL on your site. If you can do it through the browser, you can do it through a cron job.

If you want, I can check it for you. But to do that, I need actual, real information about where this is running, not just example values and placeholders.


It looks strange to me why this doesn’t work.

Here is my script link where you can check:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.