1

在 phpMyAdmin 中,有一个选项可以将整个表(或数据库)导出到 .SQL 文件中。

我无法弄清楚他们是如何做到这一点的——因为我想把它放到 CRON 工作中,每天和每周备份数据库。但似乎没有任何单一的命令可以做到这一点 - 有吗?

4

4 回答 4

3

您可以使用 PHP 运行这些命令,将以下代码复制到文件中并使用 cron 作业执行它。

<?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;
}
?>
于 2013-03-05T12:45:55.930 回答
2

是的,mysqldump: http: //linuxcommand.org/man_pages/mysqldump1.html

不过,您需要拥有服务器访问权限才能使用它。此外,如果您喜欢冒险并希望保持一致的备份,请查找有关主/从设置的信息。

于 2013-03-05T12:19:54.933 回答
1

使用 mysqldump

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

如果主机是本地主机,主机是可选的。根据某些设置,用户和密码也可能是可选的。请注意,您可能需要添加日期名称

  mysqldump -h hostname -u root mydb > /path/to/backup/$(date +%a)
于 2013-03-05T12:26:13.527 回答
0

您可以创建一个 shell 脚本并通过 cron 作业调用该脚本。请参阅此工作示例

于 2013-03-05T12:21:37.457 回答