I have two servers which contains a same database, same tables structure
with different values. I need to compare the count of a column(ID) in both of
them for a table1 and if server1’s db’s table1’s max(ID)>max(ID) of server2’s db’s
table1 then Insert all the new records from server1 to server2
I need to keep this logic inside a shell scripts so that when I call the .sh file
it will do the job for me. I want to do it from my local machine which has access to
these servers.
how can the compare will happen inside a shell script. I m using Mysql DB
From what I get from your statement is that you want to keep checking if any of the tables has more records than the other one. And duplicate the table with more entries. What I would suggest is, since you want to use a script file to automate the task, I’ll give you two methods:
(i) Instead of comparing the column ids, you can create a mysql-dump-file for each table and check which one has more number of characters in it by comparing size. The one with the more is obviously, more populated. So, overwrite the small table with the dump of the larger one.
(ii) First store the output of COUNT(*) for both the tables in two variables and compare them.
Where your input-file will have the content:
Compare the ids, and then replace the smaller table with the dump of the larger table!
If you don’t want to replace the whole table, then use: