In this article we are picking up where we left off, we now have the ability to connect from our primary server to our secondary server without the need to enter a password with the help of a secret key. If you are lost at this point please refer to the last article Syncing a failover website : Creating an SSH key. At this point all we need to do is setup the bash script. We will create the script to log all of its activity so that we know if the process is having problems. The log file will be kept in a directory on the primary server. You will need to know the absolute path to this file and the file must be writable and readable by the system, so a chmod of 755 or 777 should do the trick.
At this point we are ready for the sync to start, we will start by syncing the files from the primary server to the secondary server. This is a one way sync, whatever we add to the primary server will be copied over to the secondary server. Whatever is deleted from the primary server will also be deleted from the secondary server we accomplish this via the use of rsync.
Notice that the first line is the path to our primary website, the second value is the port number that you use to ssh into your secondary server if they require that. The third option is the username and the ip address or hostname of the secondary server and then the path to the files on the secondary server. Again in order for this to work you really have to have completed the first step if this is not working refer back to the article that covers the appropriate way to ssh into your remote server Syncing a failover website : Creating an SSH key.
What we are doing above is copying a modified version of the wp-config.php file because we use wordpress installations as our main CMS platform, the configuration settings on the primary server will not always match exactly the configuration settings on your secondary server. Which means that if you failover and the settings on the secondary server are the ones from your first server, and the secondary server uses a different database prefix, username or password the failover will succeed but it will failover to a website that will give you the dreaded “Cannot connect to database” error.
The above code, will allow you to sync your database with the database in your secondary location. You will need to modify the settings to match your primary username and password for mysql. Your secondary username and passwords for mysql and the primary and secondary database names in mysql. What this does is it empties the target database and then it re-imports all the content from your primary server. Then it logs a line in the log, stating when it has completed. Here is what the finished script looks like.
Thats about it, in our next and final article on the topic of syncing multiple websites on multiple servers for failover purposes we will talk about the proper way to schedule your sync.