How you end up on Google querying “Merge two Databases”… Like any good story that may be a long tale. I’m writing this because a CubeCart site I maintain was on a server that crashed and the host ran FSCK. The first clue things didn’t go quite as well for us as the host seemed to think they did was that the entire site pointed to my custom RoundCube webmail sub-domain install, then we found SSL didn’t work because we’d been moved to a new server with a new IP and… then the site operator noticed she was missing 24 days of orders and new customers. And that is how these stories begin.
Status: Functional database missing 24 days of orders, new user registrations, products and related data. Database had 2 days of new data (since crash).
Backup current: Backup your current live database (in my case contains two days of new data).
Restore from backup: Restore your most recent pre-event database backup. If you don’t have a backup, or not a very current one, contact your web host as they may. Request they name it differently than the live database so they don’t overwrite it.
Databases in play: One current active database (the target), one restored or backup database copy which contains the missing data (the source).
Goal: Update active DB with missing data from restored/backup version.
- If database is active as in my case – kick out admins, close web storefront (maintenance message) – early morning, late at night.
- Backup both your active/target and restored/source DB’s using phpMyAdmin (Export Tab)
- Identify affected tables. For me I had many: images, categories, customers, products, product descriptions etc.
- Open two instances of phpMyAdmin – tabs or side by side browser windows – one with LiveDB one with RestoredDB.
- Sort Table data by ID and compare the number of rows to see if there is a difference in the number and or ID’s – write it down, these are the tables to merge.
- With the Table selected in the LiveDB that you want to update – click the SQL tab in phpMyAdmin.
- Paste the code below replacing the DB and tables names with yours.
- Click GO to update the active DB with missing data from backup.
- Rinse, lather & repeat per table that you need to update.
- That’s it!
SQL code to use
Change the Database (username_restoredDB) and Table (CubeCart_customer) to yours
INSERT IGNORE INTO `username_liveDB`.`CubeCart_customer`
What the above SQL code does:
INSERT – insert data into the the Live DB
IGNORE – ignore duplicate data = we’re going to insert data only if it doesn’t already exist.
SELECT – select all (‘*’) data from your Table ‘CubeCart_customer’ in the example above.
FROM – the restored DB