
Creating Cron Jobs for PrestaShop Backups
Having regular backups is the very first important thing to consider, whenever you have a running web-shop. In this tutorial, we will see how to automate PrestaShop Backups using a simple script and a cron job.
The backup script
PrestaShop has a very simple way to create backups. In the back office controller, once you hit the button to create a new one, it will simply call the add() method on the PrestaShopBackup object instance.
We can take advantage of this simplicity to create our own script, that can be called at any time to create a new backup.
To keep things simple, let’s use the root folder of our shop. Inside it, create a new file named back-me-up.php. We will later create a cron job using cPanel to target this very script.
Open it up, and start by including the following in php tags:
include(dirname(__FILE__).'/config/config.inc.php'); include(dirname(__FILE__).'/init.php');
This part initializes PrestaShop, and let’s us use its classes and functions.
It is essential to create at least a bare minimum level of security, as it would be really unpleasant for us to find out HDD space filled up because someone executed our backup script dozens of time repetitively.
include(dirname(__FILE__).'/config/config.inc.php'); include(dirname(__FILE__).'/init.php'); $key = 'mypassword'; if(!Tools::getValue('k') || Tools::getValue('k') != $key) die('unauthorized');
We are almost done. At this point, we need to define the admin folder constant, in case it’s not been defined. This is needed for the backup script to run, otherwise it will simply die.
include(dirname(__FILE__).'/config/config.inc.php'); include(dirname(__FILE__).'/init.php'); $key = 'mypassword'; if(!Tools::getValue('k') || Tools::getValue('k') != $key) die('unauthorized'); if(!defined('_PS_ADMIN_DIR_')) define('_PS_ADMIN_DIR_', getcwd().'/nameofmyadmin');
Make sure you replace the nameofmyadmin string with the name of your own admin folder.
Lastly, let’s run the backup itself:
include(dirname(__FILE__).'/config/config.inc.php'); include(dirname(__FILE__).'/init.php'); $key = 'mypassword'; if(!Tools::getValue('k') || Tools::getValue('k') != $key) die('unauthorized'); if(!defined('_PS_ADMIN_DIR_')) define('_PS_ADMIN_DIR_', getcwd().'/nameofmyadmin'); $backup = new PrestaShopBackup(); if($backup->add()) die('success'); else die('error');
We are done with php! We just need to create a cron job that runs our script.
Before that, I strongly recommend to manually check if it works, by reaching your site’s url /back-me-up.php?k=mypassword
Target our new file with a cron job
You can skip this part if you already know how to create a cron job. If not, read on. I am using cPanel for the example, so if you have another control panel for your hosting space, you might have to find your way through the cron jobs setup screen.
Access your hosting’s cpanel, and navigate to Cron Jobs
For the example, create a new cron job that runs every minute (you can choose it from the common settings)
If you are new to cron jobs, you might have some trouble finding out the correct url. What we need to do here, in the cron command, is executing php on our script, adding the k parameter as well. Here is how the command is supposed to look like
php /home/nameofyouraccount/public_html/path/to/your/main/folder/back-me-up.php k=mypassword
The account name is usually the cPanel login. If not, you can read it on the left side of the cPanel homepage, being part of Home Directory
Unlike with a direct url execution, when you pass in any query string parameter, they must not be preceded by a question mark, not separated by amperstands; they can simply be added one by one, separated by spaces, after the url
If your site is in the root folder (public_html), the url will be
php /home/nameofyouraccount/public_html/back-me-up.php k=mypassword
Save it, then go back to your PrestaShop admin, and reach Advanced Parameters > DB Backup. Wait a couple of minutes, then refresh the page. You should see a few new backup files (depending on the size of your shop, it might take a bit). If you do not, and your shop is relatively small, you might want to check the cron url is correct. It might take several minutes to back up your site, if you have more than a couple of hundreds of products.