1

I am trying to run a php script without cron and succeeded but doesn't work as I expected. I want the script to make a backup every 50 minutes, but it doesn't do that, it makes them continuous. This is what I tried:

cron.php

<?php
$interval=50; //minutes
set_time_limit(0);
ignore_user_abort(true);
while (true)
{
    $now=time();
    include("backup.php");
    sleep($interval*60-(time()-$now));
}
?>

backup.php

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$username = "user";
$password = "pass";
$hostname = "dbs";
$dbname   = "dbs_name";

// if mysqldump is on the system path you do not need to specify the full path
// simply use "mysqldump --add-drop-table ..." in this case
$dumpfname = $dbname . "_" . date("Y-m-d_H-i-s").".sql";
$command = "mysqldump --add-drop-table --host=$hostname --user=$username ";
if ($password)
$command.= "--password=". $password ." ";
$command.= $dbname;
$command.= " > " . $dumpfname;
system($command);

// zip the dump file
$zipfname = $dbname . "_" . date("Y-m-d_H-i-s").".zip";
$zip = new ZipArchive();
if($zip->open($zipfname,ZIPARCHIVE::CREATE))
{
    $zip->addFile($dumpfname,$dumpfname);
    $zip->close();
}

// read zip file and send it to standard output
if (file_exists($zipfname)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($zipfname));
    flush();
    //readfile($zipfname);
    //exit;
}
?>

1 Answer 1

1

First read this: PHP is meant to die.

For your Problem, You have several things to consider:

Also sleep, is not very acurate (but for this usecase acurate enough). If you realy don't have access to cron, i would check the both steps about. If you have access to cron, i would realy consider to use it (If your on windows, take a look at scheduled tasks).

A workaround could be, on every visitor call, you check for a file lastbackup.txt and check for the last backup time. If it is older then 50 minutes/not availible, you could write the current timestamp in this file and execute the backup script after flushing output.

Sure, with this way it will not 100% executed every 50 Minutes, but if it will not, there is nothing big to backup


Max execution time Nginx:

http {
    #...
        fastcgi_read_timeout 300; 
    #...
}

Max execution time Apache:

FastCgiServer /var/www/cgi-bin/php-cgi-5.3.1 -idle-timeout 120

Max exectuion time IIS:

<system.web>
    <httpRuntime executionTimeout="180" />
</system.web>

There is also a free web based cron availbile: https://cron-job.org/ (Don't know if it also on english), but i am sure, there is more then this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.