Magento Multi-Store Database Backup Without phpMyAdmin

backupdatabasemagento-1.9MySQL

I create php script for magento database backup that work well for Single store site but showing error for multi-store

//Magento Database Backup php script
    error_reporting(E_ALL ^ E_NOTICE);
    ini_set('display_errors', 1);
    ini_set('memory_limit', '1512M');

    // Get Magento Application
    require_once 'app/Mage.php';
           Mage::app('default');
           //Mage::app('main');



    // get Magento config
    $config  = Mage::getConfig()->getResourceConnectionConfig("default_setup");

    $dbinfo = array(
        "host" => $config->host,
        "user" => $config->username,
        "pass" => $config->password,
        "dbname" => $config->dbname
    );


    // Database Config
    $db_host = $dbinfo["host"];
    $db_user = $dbinfo["user"];
    $db_pass = $dbinfo["pass"];
    $db_name = $dbinfo["dbname"];

    //echo $db_name; die();

    // filename 
    $backup_file = $db_name ."-". date("ymdHis") . ".zip";

    $command = "mysqldump --database " . $db_name  . " -u ". $db_user  . " -p'". $db_pass . "' | gzip > " . $backup_file;

    echo 'command executing is ' . $command . '<br/>' ;

    $output = shell_exec($command);

    echo 'Finished!<br/>' ;


    //shell_exec("mysqldump --opt --default-character-set=utf8 -uuser {$backup_db} > {$sql_name}")

Error For Multi-store as follows:

Fatal error: Uncaught exception 'Mage_Core_Model_Store_Exception' in 
/public_html/app/code/core/Mage/Core/Model/App.php:1357 Stack trace: #0 
/public_html/app/code/core/Mage/Core/Model/App.php(842): Mage_Core_Model_App->throwStoreException() #1 /public_html/app/code/core/Mage/Core/Model/App.php(491): Mage_Core_Model_App->getStore() #2 /home
/public_html/app/code/core/Mage/Core/Model/App.php(274): Mage_Core_Model_App->_initCurrentStore('default', 'store') #3 
/public_html/app/Mage.php(616): Mage_Core_Model_App->init('default', 'store', Array) #4
 /public_html/mege_dbbackup.php(9): Mage::app('default') #5 {main} thrown in 
 /public_html/app/code/core/Mage/Core/Model/App.php on line 1357 

Best Answer

When you do a mysql dump, you're not just dumping a single store but the entire Magento database. Your issue is that

require_once 'app/Mage.php';
       Mage::app('default');

You're loading 'default' as the scope. This throws an exception:

/**
 * Initialize currently ran store
 *
 * @param string $scopeCode code of default scope (website/store_group/store code)
 * @param string $scopeType type of default scope (website/group/store)
 * @return unknown_type
 */
protected function _initCurrentStore($scopeCode, $scopeType)
{
    Varien_Profiler::start('mage::app::init::stores');
    $this->_initStores();
    Varien_Profiler::stop('mage::app::init::stores');

    if (empty($scopeCode) && !is_null($this->_website)) {
        $scopeCode = $this->_website->getCode();
        $scopeType = 'website';
    }
    switch ($scopeType) {
        case 'store':
            $this->_currentStore = $scopeCode;
            break;
        case 'group':
            $this->_currentStore = $this->_getStoreByGroup($scopeCode);
            break;
        case 'website':
            $this->_currentStore = $this->_getStoreByWebsite($scopeCode);
            break;
        default:
            $this->throwStoreException();
    }

Just leave it blank as in:

    require_once 'app/Mage.php';
       Mage::app();
Related Topic