Shell Scripting which is great for task automation, falls short when it comes to web browser automation.
In the case of Magento, using Shell script you can do many operations like Installation, Migration, Deployment, Backup, etc. & even more. But when it comes to run the Database Repair Tool(a great tool for repairing Magento database while upgrading) it becomes trickier and complex.
While Upgrading Magento says from 1.3.2.4 to 1.9.1.1, running DB repair tool for every version manually from the browser is repetitive & hectic. So I thought of writing a Ruby CLI script to automate it which will be a simple command with just two parameters(DB repair Url and Magento version).
This console script is a Ruby script which uses Mechanize gem/library.
The Mechanize library is used for automating interaction with websites. Mechanize automatically stores and sends cookies, follows redirects, and can follow links and submit forms. Form fields can be populated and submitted. Mechanize also keeps track of the sites that you have visited as a history.
Install Ruby
Ruby comes pre-installed in Mac. If you are in other operating systems please refer to the Installation Document
To test if ruby is already installed, run the following command:
ruby -v
Which will give you the output like:
ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14]
Install Mechanize Gem
You need to install RVM(Ruby Version Manager) first.
First, install the stable version of RVM stable with ruby:
\curl -sSL https://get.rvm.io | bash -s stable --ruby
Update your gems:
sudo gem update --system
Now you can install any gems. Here we will be installing mechanize gem:
gem install mechanize
Prepare & Run the Script
After installation of Ruby, RVM and all it’s required gems, we are ready to go for coding the script.
Assumptions:
OS: Mac OS X Yosemite 10.10.4
Ruby: 2.0
Magento Reference Database: magento[version]_vanilla
1. Prepare the Script
Copy the following ruby script to the root of your Magento Dir:
File: mage-db-repair-tool.rb
#!/usr/bin/env ruby
#
# Magento DB Repair tool using CLI
# Uses Ruby's Mechanize gem
#
# @author Raj KB <[email protected]>
# @website https://www.magepsycho.com
#
# Tested on Mac OS-X 10.X
#
require 'mechanize'
require 'fileutils'
#################################
# FUNCTIONS
#################################
def checkError(page)
if page.search('.msg_error').length > 0
puts "[ERROR]"
page.search('.msg_error li').each do |li|
puts li.text.strip
end
exit
end
end
def checkResult(page)
puts "[RESULT]"
page.search('.msg_success li').each do |li|
puts li.text.strip
end
end
def checkNotice(page)
puts "[NOTICE]"
page.search('.msg-note').each do |note|
puts note.text.strip
end
if page.search('.msg-note').length > 1
puts "See log for more details"
end
end
#################################
# SCRIPT CODE
#################################
abort "#{$0} Argument Missing" if (ARGV.size < 1)
dbRepairUrl = ARGV[0]
mageVersion = ARGV[1]
mageDir = Dir.pwd
dbRepairLogDir = "#{mageDir}/var/dp-repair-tool"
FileUtils.mkdir_p "#{dbRepairLogDir}"
fp = File.new("#{dbRepairLogDir}/mage-#{mageVersion}-result.html", "a+")
agent = Mechanize.new
page = agent.get(dbRepairUrl)
puts "Loading page: #{page.title}..."
form = page.forms.first
puts "Setting db repair form values..."
# Get DB value from app/etc/local.xml
xmlFile = File.open("#{mageDir}/app/etc/local.xml")
doc = Nokogiri::XML(xmlFile)
corruptedHostname = doc.xpath('/config/global/resources/default_setup/connection/host').text()
corruptedUsername = doc.xpath('/config/global/resources/default_setup/connection/username').text()
corruptedPassword = doc.xpath('/config/global/resources/default_setup/connection/password').text()
corruptedDatabase = doc.xpath('/config/global/resources/default_setup/connection/dbname').text()
xmlFile.close
form['post_form'] = 'true'
form['corrupted[hostname]'] = corruptedHostname
form['corrupted[database]'] = corruptedDatabase
form['corrupted[username]'] = corruptedUsername
form['corrupted[password]'] = corruptedPassword
# Edit reference database credentials
form['reference[hostname]'] = corruptedHostname
form['reference[database]'] = "magento#{mageVersion}_vanilla"
form['reference[username]'] = corruptedUsername
form['reference[password]'] = corruptedPassword
#p form; exit
puts "Submitting db repair form..."
result_page = form.submit(form.button_with(:id => "button-continue"))
# Check if there is an error & exit
checkError(result_page)
# Continue if there is not an error
# Check if it further requires submission
if result_page.search('button#button-continue').length > 0
checkNotice(result_page)
# Form found again?
puts "Submitting again..."
form = result_page.forms.first
result_page = form.submit(form.button_with(:id => "button-continue"))
checkResult(result_page)
checkNotice(result_page)
else
checkResult(result_page)
checkNotice(result_page)
end
fp.write(result_page.body)
You can also download the script from Magento DB Repair Tool Using CLI – Ruby + Mechanize
2. Run the Script
Give the script the executable permission:
cd /path/to/mage-db-repair-tool.rb
chmod +x ./mage-db-repair-tool.rb
Now, You can run the script either by using the command:
ruby mage-db-repair-tool.rb <magento-db-repair-url> <magento-version>
or simply using:
./mage-db-repair-tool.rb http://magento-test.dev/magento-db-repair-tool-1.2.php 1702
Example:
./mage-db-repair-tool.rb http://magento-test.dev/magento-db-repair-tool-1.2.php 1702
Apart from the results shown at the console output (refer to the above snapshot), You can also check the detailed output logged in your /path/to/magento/var/db-repair-tool/mage-[magento-version]-result.html
Bonus Tips:
If you want to make system-wide command, then copy the file to the system-wide paths like /usr/local/bin, /usr/bin, /bin etc:
sudo cp /path/to/your/mage-db-repair-tool.rb /usr/local/bin/mageDbRepairToolRb
Now you can run the command from anywhere
mageDbRepairToolRb http://magento-test.dev/magento-db-repair-tool-1.2.php 1702
Shall you have any issues please post a comment below and I’ll try and help you out.