0

How would I copy all tables from database A to database B that do not exist in database B? Both databases are on the same server.

Thanks for ideas :)!

Arnie
  • 661
  • 9
  • 20

1 Answers1

0

You can use below script, which will copy only those tables, don't exist on target db.

#!/bin/bash

USER=root
PASS=root123
SOURCE_DB=db1
TARGET_DB=db2

TBL=`mysql -u$USER -p$PASS -e"SET group_concat_max_len=102400;SELECT GROUP_CONCAT(DISTINCT a.table_name SEPARATOR ' ') FROM information_schema.tables a LEFT JOIN information_schema.tables b ON a.table_name=b.table_name AND b.table_schema='$TARGET_DB' AND b.table_type='BASE TABLE' WHERE a.table_schema='$SOURCE_DB' AND b.table_name IS NULL AND a.table_type='BASE TABLE';"`

mysqldump --single-transaction -u$USER -p$PASS $SOURCE_DB $TBL | mysql -u$USER -p$PASS $TARGET_DB

change root to whatever your user

root123 with that user password

db1 to your source DB

db2 to your target DB

Zafar Malik
  • 6,734
  • 2
  • 19
  • 30