note 33617 added to function.system
| From: | chris at karakas-online dot de | Date: | Tue, 01 Jul 2003 23:52:42 +0000 |
| Subject: | note 33617 added to function.system | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-51328@lists.php.net to get a copy of this message | ||
If your PHP installation permits execution of commands through system() (e.g. you are not running in
safe mode, or, if you are, safe_mode_exec_dir contains all the commands you need), you can trigger a
backup of your MySQL database, just by pointing your browser to the following script:
<?php
// Enter your MySQL access data
$host= 'dbhost';
$user= 'dbuser';
$pass= 'dbpassword';
$db= 'db';
$backupdir = 'backups';
// Compute day, month, year, hour and min.
$today = getdate();
$day = $today[mday];
if ($day < 10) {
$day = "0$day";
}
$month = $today[mon];
if ($month < 10) {
$month = "0$month";
}
$year = $today[year];
$hour = $today[hours];
$min = $today[minutes];
$sec = "00";
// Execute mysqldump command.
// It will produce a file named $db-$year$month$day-$hour$min.gz
// under $DOCUMENT_ROOT/$backupdir
system(sprintf(
'mysqldump --opt -h %s -u %s -p%s %s | gzip > %s/%s/%s-%s%s%s-%s%s.gz',
$host,
$user,
$pass,
$db,
getenv('DOCUMENT_ROOT'),
$backupdir,
$db,
$year,
$month,
$day,
$hour,
$min
));
echo '+DONE';
?>
If you are not allowed cron access on the web server, you can set up your own cron job to
periodically call the above script. If you don't have cron, or a similar functionality on your
system, you can still modify the above script to inform the browser to reget the file every xxx
hours. A poor man's cron, so to say ;-)
Of course, the $backupdir should at least be protected with a .htaccess file.
And of course, you are not going to backup a really large database this way, if your PHP has some
timeout set (as is usually the case with web hosters).
----
Manual Page -- http://www.php.net/manual/en/function.system.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33617
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33617&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33617&report=yes