Run Magento database repair tool from CLI using Ruby’s Mechanize

June 11, 2015  |  No Comments  |  by Raj (MagePsycho)  |  Linux, Magento, Ruby

Shell Scripting which is great for task automation, falls short when it comes to web browser automation.
In 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.

Magento Database Repair Tool

Magento Database Repair Tool

While Upgrading Magento say from 1.3.2.4 to 1.9.1.1, running DB repair tool for every version manually from 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 system 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 instally 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 <magepsycho@gmail.com>
# @website  http://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 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 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
Ruby DB Repair Tool Console Output

Ruby DB Repair Tool Console Output

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/dp-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.

Backup WordPress project files / db using bash script

August 8, 2013  |  5 Comments  |  by Raj (MagePsycho)  |  Linux, Magento

WordPress is undoubtedly the most popular blogging platform on the Internet. And when it comes to the maintenance or regular backups, life is not easier unless you have some backup script which works great via SSH & cron daemons.

Here is the custom backup script used for taking backup of wordpress project files & db:
(If you want backup script for Magento then please go here)

#!/bin/bash
#@author		MagePsycho <magepsycho@gmail.com>
#@website		http://www.magepsycho.com
#@blog			http://www.blog.magepsycho.com
#@version		0.1.0

#/************************ EDIT VARIABLES ************************/
projectName=magepsycho_blog
backupDir=/home/magepsycho/_backups
#/************************ //EDIT VARIABLES **********************/

fileName=$projectName-$(date +"%Y-%m-%d")
host=$(grep DB_HOST "wp-config.php" |cut -d "'" -f 4)
username=$(grep DB_USER "wp-config.php" | cut -d "'" -f 4)
password=$(grep DB_PASSWORD "wp-config.php" | cut -d "'" -f 4)
dbName=$(grep DB_NAME "wp-config.php" |cut -d "'" -f 4)

printf "What kind of backup you would like?\n[ d ] DB backup only\n[ f ] Files backup only\n[ b ] Files backup with DB\n"
read backupType
if [[ $backupType = @(d|b) ]]; then
	echo "----------------------------------------------------"
	echo "Dumping MySQL..."
	mysqldump -h "$host" -u "$username" -p"$password" "$dbName" | gzip > $fileName.sql.gz
	echo "Done!"
fi

if [[ $backupType = @(f|b) ]]; then
	echo "----------------------------------------------------"
	echo "Archiving Files..."
	tar -zcf $fileName.tar.gz * .htaccess
	echo "Done!"
	echo "----------------------------------------------------"
	echo "Cleaning..."
	rm -f $fileName.sql.gz
	echo "Done!"
fi

if [[ $backupType = @(d|f|b) ]]; then
	echo "----------------------------------------------------"
	mkdir -p $backupDir;
	echo "Moving file to backup dir..."
	if [[ $backupType == d ]]; then
		mv $fileName.sql.gz $backupDir
	fi

	if [[ $backupType = @(f|b) ]]; then
		mv $fileName.tar.gz $backupDir
	fi
	echo "Done!"
else
	echo "Invalid selection!"
fi

Or you can download it from here: [WordPress Backup Script]
Notes: If you get the following error:

