在不使用cron的情况下运行php脚本以执行自动数据库备份
我正在尝试运行一个没有cron的php脚本,但成功了,但没有像我预期的那样工作。我希望脚本每50分钟进行一次备份,但它不能做到这一点,它使它们连续运行。这就是我所尝试的: cron.php在不使用cron的情况下运行php脚本以执行自动数据库备份,php,mysql,cron,Php,Mysql,Cron,我正在尝试运行一个没有cron的php脚本,但成功了,但没有像我预期的那样工作。我希望脚本每50分钟进行一次备份,但它不能做到这一点,它使它们连续运行。这就是我所尝试的: cron.php <?php $interval=50; //minutes set_time_limit(0); ignore_user_abort(true); while (true) { $now=time(); include("backup.php"); sleep($interval
<?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;
}
?>
首先阅读以下内容:
对于您的问题,您需要考虑以下几点:
- 是
设置时间限制(0)代码>允许吗
- 您的Web服务器也可以有一个单独的
lastbackup.txt
,并检查上次备份时间。如果时间超过50分钟/不可用,则可以在该文件中写入当前时间戳并执行
当然,通过这种方式,它不会每50分钟100%执行一次,但如果不会,那么就没有什么大的备份了
最大执行时间: 最大执行时间: 最大执行时间:
还有一个免费的基于网络的cron可用文件:(不知道它是否也使用英语),但我确信,还有更多
http {
#...
fastcgi_read_timeout 300;
#...
}
FastCgiServer /var/www/cgi-bin/php-cgi-5.3.1 -idle-timeout 120
<system.web>
<httpRuntime executionTimeout="180" />
</system.web>