SHARE
TWEET

Untitled

a guest Jun 29th, 2018 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE TABLE x LIKE y;
  2.    
  3. CREATE TABLE x LIKE other_db.y;
  4.    
  5. INSERT INTO x SELECT * FROM other_db.y;
  6.    
  7. #!/bin/bash
  8.  
  9. DBUSER=user
  10. DBPASSWORD=pwd
  11. DBSNAME=sourceDb
  12. DBNAME=destinationDb
  13. DBSERVER=db.example.com
  14.  
  15. fCreateTable=""
  16. fInsertData=""
  17. echo "Copying database ... (may take a while ...)"
  18. DBCONN="-h ${DBSERVER} -u ${DBUSER} --password=${DBPASSWORD}"
  19. echo "DROP DATABASE IF EXISTS ${DBNAME}" | mysql ${DBCONN}
  20. echo "CREATE DATABASE ${DBNAME}" | mysql ${DBCONN}
  21. for TABLE in `echo "SHOW TABLES" | mysql $DBCONN $DBSNAME | tail -n +2`; do
  22.         createTable=`echo "SHOW CREATE TABLE ${TABLE}"|mysql -B -r $DBCONN $DBSNAME|tail -n +2|cut -f 2-`
  23.         fCreateTable="${fCreateTable} ; ${createTable}"
  24.         insertData="INSERT INTO ${DBNAME}.${TABLE} SELECT * FROM ${DBSNAME}.${TABLE}"
  25.         fInsertData="${fInsertData} ; ${insertData}"
  26. done;
  27. echo "$fCreateTable ; $fInsertData" | mysql $DBCONN $DBNAME
  28.    
  29. echo "set foreign_key_checks = 0; $fCreateTable ; $fInsertData ; set foreign_key_checks = 1;" | mysql $DBCONN $DBNAME
  30.    
  31. CREATE TABLE t2 SELECT * FROM t1;
  32.    
  33. function cloneTable($table,$newTable){
  34. global $admin;
  35. $db_check = @mysql_select_db ( $table );
  36. $getTables  =   $admin->query("SHOW TABLES");  
  37. $tables =   array();
  38. while($row = mysql_fetch_row($getTables)){
  39.     $tables[]   =   $row[0];
  40.     }
  41. $createTable    =   mysql_query("CREATE DATABASE `$newTable` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;") or die(mysql_error());
  42. foreach($tables as $cTable){
  43.     $db_check   =   @mysql_select_db ( $newTable );
  44.     $create     =   $admin->query("CREATE TABLE $cTable LIKE ".$table.".".$cTable);
  45.     if(!$create) {
  46.         $error  =   true;
  47.         }
  48.     $insert     =   $admin->query("INSERT INTO $cTable SELECT * FROM ".$table.".".$cTable);
  49.     }
  50. return !isset($error) ? true : false;
  51. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top