Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento: Possible to run export profile from shell ssh

I'm trying to export 18,0000 products from Magento and it gets stuck at the

 Warning: Please do not close the window during importing/exporting data

stage.

I've found that it's possible to quickly reindex this many products by using php indexer.php --reindex <code>

Is it possible to use something similar to force export all products, or run a dataflow profile by its id?

like image 901
James Avatar asked Aug 08 '12 02:08

James


People also ask

How to use SSH in Magento 2?

To use SSH, you need to: 1 Generate your SSH public and private keys. 2 Add your SSH public key to your remote server either through CLI commands or the Project Web Interface. 3 Use Magento Cloud CLI or Git commands to SSH to an environment.

How do I export my Magento data?

You can use the following methods to export your Magento data in your desired format: Launch Magento on your system and log in to your account with your credentials. Once you have logged in, select the system option from the panel on the top. Click on the System option, then dataflow and finally select the “export all products” option.

How to connect to remote Magento 2 server?

In this case, our Magento 2 source code placed in /var/www/magento2, this may be difference from your server. By Default, Mac OS and Linux has Terminal tool, you can use this to access to remote Magento server. -pPORT (Optional): SSH Server Port.

What is SSH (Secure Shell)?

SSH, or Secure Shell, is a common protocol used to securely log into remote servers and systems. You will typically use SSH to access your environments directly to enter CLI commands for managing your branching, creating variables, and much more. We also support sFTP (Secure FTP) using your SSH public key.


1 Answers

This will run the export profile by ID and then clean the dataflow_batch_? table afterwards. Be aware that it runs as the command line user and can create cache files as that user which can be inconvenient under certain web server configurations. It wouldn't hurt to clear the cache after run.

For a while, on 1.4.1.1, this was the only way to export large quantities of product until we found the memory leak and fixed it.

<?php

/***********************
 * Import/Export Script to run Import/Export profile 
 * from command line or cron. Cleans entries from dataflow_batch_(import|export) table
 ***********************/

$mageconf = './app/etc/local.xml';  // Mage local.xml config
$mageapp  = './app/Mage.php';       // Mage app location
$logfile  = 'export_data.log';      // Import/Export log file

/* uncomment following block when moved to server - to ensure this page is 
 * not accessed from anywhere else 
 */

//if ($_SERVER['REMOTE_ADDR'] !== '<your server ip address>') {
//   die("You are not a cron job!");
//}


/* System -> Import/Export -> Profiles get profile ID from 
 * Magento Import/Export Profiles
 */

$profileId = 9;

/* Post run housekeeping table bloat removal
 * imports use "dataflow_batch_import" table
 * exports use "dataflow_batch_export" table
 */

$table = 'dataflow_batch_export';

/* Scan Magento local.xml file for connection information */

if (file_exists($mageconf)) {

$xml = simplexml_load_file($mageconf, NULL, LIBXML_NOCDATA);

$db['host'] = $xml->global->resources->default_setup->connection->host;
$db['name'] = $xml->global->resources->default_setup->connection->dbname;
$db['user'] = $xml->global->resources->default_setup->connection->username;
$db['pass'] = $xml->global->resources->default_setup->connection->password;
$db['pref'] = $xml->global->resources->db->table_prefix;

} 

else {
    Mage::log('Export script failed to open Mage local.xml', null, $logfile);
    exit('Failed to open Mage local.xml');
}


/* Initialize profile to be run as Magento Admin and log start of export */

require_once $mageapp;
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$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()) {
    Mage::getSingleton('adminhtml/session')->addError('ERROR: Incorrect profile id');
}

Mage::log('Export ' . $profileId . ' Started.', null, $logfile);

Mage::register('current_convert_profile', $profile);
$profile->run();
$recordCount = 0;
$batchModel = Mage::getSingleton('dataflow/batch');

Mage::log('Export '.$profileId.' Complete. BatchID: '.$batchModel->getId(), null, $logfile);

echo "Export Complete. BatchID: " . $batchModel->getId() . "\n";

/* Connect to Magento database */

sleep(30);

mysql_connect($db['host'], $db['user'], $db['pass']) or die(mysql_error());
mysql_select_db($db['name']) or die(mysql_error());

/* Truncate dataflow_batch_(import|export) table for housecleaning */

$querystring = "TRUNCATE ".$db['pref'].$table;

mysql_query($querystring) or die(mysql_error());

?> 
like image 170
Fiasco Labs Avatar answered Sep 26 '22 03:09

Fiasco Labs