Feb 2, 2013
kalpesh

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

  • 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 …….

Leave a comment

 

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions