samedi 18 juin 2016

Export tables from multiple databases and import into a single database

I would like to export specific data from three table with the same name from multiple databases and import them into a single database SUPPORT_DATABASE, the tables are XXX_users, XXX_usergroups, XXX-user_usergroup_map.

the user id is the primary key in all tables.

Here are the scripts i have so far

MySQL-Export.php

<?php
//ENTER THE RELEVANT INFO BELOW
$mysqlDatabaseName ='db1';
$mysqlUserName ='db1owner';
$mysqlPassword ='Password';
$mysqlHostName ='db1.mywebhost.net';
$mysqlExportPath ='db1backup.sql';

//DONT EDIT BELOW THIS LINE
//Export the database and output the status to the page
$command='mysqldump --opt -h' .$mysqlHostName .' -u' .$mysqlUserName .' -p'    .$mysqlPassword .' ' .$mysqlDatabaseName .'
SELECT id, name, username, password, email FROM `xxx_users` WHERE `id` in (SELECT `user_id` FROM `xxx_user_usergroup_map` WHERE `group_id` = (SELECT `id`     FROM `xxx_usergroups` WHERE `title` = 'Administrator'));
SELECT id, name, username, password, email FROM `xxx_users` WHERE `id` in (SELECT `user_id` FROM `xxx_user_usergroup_map` WHERE `group_id` = (SELECT `id` FROM `xxx_usergroups` WHERE `title` = 'Engineer'));
> ~/' .$mysqlExportPath;
exec($command,$output=array(),$worked);
switch($worked){
case 0:
echo 'Database <b>' .$mysqlDatabaseName .'</b> successfully exported to <b>~/' .$mysqlExportPath .'</b>';
break;
case 1:
echo 'There was a warning during the export of <b>' .$mysqlDatabaseName .'</b> to <b>~/' .$mysqlExportPath .'</b>';
break;
case 2:
echo 'There was an error during export. Please check your values:<br/><br/><table><tr><td>MySQL Database Name:</td><td><b>' .$mysqlDatabaseName .'</b></td></tr><tr><td>MySQL User Name:</td><td><b>' .$mysqlUserName .'</b></td></tr><tr><td>MySQL Password:</td><td><b>NOTSHOWN </b></td></tr><tr><td>MySQL Host Name:</td><td><b>' .$mysqlHostName .'</b></td></tr></table>';
break;
}
?>

MySQL-Import.php

$mysqlDatabaseName ='db2';
$mysqlUserName ='db2owner';
$mysqlPassword ='Password';
$mysqlHostName ='db2.mywebhost.net';
$mysqlImportFilename ='db1backup.sql';

//DONT EDIT BELOW THIS LINE
//Export the database and output the status to the page
$command='mysql -h' .$mysqlHostName .' -u' .$mysqlUserName .' -p' .$mysqlPassword .' ' .$mysqlDatabaseName .' < ' .$mysqlImportFilename;
exec($command,$output=array(),$worked);
switch($worked){
case 0:
echo 'Import file <b>' .$mysqlImportFilename .'</b> successfully imported to    database <b>' .$mysqlDatabaseName .'</b>';
break;
case 1:
echo 'There was an error during import. Please make sure the import file is saved in the same folder as this script and check your values:<br/><br/><table><tr><td>MySQL Database Name:</td><td><b>' .$mysqlDatabaseName .'</b></td></tr><tr><td>MySQL User Name:</td><td><b>' .$mysqlUserName .'</b></td></tr><tr><td>MySQL Password:</td><td><b>NOTSHOWN</b></td></tr><tr><td>MySQL Host Name:</td><td><b>' .$mysqlHostName .'</b></td></tr><tr><td>MySQL Import Filename:</td><td><b>' .$mysqlImportFilename .'</b></td></tr></table>';
break;
}
?> 

Table structures

XXX_user
user_id, name, username, email, password
300, john smtih, jsmith, jsmith@test.com, password123@

XXX_usergroups
group_id, type
8, administrator

XXX_user_group_map
user_id, group_id
300, 8

What I thought I can do, is export the data from three databases using MySQL-Export.php and store the databases on the server, then import the databases using MySQL-import.php, from all three databases into an auxiliary database, once the data is here MODIFY the user group_id in XXX-user_usergroup_map to match the usergroups in the SUPPORT_DATABASE

at this point I am a little stuck, as importing users from multiple databases, the user may share the same user_id, so i am unsure what to do, where and how can i change the user_id.

It doesnt matter what the user_id is once it is on SUPPORT_DATABASE

can any body suggest some thing please.

Aucun commentaire:

Enregistrer un commentaire