1

In phpMyAdmin, there is an option to export the entire table (or database) into a .SQL file.

I cannot work out how they do this - as I want to stick it into a CRON job to make daily and weekly backups of databases. But there doesn't seem to be any single command that does it - is there?

2

4 Answers 4

3

You can run these commands with PHP, copy the following piece of code into a file and execute it with cron job.

<?php

//Backup:

//ENTER THE RELEVANT INFO BELOW
$mysqlDatabaseName ='databasename';
$mysqlUserName ='username';
$mysqlPassword ='password';
$mysqlHostName ='localhost';
//$mysqlExportPath ='database.sql';

$mysqlImportFilename ='myownsit_obsolcms.sql';

//DONT EDIT BELOW THIS LINE
//Export the database and output the status to the page
$command='mysql -h' .$mysqlHostName .' -u' .$mysqlUserName .' -p' .$mysqlPassword .' ' .$mysqlDatabaseName .' < ' .$mysqlImportFilename;
exec($command,$output=array(),$worked);
switch($worked){
    case 0:
        echo 'Import file <b>' .$mysqlImportFilename .'</b> successfully imported to database <b>' .$mysqlDatabaseName .'</b>';
        break;
    case 1:
        echo 'There was an error during import. Please make sure the import file is saved in the same folder as this script and check your values:<br/><br/><table><tr><td>MySQL Database Name:</td><td><b>' .$mysqlDatabaseName .'</b></td></tr><tr><td>MySQL User Name:</td><td><b>' .$mysqlUserName .'</b></td></tr><tr><td>MySQL Password:</td><td><b>NOTSHOWN</b></td></tr><tr><td>MySQL Host Name:</td><td><b>' .$mysqlHostName .'</b></td></tr><tr><td>MySQL Import Filename:</td><td><b>' .$mysqlImportFilename .'</b></td></tr></table>';
        break;
}



//Restore:
$mysqlExportPath ='path_to_file/database.sql';

//DONT EDIT BELOW THIS LINE
//Export the database and output the status to the page
$command='mysqldump --opt -h' .$mysqlHostName .' -u' .$mysqlUserName .' -p' .$mysqlPassword .' ' .$mysqlDatabaseName .' admin contents categires google_map cms_labels news pages page_contents partners photos plugins products social testimonials themes users youtube> ~/' .$mysqlExportPath;
//$command = "$ mysqldump -u $mysqlUserName -p $mysqlPassword $mysqlDatabaseName > myownsit_obsolcms.sql";
exec($command,$output=array(),$worked);
switch($worked){
    case 0:
        //echo 'Database <b>' .$mysqlDatabaseName .'</b> successfully exported to <b>~/' .$mysqlExportPath .'</b>';
        break;
    case 1:
        echo 'There was a warning during the export of <b>' .$mysqlDatabaseName .'</b> to <b>~/' .$mysqlExportPath .'</b>';
        break;
    case 2:
        echo 'There was an error during export. Please check your values:<br/><br/><table><tr><td>MySQL Database Name:</td><td><b>' .$mysqlDatabaseName .'</b></td></tr><tr><td>MySQL User Name:</td><td><b>' .$mysqlUserName .'</b></td></tr><tr><td>MySQL Password:</td><td><b>NOTSHOWN</b></td></tr><tr><td>MySQL Host Name:</td><td><b>' .$mysqlHostName .'</b></td></tr></table>';
        break;
}
?>
Sign up to request clarification or add additional context in comments.

Comments

2

Yeah, mysqldump: http://linuxcommand.org/man_pages/mysqldump1.html

You'll need to have server access to use it though. Also, if you're feeling adventurous and want to maintain consistent backups look up information on master/slave setups.

Comments

1

use mysqldump

  mysqldump -h hostname -u user -pPassword dbname >/path/to/file

host is optional if host is localhost. depending on some settings user and password may be optional as well. Note you may want to add the date name

  mysqldump -h hostname -u root mydb > /path/to/backup/$(date +%a)

Comments

0

You can create a shell script and call that script via cron job. See this Working Example

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.