Luckily there is no need for code here.
Replication has been built into MySQL for many years.
The trick is to setup the remote host as master and the local copy as slave.
All updates go to the master.
And the slave reads from the remote.
The documentation is here: http://dev.mysql.com/doc/refman/5.7/en/replication.html
Here's a tutorial: http://www.howtoforge.com/mysql_master_master_replication
Note that there can really only be one master, if not the setup will get too complicated to be workable.