Bash Script to Copy Maxmind Databases to WordPress Uploads

My previous tutorial explained how to install MaxMind Geoipupdate on CentOS Linux with a weekly cronjob to update each database. This tutorial introduces a Bash script to copy MaxMind databases to your server’s WordPress websites. There, custom WordPress functions or plugins can access them in the /uploads folder.

Bash Script to Copy Files

Intended Audience: Novice – Intermediate Linux User
Example Linux OS: CentOS 7.x (Adjust commands accordingly)

First, let’s create a basic Bash script. I highly suggest putting this script in a custom location. Do not put executable scripts in website root folders. In fact, don’t put them website folders at all if you can help it.

Instead, create a /scripts/custom-scripts folder. Others prefer /opt or /usr/local/bin. See this page for a comprehensive discussion. Location may matter depending on what system variables and paths you need for other scripts.

First, go to your scripts folder.

]# cd /scripts/custom-scripts

Next, create a new file using vim, nano, or another editor of your choice.

]# vi geoipupdate-weekly

Tip: Remember the file name for the cronjob!

Now, copy+paste the following. Carefully review the commands and comments. Customize any paths and commands to your system.

#!/bin/bash

# Run geoipupdate first. Saves updated .mmdb files to /usr/share/GeoIP folder.
# Include full path or else the commands won't fire on the Cronjob.

/usr/bin/geoipupdate -v

# The path to each website's folder will be similar.
# The difference will be either a domain name, web name, or number, like web1.
# This varies depending on your Linux OS and control panel.
# Put each unique folder name in webarray

webarray=(
wordpress1.com
wordpress2.com
wordpress3.com
)

# Loop through each webarray value
for WEB in "${webarray[@]}"
do

# Important - Customize your destination folder path. It may not match the example.
# Public_html is your web root folder on Cpanel. It might be www, web, or html on another system.
# /wp-content/uploads should be the same on most servers.
DESTFOLDER=/var/www/sites/$WEB/public_html/wp-content/uploads

echo Copying Maxmind files to $DESTFOLDER

# Use cp command to copy each GeoIP database.
# Make sure to include -f (force) to overwrite an old database.
# Remove any unnecessary cp commands from this example, or add more.

/usr/bin/cp -f /usr/share/GeoIP/GeoLite2-City.mmdb $DESTFOLDER
/usr/bin/cp -f /usr/share/GeoIP/GeoLite2-ASN.mmdb $DESTFOLDER
/usr/bin/cp -f /usr/share/GeoIP/GeoLite2-Country.mmdb $DESTFOLDER
/usr/bin/cp -f /usr/share/GeoIP/GeoIP2-Country.mmdb $DESTFOLDER

done

echo All Done
exit

Give Root Permission to Run the Script

We’ll add the cronjob to root’s crontab. That means root must be able to run the script. Give root access to the script.

]# chmod 0755 geoipupdate-weekly

Bash Script Cronjob

Notice the script includes the geoipupdate -v command. We’ll need to update the Cronjob suggested in the last tutorial. Now, we’ll modify the crontab to run our script, with a convenient log to review if anything goes wrong.

Edit your crontab.

]# crontab -e

Find the cronjob from the last tutorial. Make a couple simple edits:

Old Cronjob
23 1 * * 6,4 /usr/bin/geoipupdate

New Cronjob
23 1 * * 6,4 /scripts/custom-scripts/geoipupdate-weekly 2>&1> /usr/share/GeoIP/_last_update.txt

Replace /usr/bin/geoipupdate with the path to your new script.

Send output to a text file, overwriting any existing output:
2>&1>/usr/share/GeoIP/_last_update.txt

That’s it! Now, your WordPress websites will each have the latest MaxMind GeoLite databases.

Using MaxMind Databases with WordPress

Now it’s time to put these databases to work.

Knowledgeable developers can quit reading here. You already know how to create custom WordPress functions and plugins.

Suggested WordPress Plugins

Others have already created plugins that can access MaxMind’s GeoLite databases.

Geolocation IP Detection Plugin

Install and activate as usual. Make sure you carefully review and edit the plugin’s options:

  • Add your MaxMind account and license key.
  • Specify the filepath, eg. wp-content/uploads/GeoLite2-City.mmdb
  • Choose manual download & update if you’re using the cronjob.
  • Automatic update is also available. Do not use the cronjob at the same time. Make sure your WP FTP credentials and folder permissions are correct.
  • Enable Ajax endpoint for cached pages.
  • Review – This server is behind a reverse-proxy. Check the box if you’re using Cloudflare or some other CDN.

Call one of this plugin’s functions to access a visitor’s geolocation information.

function my_user_loc() {
if (function_exists('geoip_detect2_get_info_from_current_ip')) 
	{
	// Get user info object
	$userInfo = geoip_detect2_get_info_from_current_ip();
	// User state, city, country if available
	$userState = $userInfo->mostSpecificSubdivision->isoCode;
	$userCity = $userInfo->city->name;
	$userCountry = $userInfo->country->isoCode;
	}
return array('city' => $userCity, 'state' => $userState, 'country' => $userCountry);
}
// Get User Details
$user = my_user_loc();
// Setup Vars
$city = $user['city']; $state = $user['state']; $country = $user['country'];

Use $city, $state, $country, or any other variable on your WP website, theme, or plugin!

Leave a Comment