15 days money back guarantee   Onetime payment, lifetime usage

Blog

Magento create database backup from PHP code

In this article we will provide you with a simple code example of how you can backup your Magento database programmatically. Backup your code from a simple PHP file. This file can be executed via URL, cron or elsewise programatically.

You can reuse and modify the code example below to create your own database backup script.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
* Database backup script
* Author: Emvee Solutions
* Backup can be found in var/backups
*/
 
//increase execution time
ini_set('max_execution_time', 900); //900 seconds = 15 minutes
 
//require Magento
require_once 'app/Mage.php';
$app = Mage::app('admin');
umask(0);
 
//set error reporting
error_reporting(E_ALL & ~E_NOTICE);
Mage::setIsDeveloperMode(true);
 
//do backup
try {
$backupDbHelper = Mage::getModel('backup/db');
//create backup instance, set certain options
$backup = Mage::getModel('backup/backup')
->setTime(time())
->setType('db')
->setPath(Mage::getBaseDir("var") . DS . "backups");
//do actual backup
$backupDbHelper->createBackup($backup);
//return success
print 'Backup successfully created';
 
} catch (Exception $e) {
//log exception magento and print to screen
Mage::logException($e->getMessage());
print 'Error while creating backup: ' . $e->getMessage();
}
/*
* Database backup script
* Author: Emvee Solutions
* Backup can be found in var/backups
*/

//increase execution time
ini_set('max_execution_time', 900); //900 seconds = 15 minutes

//require Magento
require_once 'app/Mage.php';
$app = Mage::app('admin');
umask(0);

//set error reporting
error_reporting(E_ALL & ~E_NOTICE);
Mage::setIsDeveloperMode(true);

//do backup
try {
$backupDbHelper = Mage::getModel('backup/db');
//create backup instance, set certain options
$backup = Mage::getModel('backup/backup')
->setTime(time())
->setType('db')
->setPath(Mage::getBaseDir("var") . DS . "backups");
//do actual backup
$backupDbHelper->createBackup($backup);
//return success
print 'Backup successfully created';

} catch (Exception $e) {
//log exception magento and print to screen
Mage::logException($e->getMessage());
print 'Error while creating backup: ' . $e->getMessage();
}

2 Responses to “Magento create database backup from PHP code”

  1. Perfect! working fine

Leave a Reply to Emvee Solutions Click here to cancel reply.