Run Magento Data Flow Profile from shell

Running Magento data flow profiles for importing/exporting data is quite resource intensive and can take a while to execute for larger junks of data. Although there exist alternative ways to import/export data in Magento sometimes existing data flow profiles can’t be replaced just yet. Below you find a shell script to run Magento data flow profiles from the shell based on the data flow profile id. You can execute this script using PHP or PHP-CI, thus being able to specify different resource limits for your PHP setups (e.g. time outs)

Create data flow profile

Go ahead and create a data flow profile as usual through Magento’s admin interface and take note of the profile id (13) in the URL: Magento data flow profile URL You will use this profile ID to run the data flow profile via the shell script provided below.

Run data flow profile via shell script

In order to execute the script specify the data flow profile id using the –profile switch, e.g. using PHP-CLI.


$ php5-5.4-cli ./run_data_flow_profile.php --profile 13

This will run the data flow profile with the ID 13.

Magento data flow profile shell script

Save the code below in a file called run_data_flow_profile.php in Magento’s shell folder. Use the command specified above to run the script from within your shell.

<?php

require_once 'abstract.php';

/**
 * @author Matthias Kerstner <matthias@kerstner.at>
 * @version 1.0.0
 */
class Mage_Shell_Run_Data_Flow_Profile extends Mage_Shell_Abstract {

  /** @var string this module's namespace */
  private static $_MODULE_NAMESPACE = 'kerstnerat_rundataflowprofileshell';

  /**
   * Logs $msg to logfile specified in configuration.
   * @param string $msg
   */
  private function logToFile($msg) {
    Mage::log($msg, null, self::$_MODULE_NAMESPACE . '.log');
  }

  /**
   * Run script based on shell arguments specified.
   */
  public function run() {
    try {
      if (!$this->getArg('profile')) {
        throw new Exception('Missing profile');
      }

      $profileId = (int) $this->getArg('profile');

      $this->logToFile('Profile started: ' . $profileId . ' at ' . date('Y-m-d H:i:s')
        . '...');

      $profile = Mage::getModel('dataflow/profile');
      $userModel = Mage::getModel('admin/user');
      $userModel->setUserId(0);

      Mage::getSingleton('admin/session')->setUser($userModel);
      $profile->load($profileId);

      if (!$profile->getId()) {
        $this->logToFile('error: ' . $profileId . ' - incorrect profile id');
        return;
      }

      Mage::register('current_convert_profile', $profile);
      $profile->run();

      $this->logToFile('Profile ended: ' . $profileId . ' at ' . date('Y-m-d H:i:s'));
    } catch (Exception $ex) {
      $this->logToFile($ex->getMessage());
      echo $this->usageHelp();
    }
  }

 /**
  * Retrieve Usage Help Message.
  */
 public function usageHelp() {
   return " 
Usage: php -f run_data_flow_profile.php --[options]
 
 --profile  Data Flow Profile ID
 help Show this help
 
  ID of Data Flow Profiles to run";
 }
}

$shell = new Mage_Shell_Run_Data_Flow_Profile();
$shell->run();

Compatibility

The script has been tested with Magento 1.8 and higher. If you happen to find a problem please leave a comment.

You may also like...

8 Responses

  1. Shashikant says:

    I have 1.9 version of magento and I tried to run the above code with minor changes as “$ php5-5.4-cli ./run_data_flow_profile.php –profile 13″ is not running in command prompt so I tried”$ php ./run_data_flow_profile.php –profile 13”, but it is not exporting all products from my website. I have this profile to export all my listed products. Help me to solve he problem.

  2. Hi, unfortunately I don’t know your data flow profile and your (optional) filter settings. So I suggest that you check your profile by running it from within Magento first. If this works than please enable debugging in Magento and potentially in the script provided to investigate further. Hope that helps. Cheers

  3. Jickson says:

    Hello,

    If I want to do a dataflow import, where do I mention the import file ( file selection in RUN tab ) ?

    thanks,

    • PJ says:

      For anyone seeking this information, the import file is set within your data profile configuration settings within the magento admin panel

  4. Martin says:

    Works like charm to export 22.202 customers from Magento 1.9.2.3. Thanx Man !

  5. You’re most certainly welcome 🙂

  6. Nicola says:

    I use Magento 1.9.3.1.
    The script works well for the dataflow default profile Export Product Stocks, but does not work with the profile Import Product Stocks.
    This is log for default dataflow profile Export Product Stocks and Import Product Stocks:
    2017-01-17T11:57:46+00:00 DEBUG (7): Profile started: 2 at 2017-01-17 11:57:46…
    2017-01-17T11:57:48+00:00 DEBUG (7): Profile ended: 2 at 2017-01-17 11:57:48
    2017-01-17T11:58:12+00:00 DEBUG (7): Profile started: 4 at 2017-01-17 11:58:12…
    2017-01-17T11:58:12+00:00 DEBUG (7): Profile ended: 4 at 2017-01-17 11:58:12

    What am I doing wrong?

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *