Tag: shell

  • Run Magento Data Flow Profile from shell

    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.