Create MySQL dumps with PHP

From Servage Wiki
Jump to: navigation, search

How to make MySQL dumps using PHP?

To make a MySQL dump with PHP, you'll simply have to execute the following command in your PHP script:
(The code below as one line in your PHP-script).

passthru("/usr/bin/mysqldump --opt --host=$dbhost --user=$dbuser --password=$dbpwd $dbname > $dumpfile");

MYSQL-HOST = MySQL server adress (mysql10**.servage.net).
USERNAME = the username for the database.
PASSWORD = the password for the database.
DATABASENAME = the name on the database.

Another bit of code that does the same job, better coded maybe.


<?php

$dbhost   = "mysql10**.servage.net";
$dbuser   = "USERNAME";
$dbpwd    = "PASSWORD";
$dbname   = "DATABASENAME";
$dumpfile = $dbname . "_" . date("Y-m-d_H-i-s") . ".sql";

passthru("/usr/bin/mysqldump --opt --host=$dbhost --user=$dbuser --password=$dbpwd $dbname > $dumpfile");

// report - disable with // if not needed
// must look like "-- Dump completed on ..." 

echo "$dumpfile "; passthru("tail -1 $dumpfile");

?>

Tested and it's working on Servage's servers (PHP5).
Turn OFF the SAFE MODE ! before using this code.
It will create a SQL file in the same dir as the PHP-file.
Make the directory writable (CHMOD 777).
Tip: Use CRON to run the PHP-script to do automatic backups.

The SQL-files in your folder is good to go.
Just click the link "Import database" and follow the instructions.
Just use the predefind choices and that all.
It will uniqly name each file, with date and time: date("Y-m-d_H-i-s").
Y-m-d_H-i-s becomes for example 2009-03-03_21-00-00.
That's 3rd of March, 2009, 9 PM sharp.
Go the to retrive the letters to change the date/time stamp above.

Personal tools
Navigation