Skip to content

Instantly share code, notes, and snippets.

@DrayChou
Created July 19, 2014 14:42
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 DrayChou/51f36930ebd2deb8d0eb to your computer and use it in GitHub Desktop.
Save DrayChou/51f36930ebd2deb8d0eb to your computer and use it in GitHub Desktop.
php 导出指定表的脚本
<?php
/**
* Created by PhpStorm.
* User: Dray
* Date: 14-7-19
* Time: 下午4:58
*/
$update_table = array(
'table1',
);
date_default_timezone_set('Asia/Shanghai');
$mysql_config = array(
'exe' => '/cygdrive/e/WT-NMP/bin/mysql-5.6.19/bin/',
'put' => '/cygdrive/e/WT-NMP/WWW/tool/sql/' . date('Ymd.H') . '/',
'user' => "",
'passwd' => "",
'host' => "",
'db' => array(
'db1',
),
);
if (isset($_SERVER['SERVER_NAME'])) {
$mysql_config = array_merge($mysql_config, array(
'exe' => 'E:\WT-NMP\bin\mysql-5.6.19\bin\\',
'put' => 'E:\WT-NMP\WWW\tool\sql' . '\\' . date('Ymd.H') . '\\',
));
}
echo "<pre>";
foreach ($mysql_config['db'] as $db) {
$tables = array();
$exec = "{$mysql_config['exe']}mysql.exe -h {$mysql_config['host']} -u {$mysql_config['user']} -p\"{$mysql_config['passwd']}\" -e \"use {$db}; show tables; \" | grep -vE \"Tables_in_{$db}\"";
exec($exec, $tables);
// var_dump($exec);
// var_dump($tables);
foreach ($tables as $tab) {
if (in_array($tab, $update_table)) {
echo 'table:' . $tab . PHP_EOL;
$res = array();
$exec = "{$mysql_config['exe']}mysqldump.exe -h {$mysql_config['host']} -u {$mysql_config['user']} -p\"{$mysql_config['passwd']}\" {$db} {$tab}";
exec($exec, $res);
// var_dump($exec);
// var_dump($res);
if (!is_dir($mysql_config['put'])) {
mkdir($mysql_config['put'], 0777);
}
$sql_file = $db . '.' . $tab . '.sql';
$file = $mysql_config['put'] . $sql_file;
echo 'mysqldump:' . $file . PHP_EOL;
$fp = fopen($file, 'wb');
if (!$fp) {
die();
}
foreach ($res as $row) {
fwrite($fp, $row . PHP_EOL);
}
fclose($fp);
echo 'done' . PHP_EOL;
}
}
}
echo "</pre>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment