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.
#!/bin/bash
echo Sync started `date` >> /Volumes/Logs/Sync_log.txt
echo "Now starting rsync"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.
rsync -avz --delete "/Volumes/PrimaryWebsite/" --rsh='ssh -p8286' username@XX.18.XX.22:www/domains/SecondaryWebsiteNotice 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.
echo "Now starting modifications"
scp -oPort=8286 "/Volumes/modifications/wp-config.php" username@XX.18.XX.22:www/domains/SecondaryWebsiteWhat 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.
echo "Now starting database sync"
mysqldump --user=primarymysqlusername --password=primarymysqlpassword primarydatabasename | ssh secondarysshusername@XX.18.XX.22 -p8286 mysql --user= secondarymysqlusername --password= secondarymysqlpassword secondarydatabasename
echo Sync finished `date` >> /Volumes/Logs/Sync_log.txtThe 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.
#!/bin/bash
echo Sync started `date` >> /Volumes/Logs/Sync_log.txt
echo "Now starting rsync"
rsync -avz --delete "/Volumes/PrimaryWebsite/" --rsh='ssh -p8286' username@XX.18.XX.22:www/domains/SecondaryWebsite
echo "Now starting modifications"
scp -oPort=8286 "/Volumes/modifications/wp-config.php" username@XX.18.XX.22:www/domains/SecondaryWebsite
echo "Now starting database sync"
mysqldump --user=primarymysqlusername --password=primarymysqlpassword primarydatabasename | ssh secondarysshusername@XX.18.XX.22 -p8286 mysql --user= secondarymysqlusername --password= secondarymysqlpassword secondarydatabasename
echo Sync finished `date` >> /Volumes/Logs/Sync_log.txtThats 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.
Related Posts
Roll your own DNS monitoring with DIG, Bash & CRON
But what about DNS monitoring. You heard me, what if you want to monitor or be notified on DNS record change. I know what your thinking likely DNS changes are cleared by...
Authenticate with AD credentials via ARD / SSH
The way to accomplish ARD AD authentication is by nesting an AD group inside a local group. You can create any group you want but for the sake of this article we...
Migrate Outlook 2016 Profile from one Mac to another Mac
{% highlight bash %} ~/Library/Group Containers/UBF8T346G9.Office/Outlook/Outlook 15 Profiles/Main Profile {% endhighlight %}
Migrate Open Directory 10.10
In my case I was using it with Safety Net enabled and was able to restore the entire drive which took under an hour. Once restored I realized the best option for...
Munki Report-PHP, the new old kid on the block
I have used SCCM for a while now and have to say that I find it very very powerful. The fact that collects plenty of information from the clients, uploads it to...
Munki Software License Tracking
lient running munkitools 0.9.1.x or later Web service that is tracking available seats and that provides information in a specific format in response to queries. One such server is MunkiWebAdmin as of...
Install Zenoss on 10.9 Mavericks with VMWare Fusion
Zenoss is an outstanding, enterprise-ready network monitoring tool that includes all of the features you are accustomed to finding in a much costlier solution. Many a network administrator would do well to...
Deploying Printers with Munki on 10.9 Mavericks
You might be asking: Why add and remove printers using Munki? Why not just use Profile Manager?
Updating Munki Web Admin on 10.9 Mavericks Server
Many third party utilities have been created to work along side Munki to augment the ability to manage inventory and other aspects of Munki such as software reporting, software licensing escrow, and...
10.9 Deploying Mac App Store Packages
All of this hinges on a few things. First your company has to be willing to enroll in Apples Volume Purchasing Program many companies are not interested because many companies are simply...