简体   繁体   English

备份mysql数据库并下载为文件

[英]Backup a mysql database and download as a file

如何通过使用PHP代码备份mysql数据库并将其下载为.sql文件

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 (ie 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()或类似内容)即可。

mysqldump -u username -p password database > file

另外,phpMyAdmin也可以使用导出工具来执行此操作。

Use phpmyadmin 使用phpmyadmin

Edit: 编辑:

You can use shell_exec to 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. 这将生成一个转储文件,然后将用户重定向到此生成的文件。

Do you have phpmyadmin? 你有phpmyadmin吗? If so you can export it from there by clicking "Export" at the top (on the selected table/db). 如果是这样,您可以通过单击顶部(在所选表/数据库上)的“导出”从那里导出它。

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';

    }
}


?>

If you have phpMyAdmin you can do it at the Export menu. 如果您有phpMyAdmin,则可以在“导出”菜单中进行操作。

If you look for a command-line tool take a look at mysqldump . 如果您寻找命令行工具,请查看mysqldump

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM