Can I run backups from the shell / cron?

N.B. (April 2018 – the instructions on this page should still work, but we now support WP-CLI, which is the recommended way to interact with UpdraftPlus from the command-line. See our WP-CLI documentation here).

If you have shell access, then running a backup from the command-line has one big advantage over running it via your website: you shouldn’t get time-outs, so the backup will not need to automatically resume, but can run through in one go.

Furthermore, from the shell you can view the progress more easily in real-time. You can also run these as cron jobs.

To kick off a backup from the shell or cron, run a PHP file in the directory of your WordPress installation that looks like this:

<?php

define('UPDRAFTPLUS_CONSOLELOG', true);
define('DOING_CRON', true);
require_once('wp-load.php');
do_action('updraft_backupnow_backup_all', array('nocloud' => 0));

If you don’t want to watch the lines being logged running past, then you can miss out the “define” line.

If you don’t want to send the backup to remote storage, change the ‘nocloud’ parameter to ‘1’.

  • To backup just files, replace the ‘do_action’ line with:
    do_action('updraft_backup');
  • To backup just the database, replace the ‘do_action’ line with:
    do_action('updraft_backup_database');

If you want to only backup certain entities (e.g. plugins, themes, database) then replace the do_action line with:

global $updraftplus;
$backup_files = true;
$backup_database = true;
$entities_to_include = array('plugins', 'themes');
$updraftplus->boot_backup($backup_files, $backup_database, $entities_to_include);

If you wish or want to resume an already-started job, then instead of the do_action line above, use this one – but change the 12-digit hex code to the code for your job (which you can easily spot in the log file of a started job, or from the filenames in your wp-content/updraft directory):

do_action('updraft_backup_resume', 'fbb7e2919e0c', 1);

If you wish to run incremental backups, in addition to the normal backup cronjob, you will need to add a second task calling the following action:

do_action('updraft_backup_increments');

For example:

<?php

define('UPDRAFTPLUS_CONSOLELOG', true);
define('DOING_CRON', true);
require_once('wp-load.php');
do_action('updraft_backup_increments');

Schedule this cron task at the times you wish for the incremental backups to run, and the full backup task with a wider interval.

Note that on a WordPress multi-site install, you will also need to add (before the line that loads wp-load.php), lines like these – but change them so that the host and URI together indicate one of your sites. (If they do not match any, then the line that loads wp-load.php will not return; WordPress will die first).

$_SERVER['HTTP_HOST'] = 'site.localdomain';
$_SERVER['REQUEST_URI'] = '/';

On older versions of WordPress, you may instead need to install the patch from this ticket, in order to overcome a WordPress bug; here is the patch for 3.7 (believed to work for some later versions too): https://core.trac.wordpress.org/ticket/14913 (or if you are on 3.6 or 3.7, get an already-patched version of the relevant WordPress file from: https://updraftplus.com/forums/support-forum-group1/paid-support-forum-forum2/problem-whit-uploads-dir-thread33.1/#postid-232).

Posted in: Advanced usage, Backing up

twitterlinkedinFacebook