Excluding Folders From a Cron Backup

Cron JobsA while ago, I posted an article on using cron to make backups of folders on Linux web hosting. One issue I’ve become aware of since then is that there are some folders I want to exclude from the backup process. I did some exploring and found the --exclude switch can be used to exclude a single folder; to exclude multiple folders, you can use the switch multiple times.

The example below, excludes the backup folder and anything which has a name starting with a .:

Created by Ian Grieve of azurecurve|Ramblings of a Dynamics GP Consultant (http://www.azurecurve.co.uk) This code is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0 Int). */
/bin/tar -czvf /home/{username]/backups/backup_{foldername}_$(date +\%Y\%m\%d\%H\%M\%S).tar.gz --exclude=backups --exclude=".*" /home/{username}/{foldername}

The highlighted section shows the two exclude switches used to exclude the folders and files we don’t want to backup.

Replaced ClassicPress Cron Job Timing Out

ClassicPressBack in March I blogged about replacing the standard ClasicPress cron job with a system cron running on a schedule. The reason I did this was to improve the efficiency of my sites by having cron called on a schedule rather than every time a page was loaded.

However, I’ve recently been adding new functionality to my To Twitter plugin and found that scheduled tweets containing images were not working. I did some exploring and discovered that the problem was that cron jobs timeout after 30 seconds and processing one large, or several smaller, image would exceed this timeout.

I then found that an alternative method of calling cron seemed to run much faster and I was no longer having problems with the cron job timing out. This alternative method uses wget:

wget -q -O - https://{your domain}/wp-cron.php?doing_wp_cron >/dev/null 2>&1

The highlighted section should be replaced with your domain.

Replacing ClassicPress Cron with system cron

ClassicPressClassicPress is a hard-fork of Wordpress 4.9 and has the same functionality as that version of WordPress although version 2 of ClassicPress is on the way, which is where changes will start to be introduced.

Some of my sites have been experiencing a growing number of visitors to the point where it is starting to cause problems. The reason for the problems is that the built-in ClassicPress cron handler (WP-Cron) is not a real cron job, but a mimic. And an inefficient one at that.

On high traffic sites this cron-mimic can cause performance problems. A normal system cron job will run continuously and trigger at the scheduled times, but WP-Cron does not run continuously. Instead, it fires on every page load which is where the performance problems can come in; if there are not sufficient PHP workers for the requested processes, a request will be initiated and wait until the next worker comes available. There is also an issue on low traffic sites that a scheduled task won’t start until a page is loaded.

It is recommended that the WP-Cron job be disabled and a system job created and run on a pre-defined schedule.

Disabling WP-Cron is easy to do; edit your wp-config.php file and add the following code above the /* That's all, stop editing! Happy blogging. */ line:

/** Disable cron. */
define('DISABLE_WP_CRON', 'true');

You then need to create the system cron job; the format of this may vary depending on your host. This is the one which works for my host:

cd ${HOME}/public_html; /usr/local/bin/php -q wp-cron.php

Two possible variations are:

cd /home/{username}/public_html; php -q wp-cron.php
php -q /home/{username}/public_html/wp-cron.php

For my main sites I have set the cron to once every hour, but on less trafficked sites I’ve opted for once or twice a day or week.