php 备份一个mysql数据库并作为文件下载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3751069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Backup a mysql database and download as a file
提问by mrN
How to backup the mysql database and download it as a .sql file by using PHP Codes
如何使用 PHP 代码备份 mysql 数据库并将其下载为 .sql 文件
采纳答案by Alexej Kubarev
A very simple solution would be something like (first example): http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/using-php-to-backup-mysql-databases.aspx
一个非常简单的解决方案将类似于(第一个示例):http: //www.php-mysql-tutorial.com/wikis/mysql-tutorials/using-php-to-backup-mysql-databases.aspx
Naturally this will only make a Data dump of the table.
当然,这只会对表进行数据转储。
What you could do is use this code:
您可以做的是使用以下代码:
http://snipplr.com/view/173/mysql-dump/
http://snipplr.com/view/173/mysql-dump/
What this code does is actually gets a description of the table (i.e its structure), creates all the tables and pushes data. pretty much like any other tool does.
这段代码所做的实际上是获取表的描述(即它的结构),创建所有表并推送数据。几乎就像任何其他工具一样。
Then its just a matter of saving it from string to a file (file_put_contents() for instance or something similar, depending on your preference and need)
然后它只是将它从字符串保存到文件(例如 file_put_contents() 或类似的东西,取决于你的偏好和需要)
回答by Delan Azabani
mysqldump -u username -p password database > file
Alternatively, phpMyAdmin can do this too with the Export tool.
或者,phpMyAdmin 也可以使用导出工具执行此操作。
回答by Jigar Joshi
Use phpmyadmin
Edit:
编辑:
You can use shell_execto execute this command
你可以使用 shell_exec来执行这个命令
mysqldump -u username -p password database > file
mysqldump -u 用户名 -p 密码数据库> 文件
This will generate a dump file,and then redirect user to this generated file.
这将生成一个转储文件,然后将用户重定向到这个生成的文件。
回答by Alex
Do you have phpmyadmin? If so you can export it from there by clicking "Export" at the top (on the selected table/db).
你有 phpmyadmin 吗?如果是这样,您可以通过单击顶部的“导出”(在所选表/数据库上)从那里导出它。
回答by Ricky Downey
I know its a bit late but hope someone else will find this.
我知道这有点晚了,但希望其他人能找到这个。
//php file - html code:
require_once 'connect.php'; //holds database variables with connect details
require_once 'Admin_DatabaseFiles_Backup.php'; //Include the admin logout script
<form action="" method="post" class="form form">
<!--<input type="hidden" name="backup" value="1" />-->
<div class="float_left w200">
<p>
<label class="title">Backup database</label>
<span class="left">
<input type="checkbox" name="db" value="1" checked="checked"/>
</span>
</p>
<p>
<label class="title">Backup files</label>
<span class="left">
<input type="checkbox" name="files" value="1" checked="checked" />
</span>
</p>
</div>
<p class="float_left">
<input type="submit" name="submit" value="Backup" class="button" />
</p>
</form>
//php file Admin_DatabaseFiles_Backup.php:
<?php
if ($_POST['submit']=="Backup"){
if ($_POST['db'] == "1"){
$directory = "DatabaseFileBackups/";
$dateAndTime = "".date('d-m-Y-H-i-s');
$fileName = "".$dbname.$dateAndTime.".sql";
$backupFile = "mysqldump --user=$dbuser --password='$dbpass' --host=$dbhost $dbname > ".$directory.$fileName;
exec($backupFile,$output);
if($output == ''){
echo = '<br />Failed To Backup Database!';
}else{
echo = '<br />Database Backup Was Successful!';
}
}
if ($_POST['files'] == "1"){
echo 'Seleceted files';
}
}
?>