note 33617 deleted from function.system by cmb

From: Date: Sun, 08 Dec 2019 11:43:20 +0000
Subject: note 33617 deleted from function.system by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-213475@lists.php.net to get a copy of this message
Note Submitter: chris at karakas-online dot de ---- 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).

« previous php.notes (#213475) next »