Ready to get started?

Learn more about the Magento PowerShell Cmdlets or download a free trial:

Download Now

Magento Data をMySQL にレプリケーションするPowerShell スクリプト

PowerShell でシンプルなスクリプトを記述し、Magento data をMySQL データベースにレプリケーション(複製)。

CData Cmdlets for Magento をつかって、PowerShell からリアルタイムMagento data に連携できます。データ同期などのタスクの連携にぴったりの製品です。 本記事では、PowerShell からCData Cmdlets for Magento およびCData Cmdlets for MySQL を使って、同期スクリプトを作成して実行します。

After obtaining the needed connection properties, accessing Magento data in PowerShell and preparing for replication consists of four basic steps.

Magento uses the OAuth 1 authentication standard. To connect to the Magento REST API, you will need to obtain values for the OAuthClientId, OAuthClientSecret, and CallbackURL connection properties by registering an app with your Magento system. See the "Getting Started" section in the help documentation for a guide to obtaining the OAuth values and connecting.

You will also need to provide the URL to your Magento system. The URL depends on whether you are using the Magento REST API as a customer or administrator.

  • Customer: To use Magento as a customer, make sure you have created a customer account in the Magento homepage. To do so, click Account -> Register. You can then set the URL connection property to the endpoint of your Magento system.

  • Administrator: To access Magento as an administrator, set CustomAdminPath instead. This value can be obtained in the Advanced settings in the Admin menu, which can be accessed by selecting System -> Configuration -> Advanced -> Admin -> Admin Base URL.

    If the Use Custom Admin Path setting on this page is set to YES, the value is inside the Custom Admin Path text box; otherwise, set the CustomAdminPath connection property to the default value, which is "admin".

Collecting Magento Data

  1. Install the module:

    Install-Module MagentoCmdlets
  2. Connect to Magento:

    $magento = Connect-Magento -OAuthClientId $OAuthClientId -OAuthClientSecret $OAuthClientSecret -CallbackURL $CallbackURL -Url $Url
  3. Retrieve the data from a specific resource:

    $data = Select-Magento -Connection $magento -Table "Products"

    You can also use the Invoke-Magento cmdlet to execute pure SQL-92 statements:

    $data = Invoke-Magento -Connection $magento -Query 'SELECT * FROM Products WHERE Style = @Style' -Params @{'@Style'='High Tech'}
  4. Save a list of the column names from the returned data.

    $columns = ($data | Get-Member -MemberType NoteProperty | Select-Object -Property Name).Name

Inserting Magento Data into the MySQL Database

With the data and column names collected, you are ready to replicate the data into a MySQL database.

  1. Install the module:

    Install-Module MySQLCmdlets
  2. Connect to MySQL, using the server address and port of the MySQL server, valid user credentials, and a specific database with the table in which the data will be replicated:

    $mysql = Connect-MySQL -User $User -Password $Password -Database $Database -Server $Server -Port $Port
  3. Loop through the Magento data, store the values, and use the Add-MySQL cmdlet to insert the data into the MySQL database, one row at a time. In this example, the table will need to have the same name as the Magento resource (Products) and to exist in the database.

    $data | % { $row = $_ $values = @() $columns | % { $col = $_ $values += $row.$($col) } Add-MySQL -Connection $mysql -Table "Products" -Columns $columns -Values $values }

You have now replicated your Magento data to a MySQL database. This gives you freedom to work with Magento data in the same way that you work with other MySQL tables, whether that is performing analytics, building reports, or other business functions.

Notes

  • Once you have connected to Magento and MySQL in PowerShell, you can pipe command results to perform the replication in a single line:

    Select-Magento -Connection $magento -Table "Products" | % { $row = $_ $values = @() $columns | % { $col = $_ $values += $row.$($col) } Add-MySQL -Connection $mysql -Table "Products" -Columns $columns -Values $values }
  • If you wish to replicate the Magento data to another database using another PowerShell module, you will want to exclude the Columns, Connection, and Table columns from the data returned by the Select-Magento cmdlet since those columns are used to help pipe data from one CData cmdlet to another:

    $columns = ($data | Get-Member -MemberType NoteProperty | Select-Object -Property Name).Name | ? {$_ -NotIn @('Columns','Connection','Table')}
 
 
ダウンロード