Magento: Create database backups daily programatically by cron
It’s a good practice to create your database backup every day/week. But to manually go to Magento admin and create backup daily is a cumbersome process. You can’t guarantee that your database will be secure especially if you are on a cloud storage. Backups are MUST and if it’s automated, life becomes very easier.
This code will create backup of your Magento DB and place it to var/backups directory.
public function backup() | |
{ | |
try { | |
$backupDb = Mage::getModel('backup/db'); | |
$backup = Mage::getModel('backup/backup') | |
->setTime(time()) | |
->setType('db') | |
->setPath(Mage::getBaseDir("var") . DS . "backups"); | |
$backupDb->createBackup($backup); | |
return Mage::helper('core')->__('Backup successfully created'); | |
} catch (Exception $e) { | |
Mage::logException($e); | |
} | |
return $this; | |
} |
Now, you can set the cron to run every midnight and call this above method to automatically create your DB backups. Sounds good? But there is a problem. As Magento DB size is huge, running daily/weekly backups will soon fill up your hardrives like hell.
So, we will now first create new DB database backup through our above method, and after it’s created we will delete the older backups just to free up the disk space.
public function endsWith($haystack, $needle) | |
{ | |
$length = strlen($needle); | |
if ($length == 0) { | |
return true; | |
} | |
return (substr($haystack, -$length) === $needle); | |
} | |
public function backup() | |
{ | |
//get all the older db backup files in an array | |
$filelist = array(); | |
if ($handle = opendir(Mage::getBaseDir('var') . DS . "backups" . DS)) { | |
while ($entry = readdir($handle)) { | |
if ($this->endsWith($entry, "_db.gz")) { | |
$filelist[] = $entry; | |
} | |
} | |
closedir($handle); | |
} | |
try { | |
//create the db backup | |
$backupDb = Mage::getModel('backup/db'); | |
$backup = Mage::getModel('backup/backup') | |
->setTime(time()) | |
->setType('db') | |
->setPath(Mage::getBaseDir("var") . DS . "backups"); | |
$backupDb->createBackup($backup); | |
//delete all older db backup files we found | |
foreach((array)$filelist as $fileToDelete) { | |
unlink(Mage::getBaseDir("var") . DS . "backups" . DS . $fileToDelete); | |
} | |
return Mage::helper('core')->__('Backup successfully created'); | |
} catch (Exception $e) { | |
Mage::logException($e); | |
} | |
return $this; | |
} |
Now you can set the CRON to run the file every midnight, which calls the above backup() method. You will now get daily fresh backups replacing the old ones 🙂
If you are not sure how to create a cron, check this link: http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/how_to_setup_a_cron_job
4 Comments
Leave a comment
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)
This is the first time I’ve been to your site. Thanks for explaining more details.
in your script, database will backup, but i want database and media files.. what i need change in script?
Hello i want magento site backup not database backup it possible like programatically ?
This is a great or helpful blog …….