Posted on 10 Comments

Run Magento Data Flow Profile from shell

Magento Logo

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.

Posted on 2 Comments

Determine last day of month using Bash script

Oftentimes software systems need to carry out tasks on a regular basis at a given time, such as archiving log files or sending out newsletters. Normally cron is able to handle all kinds of time-based setups (daily, monthly, etc.) but what if you want to execute tasks on the last day of each month?

A simple bash script does the trick:

#!/bin/bash

TODAY=`/bin/date +%d`
TOMORROW=`/bin/date +%d -d "1 day"`

if [ $TOMORROW -lt $TODAY ]; then
   exit 0
fi

exit 1

So, as you can see we need to simply check if the next day is “less” than today. All you need to do is to call this script at the required time each day and it will check if it’s the last day of the current month, like so:

59 23 * * * isLastDayOfMonth.sh && yourJob.sh

Thats’s it.

Posted on Leave a comment

Print list of folders sorted by their size

Source Code Icon

In order to print a list of folders and files sorted according to their size you only need two built-in UNIX commands, put together using the pipe (“|”):

  1. du
  2. sort
du -h /var/www/ --max-depth=1 | sort -h

Whereas du gives you the storage space occupied by certain files and folders, sort actually sorts the resulting list. Using the additional parameter max-depth you can specify the folder depth which du will use to echo files and folders on the shell during its recursive parsing process.