在phpMyAdmin中,有一个选项可以将整个表(或数据库)导出到.SQL文件中。
我无法弄清楚他们是如何做到这一点的 - 因为我想把它放在CRON工作中,以便每天和每周备份数据库。但似乎没有任何一个命令可以做到 - 是吗?
答案 0 :(得分:3)
您可以使用PHP运行这些命令,将以下代码段复制到文件中并使用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;
}
?>
答案 1 :(得分:2)
是的,mysqldump:http://linuxcommand.org/man_pages/mysqldump1.html
您需要具有服务器访问权限才能使用它。此外,如果您喜欢冒险并希望保持一致的备份,请查看有关主/从设置的信息。
答案 2 :(得分:1)
使用mysqldump
mysqldump -h hostname -u user -pPassword dbname >/path/to/file
如果host是localhost,则host是可选的。根据某些设置,用户和密码也可以是可选的。请注意,您可能想要添加日期名称
mysqldump -h hostname -u root mydb > /path/to/backup/$(date +%a)
答案 3 :(得分:0)
您可以创建一个shell脚本并通过cron job调用该脚本。 请参阅此工作Example