I want to use WP-CLI to export a database - using wp db export - to create backups. I also need to use cron to control the backup schedule. WP-CLI is installed on my managed host, and I can use it via SSH, but I can't use cron directly on my server.

In order to use my own cron job to schedule the backups, I need to use the plugin WP Crontrol to add and edit cron jobs; that plugin uses functions as an add_action, i.e. my_backup_cron to fire my_backup_function:

This is the format the plugin uses:

add_action( 'my_backup_cron', 'my_backup_function' );

The function that runs a WP-CLI command can be a standard function, i.e. this in functions.php

function my_backup_function() {
    wp_mail( '[email protected]', 'WP Crontrol', 'WP Crontrol rocks!' );

But how do I turn a WP-CLI command - such as wp --path=/var/www/ db export, along with the path and other parameters - into a function like my_backup_function?

1 Answer 1


WP-CLI can evaluate a PHP file.

See wp eval-file {PHP file name} for details, but essentially you:

  1. Write a PHP file containing the commands you want executed in WordPress's context (let's say you call it myfile.php)
  2. Run it using WP-CLI: wp eval-file myfile.php

If you need it scheduled, you could use cron, the Windows Scheduler, or whatever your local OS provided to schedule tasks.

Update 1

On re-reading your question, this might be more what you're looking for (essentially you want to run a shell script at a scheduled time).

  1. Create a shell script (let's suppose it's located at /home/you/wp-backup.sh:

    /usr/local/bin/wp  --path=/var/www/ db export
  2. Add that script to your crontab using crontab -e:

    0 0 * * * /bin/sh /home/you/wp-backup.sh

...which would run the job every day at midnight. (See the crontab quick reference for more options.)

This also assumes that the sh shell is located at /bin/sh and wp is at /usr/local/bin/wp. If not, please adjust the code accordingly.

Update 2

From the comments, what you're looking for is how to emulate wp db export in a WordPress context. I'm afraid I have what may be bad news.

Looking into the wp db source, it appears that the export command essentially uses PHP's exec() command to run mysqldump. I don't think you'd be wise to emulate this -- I feel there are security experts who are cringing at just the thought of it.

There do exist WordPress backup plugins that can export your database; you can look into them (or, if they're open source, examine their code to see if anything in there can help you out).

  • Thanks, but I can't run cron on my server, that's why I need to use a plugin that adds cron capability. And the plugin uses the add_action format, so I'm trying to find out how to turn wp --path=/var/www/ db export into a function that goes in the theme's functions.php so I can call it via add_action. Commented Aug 24, 2020 at 20:29
  • Oh, OK, you're looking for what wp db export does under the hood. I'll update my answer.
    – Pat J
    Commented Aug 24, 2020 at 20:42
  • Thanks, that's an idea, but I can't use backup plugins on the host; they create full file backups, but I want an extra database backup. Commented Aug 24, 2020 at 21:09
  • Sure, but they also often do full DB backups. For example, BackWPUp says it does DB backups (using mysqli). The code is here -- plugins.svn.wordpress.org/backwpup/trunk . Perhaps you can find what you're looking for there.
    – Pat J
    Commented Aug 24, 2020 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.