user4661379
user4661379

Reputation:

How to copy few records from a table on one server to the table with same structure on another server in PHP and MySQL?

I've two same MySQL databases present at two different servers. Initially both the databases have the exactly same structure and contained the same data.

But later on few new records(new users) got added to both the tables. This created the difference in the data present in these tables.

Now the issue is somehow the old data(which is present since when the structure and data of both the databases were same) from one table got tampered by some unauthorized means and in one field contains blank values.

Now I want that data back as of previous(like initial). For this I've to fire a query which would take the missing data from the same table from one server and add it to the same table on another server where the issue of data tampering occurred.

If there is some way by writing some script in PHP which could do this job that will also be fine for me.

Can some one please suggest me how should I achieve this?

If you want any more details regarding the issue I'm facing please do let me know.

Thanks.

Upvotes: 1

Views: 507

Answers (2)

Misunderstood
Misunderstood

Reputation: 5661

Why not export the data from the other server?

Then import the table into the current server (with different name or make new database)

UPDATE

No it will not replace the table. You can create another database and import it into the new database and not the existing.

Or you edit the export file and changed the name then import it.

Example:

If this is your export file:

CREATE TABLE IF NOT EXISTS `Customer` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `Name` char(32) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

Change the name (notice I change customer to customer2)

THEN do the import.

CREATE TABLE IF NOT EXISTS `Customer2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `Name` char(32) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

UPDATE 2

Now you have current table with a blank column, and a "backup" table2.

Both tables should have a unique id or some sort.

This is the basic scenario:

Loop through all the records in table1 get the unique id column value.

Use that id to look up the value of the blank column in table2.

Update the blank column in table1 from the value in table2.

SELECT `id` FROM `table1` WHERE 1

SELECT `blankcolumn` FROM `table2` WHERE `id` = $row['id']

UPDATE `table1` SET `blankcolumn` = `table2`.`blankcolumn`

Upvotes: 1

braks
braks

Reputation: 1610

I would create a RESTful API, allowing one server to GET, PUT, DELETE stuff to the other server using JSON.

So one server has a URL that you can submit some JSON (or XML if you really want) data to it.

Then the other server performs a CURL request and sends the data over.

You probably want to wait until you have enough data to send at once so you are not constantly making requests between the servers.

You can password protect the system also so nobody else that figures this out can edit your database :P

There is a lot to go into, but I think if you look up how to create a REST API in PHP, and also how to make CURL requests, you will find it is quite straightforward. You can also communicate to such a system using JavaScript AJAX calls which opens some more possibilities.

Some database types and database service providers (they run the db for you) can scale databases to multiple instances to do this sort of thing. That might be worth looking into as well. There is usually a delay before the databases match each other - this explains a lot of weirdness on Social Media and Auction/Classifieds websites where a post might not show up for you for a little while.

If the data has to do with something really really important like financial transactions, where having some money go missing for a while is unacceptable, you may need to get much more serious about this.

Upvotes: 0

Related Questions