Skip to content

Instantly share code, notes, and snippets.

@ihsanberahim
Last active May 11, 2018 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihsanberahim/6e79c91cd12cff9139cd8ff97ce16899 to your computer and use it in GitHub Desktop.
Save ihsanberahim/6e79c91cd12cff9139cd8ff97ce16899 to your computer and use it in GitHub Desktop.
DUMP SQL USE WP

DUMP SQL USE WP

  • install Debug Bar Console
  • install File Manager
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('log_errors', false);
DEFINE('DATABASE_NAME', '');
DEFINE('DATABASE_USER', '');
DEFINE('DATABASE_PASSWORD', '');
DEFINE('DATABASE_HOST', 'localhost');
$backup=false;
$show=true;
echo '<pre>';
// var_dump(shell_exec('mysql --help'));
echo '</pre>';
//ANALYZE
if($show)
{
$newDb = new wpdb(DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_HOST);
$tableList = $newDb->get_results("show full tables where Table_Type = 'BASE TABLE'", ARRAY_A);
$tablesDetail = [];
$selectedTable = [];
$excludeTables = [
//exclude table by it name here
];
foreach($tableList as $table)
{
$tableName = array_values($table)[0];
if(!in_array($tableName, $excludeTables)) {
$selectedTable[] = $tableName;
}
$tablesDetail[] = [
'tableName' => $tableName,
'recordNumbers' => $newDb->get_var("SELECT COUNT(*) FROM {$tableName}")
];
}
$selectedTable = implode(' ', $selectedTable);
echo '<pre>';
var_dump($tablesDetail);
echo '</pre>';
}
//BACKUP
if($backup)
{
$location = realpath(trim(shell_exec('pwd')).'/../wp-content');
shell_exec("".
"mysqldump ".
"--host=\"".DATABASE_NAME."\" ".
"--user=\"".DATABASE_USER."\" ".
"--password=\"".DATABASE_PASSWORD."\" ".
"--add-drop-table ".
"--single-transaction ".
"--no-create-db ".
DATABASE_NAME." ".
"{$selectedTable} ".
"> {$location}/".DATABASE_NAME.".sql".
"");
shell_exec("".
"mysqldump ".
"--host=\"".DATABASE_NAME."\" ".
"--user=\"".DATABASE_USER."\" ".
"--password=\"".DATABASE_PASSWORD."\" ".
"--no-data ".
DATABASE_NAME." ".
"{$selectedTable} ".
"> {$location}/".DATABASE_NAME."_tables.sql".
"");
shell_exec("sed -i 's/DEFINER=[^*]*\*/\*/g' {$location}/".DATABASE_NAME.".sql");
shell_exec("sed -i 's/DEFINER=[^*]*\*/\*/g' {$location}/".DATABASE_NAME."_tables.sql");
echo '<pre>';
var_dump([time(), file_exists($location)]);
echo '</pre>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment