按日期拆分数据库

Split the database by date

我有大型 MySQL 生产数据库 (14 GB)。有时我必须下载它并在本地处理它。这当然是问题。数据库有 10 个带字段“日期”的表和 5 个没有字段“日期”的表。

有什么简单的方法可以下载上个月的吗? 10 个条件为“data > '2020-07-24'”的表和完整的 5 个其他表。我想将它转储到一个 .sql 文件中,然后在本地导入它。 不幸的是,数据库在 10 个表之间有一些关系...

是的,看看 mysqldump 的 --where 选项。

试试这个代码,它必须是用任何表

创建列CREATED_DATE

我希望它会起作用:)

如果您需要使用 GoogleDrive 进行自动备份 https://github.com/engacs/Autobackup-databse-to-GoogleDrive/

<?php

// ini_set('memory_limit', '1024M'); 

// User home directory (absolute)
$homedir = "backup/"; // If this doesn't work, you can provide the full path yourself
// Site directory (relative)
$sitedir = ""; 
// Base filename for backup file
$fprefix = "sitebackup-";
// Base filename for database file
$dprefix = "dbbackup-";
// MySQL username
$dbuser = "root";
// MySQL password
$dbpass = "";
// MySQL database
$dbname = "";
// MySQL Host
$host = "localhost";

// Get connection object and set the charset
$conn = mysqli_connect($host, $dbuser, $dbpass, $dbname);
$conn->set_charset("utf8");
// Get All Table Names From the Database
$tables = array();
$sql = "SHOW TABLES";
$result = mysqli_query($conn, $sql);

while ($row = mysqli_fetch_row($result)) {
    $tables[] = $row[0];
}
$sqlScript = "";
foreach ($tables as $table) {
    
    // Prepare SQLscript for creating table structure
    $query = "SHOW CREATE TABLE $table";
    $result = mysqli_query($conn, $query);
    $row = mysqli_fetch_row($result);
    
    $sqlScript .= "\n\n" . $row[1] . ";\n\n";
    
    $form_date='2020-08-01';  // Split From Date 
    $to_date='2020-08-31'; // Split to Date

    $query = "SELECT * FROM $table "; // 
    // if you whant to use  Split the database by date
    // $query .=" WHERE CREATED_DATE >= DATE(CONCAT_WS('-', '2020', '08', '01')) AND  CREATED_DATE < DATE(CONCAT_WS('-', '2020', '08', '31'))"
    //   $query .="  WHERE CREATED_DATE >= '$form_date' AND  CREATED_DATE < '$to_date' ";

    $result = mysqli_query($conn, $query);
    
    $columnCount = mysqli_num_fields($result);
    
    // Prepare SQLscript for dumping data for each table
    for ($i = 0; $i < $columnCount; $i ++) {
        while ($row = mysqli_fetch_row($result)) {
            $sqlScript .= "INSERT INTO $table VALUES(";
            for ($j = 0; $j < $columnCount; $j ++) {
                $row[$j] = $row[$j];
                
                if (isset($row[$j])) {
                    $sqlScript .= '"' . $row[$j] . '"';
                } else {
                    $sqlScript .= '""';
                }
                if ($j < ($columnCount - 1)) {
                    $sqlScript .= ',';
                }
            }
            $sqlScript .= ");\n";
        }
    }
    
    $sqlScript .= "\n"; 
}

if(!empty($sqlScript))
{
    // Save the SQL script to a backup file
    $backup_file_name = $homedir.$dprefix.$dbname . '_' .   $uid . '.sql';
    $fileHandler = fopen($backup_file_name, 'w+');
    $number_of_lines = fwrite($fileHandler, $sqlScript);
    fclose($fileHandler); 
}