While WP Migrate DB Pro doesn’t offer a built-in method for scheduling migrations, this is something that can be easily and reliably achieved using your system’s Cron and the WP Migrate DB Pro CLI Addon.
Determine the command to schedule
The first thing you’ll want to do is work out the command you’d like to schedule, or set up a migration profile in WP Migrate DB Pro’s Migration tab in your WordPress admin. In this example, I’ve set up a profile that will pull the database and media files from my live server down to the development server on my local machine and have noted that it’s ID is 8.
Run the command first to make sure it’s doing exactly what you want:
$ wp migratedb profile 8
Update the command to run in Cron
Since we’ll be running this command via the system’s cron we’ll want to modify the command in two ways
- Even if the
wpcommand is in your user’s
$PATHit may not be in the context that Cron runs in, so you’ll want to use the absolute path to
- Cron won’t be running the command from your WordPress install’s directory, so you’ll want to use the WP CLI global parameter
--pathto point to the root of your WordPress install.
With those two amendments, the Cron-safe command should look something like this:
$ /usr/local/bin/wp migratedb profile 8 --path=/var/www/wordpress/
You should replace
/usr/local/bin/wp with the absolute path to the
wp command on your system and
/var/www/wordpress with the absolute path to your wordpress install.
Schedule the command
Now you can set up your system’s Cron to run your migration command. Open your Crontab file for editing:
$ crontab -e
I want my migration to be performed at 2am every day so I’ll add the following line to my Crontab file:
0 2 * * * /usr/local/bin/wp migratedb profile 8 --path=/var/www/wordpress/
You can check that your Cron job was added successfully using the following command:
$ crontab -l
Check here for more information and examples of Crontab syntax.
If you’d like to generate a log every time your migration runs, you can modify your crontab to redirect the output of the command to a file like so:
0 2 * * * /usr/local/bin/wp migratedb profile 8 --path=/var/www/wordpress/ >> /path/to/your/log.txt 2>&1
This will append the output of your command to the file
If you’re running your server on MAMP, you’ve likely used this solution to get WP CLI to work on your system. In some situations, the updated $PATH variable that you’ve set up won’t be available in the context that Cron runs in, and it’s best to write a shell script to make sure that it is.
Here’s an example shell script that uses the same migration command that we’ve used above
#!/bin/bash #get mamp php in path export MAMP_PHP=/Applications/MAMP/bin/php/php5.6.7/bin export PATH="$MAMP_PHP:$PATH" #suppress warnings from WP CLI about not running in a terminal export TERM=xterm-color #print the date/time on a newline printf "\n-----------------$(date)-------------------\n\n" #the migration command /usr/local/bin/wp migratedb profile 8 --path=/var/www/wordpress/
If you’re writing a shell script, you might as well take advantage of the flexibility it offers and make your log file a bit easier to read which I’ve done here by adding a command to print out the date and time and defined the
TERM environment variable that WP CLI will otherwise print a warning about.
Save this file somewhere on your system and then make sure that it’s executable by running the following command:
$ chmod +x /path/to/your/migration-script.sh
You should also run the script to make sure that it works:
Which should output something like this:
-----------------Thu Apr 23 14:30:00 PDT 2015------------------- Verifying connection... Initiating migration... Migrating tables 100[=========================================] 0:05 / 0:05 Cleaning up... Initiating media migration... Removing all remote files before upload of local media... Determining media to migrate - 34 of 34 attachments (100%) Uploading 100 of 100 files 100[===================================] 0:01 / 0:01Success: Migration successful.
Now you can add the job to your Crontab as above. Open your Crontab for editing:
$ crontab -e
And set the job up to run at 2am every day, appending its output to a log file.
0 2 * * * /path/to/your/migration-script.sh >> /path/to/your/log.txt 2>&1