简体   繁体   中英

How to take sql backup?

I want to take a backup of my database. I am using mysqldump which is giving me an empt SQL file.

<?php
   $dbhost = 'localhost';
   $dbuser = 'root';
   $dbpass = '';
   $dbname = 'motel';
  echo  $backup_file = "motel\\".$dbname . date("Y-m-d-H-i-s") . '.sql';
  echo "<br>".$command = "mysqldump -h$dbhost -u$dbuser -p$dbpass $dbname >$backup_file";

 system($command);
?>

There is no error and not expected output. tell me where I am making a mistake, As I said I am getting a SQL file every time I run this code but empty.

Try This

take a tutorial : https://chartio.com/resources/tutorials/importing-from-and-exporting-to-files-using-the-mysql-command-line/

Exporting from MySQL The best tool for exporting a MySQL database to a text file is mysqldump.

To use mysqldump, you will need to know the login credentials of an appropriate MySQL user that has the necessary privileges to export the database in question.

With that information in hand, enter the mysqldump command with the appropriate flags and options:

$ mysqldump -u my_username -p database_name > output_file_path

The options in use are:

The -u flag indicates that the MySQL username will follow. The -p flag indicates we should be prompted for the password associated with the above username. database_name is of course the exact name of the database to export. The > symbol is a Unix directive for STDOUT, which allows Unix commands to output the text results of the issued command to another location. In this case, that output location is a file path, specified by output_file_path. Note: It is generally advisable to input the fully qualified path and filename for the output_file_path, so the resulting file is generated exactly where you want it.

For example, to export the books database as the book_admin user to the ~/backup/database directory, we might use the following command:

$ mysqldump -u book_admin -p books > ~/backup/database/books.sql

Enter password: After entering our password when prompted above, this command then creates our backup file with a .sql suffix (which is completely optional but advisable) in the appropriate directory.

By default, mysqldump will not save commands which attempt to modify the existence of the actual database. Instead, by default, only actual tables (and their respective data) are saved and thus will be prepared for later import using this file. If you need the ability to export (and later recreate) one more more databases, read up on the --databases flag in the official documentation.

Just try this php code, this will backup your db into a specific folder

 <?php

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '******';
$dbname = "db";
$tables = '*';

function backup_tables($host, $user, $pass, $dbname, $tables = '*') {
    $link = mysqli_connect($host, $user, $pass, $dbname);
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        exit;
    }
    mysqli_query($link, "SET NAMES 'utf8'");
    if ($tables == '*') {
        $tables = array();
        $result = mysqli_query($link, 'SHOW TABLES');
        while ($row = mysqli_fetch_row($result)) {
            $tables[] = $row[0];
        }
    } else {
        $tables = is_array($tables) ? $tables : explode(',', $tables);
    }
    $return = '';
    foreach ($tables as $table) {
        $result = mysqli_query($link, 'SELECT * FROM ' . $table);
        $num_fields = mysqli_num_fields($result);
        $num_rows = mysqli_num_rows($result);
        $return.= 'DROP TABLE IF EXISTS ' . $table . ';';
        $row2 = mysqli_fetch_row(mysqli_query($link, 'SHOW CREATE TABLE ' . $table));
        $return.= "

" . $row2[1] . ";

";
        $counter = 1;
        for ($i = 0;$i < $num_fields;$i++) {
            while ($row = mysqli_fetch_row($result)) {
                if ($counter == 1) {
                    $return.= 'INSERT INTO ' . $table . ' VALUES(';
                } else {
                    $return.= '(';
                }
                for ($j = 0;$j < $num_fields;$j++) {
                    $row[$j] = addslashes($row[$j]);
                    $row[$j] = str_replace("
", "
", $row[$j]);
                    if (isset($row[$j])) {
                        $return.= '"' . $row[$j] . '"';
                    } else {
                        $return.= '""';
                    }
                    if ($j < ($num_fields - 1)) {
                        $return.= ',';
                    }
                }
                if ($num_rows == $counter) {
                    $return.= ");
";
                } else {
                    $return.= "),
";
                }
                ++$counter;
            }
        }
        $return.= "


";
    }
    date_default_timezone_set('Asia/Kolkata');
   $date=date('Y-m-d'); 

 $target_dir="E:\DbBackup\ $date";  

if(!is_dir($target_dir))    {   

mkdir($target_dir,0777);

}   
$fileName = $target_dir.'\db-backup.sql'; 

  $handle = fopen($fileName, 'w+');  
 fwrite($handle, $return);   
}
?>

call the function :-

backup_tables($dbhost, $dbuser, $dbpass, $dbname, $tables);

the mysqldump command may be not recognized by the system you need to set the full binary path for mysqldump.exe

Note: In my case c:\\wamp64\\bin\\mysql\\mysql5.7.26\\bin

Verify in your case correct path also check the output file path

  <?php
   $dbhost = 'localhost';
   $dbuser = 'root';
   $dbpass = '';
   $dbname = 'motel';
   $backup_file = $dbname . date("Y-m-d-H-i-s") . '.sql';

  /*******
    if password is empty then no need to use password within command line 
    (if password present then only add --password=your_pass)
    Add mysql bin path to work mysqldump properlly and also set oputput file path 
    ******/
    $cmd  = "c: & cd c:\wamp64\bin\mysql\mysql5.7.26\bin & mysqldump.exe --user=$dbuser --host=$dbhost  $dbname> c:\wamp64\www\motel\\$backup_file";
   echo $cmd;

 system($command);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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