I want to do that using a code and not using a tool like \"MySQL Migration Toolkit\". The easiest way I know is to open a connection (using MySQL connectors) to DB1 and read
If you enabled binary logging on your current server (and have all the bin logs) you can setup replication for the second server