syntax error in conditional expression: unexpected token `(‘
line 20: syntax error near `@(d’
line 20: `if [[ $backupType = @(d|b) ]]; then’

then this means you are using older version of bash (< 4.0). And you need to patch the script by adding the following line after bash script declaration: [code language="bash"]shopt -s extglob[/code]

What does this script do?

1. Gives options for backup type which are:
– DB backup only
– Files backup only
– Files backup with DB
2. Dumps the database by taking db info from wp-config.php
3. Makes a copy of project files and compresses it in .tar.gz format.
4. Deletes the dumped SQL file as it’s already copied in compressed file.
5. Creates backup dir if not exists
6. Copies the compressed project file to the backup dir.

Besides, you can also setup cron job to run this backup script at regular intervals.

How to run backup script?

1. Edit _wpbackup.sh to configure variables ‘projectName’ & ‘backupDir’
2. Upload the edited _wpbackup.sh to the root of your WordPress installation
3. Run following series of commands in terminal:

cd /path/to/wordpress/root
chmod +x _wpbackup.sh
ex -sc $'%s/\r$//e|x' _wpbackup.sh
sh _wpbackup.sh

4. You will get the compressed backup file at backup dir

Snapshots for different backup types

DB Backup Only

DB Backup Only


File Backup Only

File Backup Only

Files Backup with DB

Files Backup with DB

Thanks for reading & sharing.

Backup Magento project files / db using bash script

July 1, 2013  |  8 Comments  |  by Raj (MagePsycho)  |  Linux, Magento

I have googled for the bash script to backup Magento sites/db but none of them worked for me the way I wanted.
So I decided to create a custom bash script which is simple and does the job perfectly.

I have developed the script for my own need. But I thought it would be helpful if shared with you guys as well.
Here goes the overall backup script:

#!/bin/bash
#@author		MagePsycho <magepsycho@gmail.com>
#@website		http://www.magepsycho.com
#@version		0.1.0

#/************************ EDIT VARIABLES ************************/
projectName=magepsycho
backupDir=/home/magepsycho/_backups
#/************************ //EDIT VARIABLES **********************/

dbXmlPath="app/etc/local.xml"
{
host="$(echo "cat /config/global/resources/default_setup/connection/host/text()" | xmllint --nocdata --shell $dbXmlPath | sed '1d;$d')"
username="$(echo "cat /config/global/resources/default_setup/connection/username/text()" | xmllint --nocdata --shell $dbXmlPath | sed '1d;$d')"
password="$(echo "cat /config/global/resources/default_setup/connection/password/text()" | xmllint --nocdata --shell $dbXmlPath | sed '1d;$d')"
dbName="$(echo "cat /config/global/resources/default_setup/connection/dbname/text()" | xmllint --nocdata --shell $dbXmlPath | sed '1d;$d')"
}

fileName=$projectName-$(date +"%Y-%m-%d")
printf "What kind of backup you would like?\n[ d ] DB backup only\n[ f ] Files backup only\n[ b ] Files backup with DB\n"
read backupType
if [[ $backupType = @(d|b) ]]; then
	echo "----------------------------------------------------"
	echo "Dumping MySQL..."
	mysqldump -h $host -u $username -p$password $dbName > $fileName.sql
	echo "Done!"
fi

if [[ $backupType = @(f|b) ]]; then
	echo "----------------------------------------------------"
	echo "Archiving Files..."
	printf "Skip /media folder?\ny: Yes\nn: No\n"
	read skipMedia
	if [ $skipMedia == y ]; then
		tar -zcf $fileName.tar.gz --exclude=var --exclude=includes --exclude=media * .htaccess
	else
		tar -zcf $fileName.tar.gz --exclude=var --exclude=includes * .htaccess
	fi
	echo "Done!"
	echo "----------------------------------------------------"
	echo "Cleaning..."
	rm -f $fileName.sql
	echo "Done!"
fi

if [[ $backupType = @(d|f|b) ]]; then
	echo "----------------------------------------------------"
	mkdir -p $backupDir;
	echo "Moving file to backup dir..."
	if [ $backupType == d ]; then
		mv $fileName.sql $backupDir
	fi

	if [[ $backupType = @(f|b) ]]; then
		mv $fileName.tar.gz $backupDir
	fi
	echo "Done!"
else
	echo "Invalid selection!"
fi

Or you can download it from [here]
Notes: If you get the following error:

syntax error in conditional expression: unexpected token `(‘
line 24: syntax error near `@(d’
line 24: `if [[ $backupType = @(d|b) ]]; then’

then this means you are using older version of bash (< 4.0). And you need to patch the script by adding the following line after bash script declaration:[code language="bash"] shopt -s extglob [/code]

What does this script do?

1. Gives options for backup type which are:
– DB backup only
– Files backup only
– Files backup with DB
2. Dumps the database by taking DB info from XML configuration.
3. Makes a copy of project files and compresses it in .tar.gz format.
While copying it also gives options whether to exclude media folder or not.
Note that by default ‘var’ & ‘includes’ folder are excluded by the script.
4. Deletes the dumped SQL file as it’s already copied in the compressed file.
5. Creates backup dir if not exists
6. Copies the compressed project file to the backup dir.

Besides, you can also setup Cron job to run this backup script at regular intervals.

How to run backup script?

1. Edit _magebackup.sh to configure variables ‘projectName’ & ‘backupDir’
2. Upload the edited _magebackup.sh to the root of your Magento installation
3. Run following series of commands in terminal:

cd /path/to/magento/root
chmod +x _magebackup.sh
ex -sc $'%s/\r$//e|x' _magebackup.sh
sh _magebackup.sh

4. You will get the compressed backup file at backup dir

Snapshots for different backup types

DB backup only


Files backup only


Files backup with DB


Let’s comment if there’s any room for improvement in this script.
Thanks for reading & sharing.