简体   繁体   English

使用php备份数据库mysql

[英]backup database mysql using php

I found this code 我找到了这段代码

Run mysqldump using system() function 使用system()函数运行mysqldump

The system() function is used to execute an external program. system()函数用于执行外部程序。 Because MySQL already have built in tool for creating MySQL database backup (mysqldump) let's use it from our PHP script 因为MySQL已经内置了用于创建MySQL数据库备份(mysqldump)的工具,所以我们从PHP脚本中使用它

<?php
include 'config.php';
include 'opendb.php';

$backupFile = $dbname . date("Y-m-d-H-i-s") . '.gz';
$command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $backupFile";
system($command);

include 'closedb.php';
?>

From HERE . 这里 It is on how to backup database. 它是关于如何备份数据库的。 I want to back up my database on button click or every month depending on the need so i search for a tutorial i found one but it does not clearly say much like 我想根据需要单击按钮或每月备份我的数据库,所以我搜索了我发现的一个教程,但它说的不像

  1. Where can i find the backup file if it was created. 如果已创建备份文件,我在哪里可以找到它。
  2. Do i need to save it as .sql file or it is saved as sql already 我需要将其另存为.sql文件还是已另存为sql
  3. If saved as .sql where can i find it 如果另存为.sql ,在哪里可以找到

UPDATE

I used 我用了

    $mysqldump=exec('which mysqldump');
    $command = "$mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname > $dbname.sql";
    exec($command);

But i get a blank file and the size is 0 kb 但我得到一个空白文件,大小为0 kb

I think writing a custom PHP script is overkill. 我认为编写自定义PHP脚本太过分了。 If you need to backup your database monthly you can use cron, elsewhere use phpmyadmin. 如果您需要每月备份数据库,则可以使用cron,在其他地方使用phpmyadmin。

You wil find backup file in "./" (directory of actually running script) 您将在“ ./”(实际运行的脚本目录)中找到备份文件。

Its saved like .sql witch is gziped 将其保存为.sql女巫,例如gziped

mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip

.SQL you can find in .gz file 您可以在.gz文件中找到.SQL

$backupFile = $dbname . date("Y-m-d-H-i-s") . '.gz';

You can use this script in cron for your monthly backup and in some "admin panel" by button for additional backups 您可以在cron中使用此脚本进行每月备份,并可以在“管理面板”中通过按钮使用此脚本进行其他备份

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

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