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.
AI Usage Transparency Report
Pre-AI Era · Written before widespread use of generative AI tools
AI Signal Composition
Score: 0.05 · Low AI Influence
Summary
This article discusses the process of syncing multiple websites on multiple servers for failover purposes. It covers creating an SSH key, setting up a bash script to log activity, and using rsync and mysqldump to sync files and databases between servers.
Related Posts
Roll your own DNS monitoring with DIG, Bash & CRON
If your like me your always looking for ways to be notified of things changing in your IT Environment. There are many tools that you can use to help do this. StatusCake is a great free online tool for monitoring website and IP level uptime and downtime with baked in email notifications. Zeonoss and NAGIOS are great tools that can offer the same with SNMP Monitoring baked in as well.
Authenticate with AD credentials via ARD / SSH
Binding a Mac to an AD is fairly straight forward. Most Mac Admin's worth their salt, know how this is done, many know how to do this via the command line. Once your Mac is bound, authentication is easy, local authentication that is. But what if you want to use your secure AD credentials over an SSH or Apple Remote Desktop connection? Well thats when things need a bit more configuration. Having recently deployed a series of servers with this configuration I figured I would share some of the commands...
Migrate Outlook 2016 Profile from one Mac to another Mac
I recently had to help a client move from one Mac to another, during the process one task proved more challenging than originally anticipated. I wanted to share my info in the event that it helps someone out there. In Outlook 2016 for Mac, Microsoft in its infinite wisdom, has changed the default location of the email profile folder. The new location is not well documented, and I stumbled upon it on an obscure forum post, the location is
Migrate Open Directory 10.10
A few weeks ago I had an old 10.9 open directory master server crash on me and I was unable to restart, luckily I had a good backup of my server which I created using Carbon Copy Cloner on a schedule. If your not using Carbon Copy Cloner I highly recommend doing so its one of the best backup utilities for OSX Server as it runs in the background and can backup and clone multiple directories and or the entire hard drive.
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 a SQL db and keeps a history, plus the ability of create dynamic computer collections based on querys to the SQL and then target those groups with tasks makes it extremely useful in an enterprise environment. Plus the amazing reports you can get if you have an SQL guru around!
Munki Software License Tracking
Beginning with the 0.9.1 builds of the munki tools, Munki can query a webserver to determine if there are available seats for licensed software (or any software you wish to make available via optional_installs, yet control the number of deployed copies). In order to use this feature, here are the things you need:
Install Zenoss on 10.9 Mavericks with VMWare Fusion
If you are a network (or systems) administrator, you know how crucial it is to have the right tools for the job. One of the toughest tools to really nail down is a network monitoring tool. Although there are plenty of such tools out there, they range from the over-priced to the under-featured. Where do you look for any sort of middle ground where features don’t lose out to price?
Deploying Printers with Munki on 10.9 Mavericks
You can use Profile Manager to manage printer lists but the functionality is limited. One major issue with managing printer lists with Profile Manager is if you add a printer to an Profile Manager client’s printer list, and the driver file for that the printer isn’t installed on the client system, the printer will be added using the Generic Printer Driver. Even if the printer driver file is installed later the printer continues to use the Generic Printer Driver.
Updating Munki Web Admin on 10.9 Mavericks Server
Discover the Power of MUNKI: A Robust Solution for Your Mac Management Needs MUNKI is a highly acclaimed product that has earned its place as a top choice among Mac administrators, thanks to its strong community backing and impressive track record. With support from industry giants like Disney and Google, this reliable solution has consistently delivered results across all versions of OSX since its inception. Whether you're managing a small fleet or a large enterprise, MUNKI's robust features and seamless integration make it an ideal choice for any Mac management...
10.9 Deploying Mac App Store Packages
If your like me then your happy that Apple has made several of their wonderful software titles free recently, specifically iLife and iWork for Mavericks. Apple has a defined workflow for deployment of these systems. Their method is to have companies enroll into their Volume Licensing Program once enrolled you can download apps from the app store and the iOS store and deploy these seamlessly to your devices with Profile Manager for Mavericks.