Shantanu's Blog

Database Consultant

February 19, 2012

 

Zmanda Quick Backup

Zmanda quick backup is a useful tool to take and schedule incremental backups. It has more features than the the built-in tool mysqldump.

http://www.zmanda.com/quick-mysql-backup.html

# download and install ZRM:
wget http://www.zmanda.com/downloads/community/ZRM-MySQL/2.2/RPM/MySQL-zrm-2.2.0-1.noarch.rpm
rpm -iUh *

# configuration parameters for ZRM Tools:
cd /etc/mysql-zrm
mkdir dailyrun
cp mysql-zrm.conf dailyrun/
cd dailyrun
vi mysql-zrm.conf

# change the configuration file on mysql-zrm.conf:
backup-level=0
backup-mode=logical
user="user1"
password="user1"
destination=/var/lib/mysql-zrm

# taking the backup:
mysql-zrm-scheduler --now --backup-set dailyrun

# restoring the full backup:
mysql-zrm --action restore --backup-set fullbackup --source-directory /var/lib/mysql-zrm/dailyrun/20120219074625 --databases "test"

# Schedules a daily backup at 1am everyday using cron:
mysql-zrm-scheduler --add --interval daily --start 01:00 --backup-level 0 --backup-set dailyrun
mysql-zrm-scheduler --query

# crontab -l
0 1 * * * /usr/bin/zrm-pre-scheduler --action backup --backup-set dailyrun --backup-level 0 --interval daily
0 4 * * * /usr/bin/mysql-zrm --action purge

# for more options for mysq-zrm are as follows:
mysql-zrm --action --help

# If you get the following error, you will have to install perl DBD mysql module.
# the DBD::mysql perl module hasn't been fully installed

wget http://search.cpan.org/CPAN/authors/id/C/CA/CAPTTOFU/DBD-mysql-4.020.tar.gz
tar xvf DBD-mysql-4.020.tar.gz
cd DBD-mysql-4.020
perl Makefile.PL
make
make install

# Other useful ZRM commands:
mysql-zrm-reporter --where backup-set=dailyrun --show backup-performance-info
mysql-zrm-reporter --where backup-set=dailyrun --show restore-info
mysql-zrm --action verify-backup --backup-set dailyrun

# backup report:

cat /var/lib/mysql-zrm/dailyrun/20120219074625/index

backup-set=dailyrun
backup-date=20120219074625
mysql-server-os=Linux/Unix
backup-type=regular
host=localhost
backup-date-epoch=1329655585
mysql-zrm-version=ZRM for MySQL Community Edition - version 2.2.0
mysql-version=5.5.20
backup-directory=/var/lib/mysql-zrm/dailyrun/20120219074625
backup-level=0
raw-databases=mysql
logical-databases=performance_schema test
backup-size=0.87 MB
read-locks-time=00:00:01
flush-logs-time=00:00:00
backup-time=00:00:01
backup-status=Backup succeeded

Labels: ,


February 14, 2012

 

Improving restoration speed of dumped data

Let's assume we want to take the backup of a big table "customer" that has 1 primary key and 2 secondary keys i.e. name and city. When we insert records in this table, both the indexes will get updated. If we add these indexes after inserting all the records, we can restore the data much faster. This was added as feature request...

http://bugs.mysql.com/bug.php?id=64248

I have created a stand-alone mysqldump utility that will add the keys after generating all "insert into" statements as shown below.

# ./mysqldump.static test customer --keys-last

CREATE TABLE `customer` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT NULL,
`city` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`,`city`),
KEY `city` (`city`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

ALTER TABLE `customer` DROP KEY `name` , DROP KEY `city` ;
INSERT INTO `customer` VALUES (1,'Amar','mumbai'),(2,'Akbar','delhi');
ALTER TABLE `customer` ADD KEY `name` (`name`,`city`), ADD KEY `city` (`city`);

As you can see from the above example, the "keys-last" option will force the dump utility to drop the indexes first and then recreate them after all inserts. This will drastically improve the time it takes to restore data from a dumped file. The time saved will depend upon the number of rows and indexes, but overall 20% gain is something that can be achieved very easily!

The utility can be downloaded from...

http://dl.dropbox.com/u/49489184/mysqldump.static
_____

percona seems to have done this long back using the option "--innodb-optimize-keys".

http://www.percona.com/downloads/Percona-Server-5.5/Percona-Server-5.5.20-24.1/RPM/rhel5/x86_64/

It applies to InnoDB tables only.

More info...

http://code.google.com/p/mysqldump/downloads/list

Labels: ,


February 11, 2012

 

aws command prompt

In order to use S3 and ec2 Simple Command-Line Access to AWS we need to install an utility:

http://aws.amazon.com/developertools/Amazon-SimpleDB/739

Here are the steps to follow. This will help to manage the buckets using command prompt and there is no need to use the web console.

curl https://raw.github.com/timkay/aws/master/aws -o aws
chmod +x aws
vi ~/.awssecret
(type key and secret key)
chmod go-rwx ~/.awssecret

perl aws --install

s3mkdir shantanuo_BUCKET_NAME
s3put shantanuo_BUCKET_NAME test.txt
s3ls shantanuo_BUCKET_NAME
s3get shantanuo_BUCKET_NAME/test.txt
s3delete shantanuo_BUCKET_NAME/test.txt
s3delete shantanuo_BUCKET_NAME

ec2run
ec2din --simple
ec2tin INSTANCEID

Labels:


 

AWS product stack

Here is the list of tabs those I can see when I log in. I am not impressed by most of them, but EC2, RDS and DynamoDB will prove miracles in the hosting world.

Elastic Beanstalk: makes it even easier to deploy and manage scalable applications on the AWS cloud, while retaining the ability to control the underlying resources.

S3: Buckets to store data that can also be shared on the net.

EC2: Start or terminate different type of virtual servers.

VPC: Virtual Private cloud for your EC2 resources.

CloudWatch: monitors operational and performance metrics for your AWS cloud resources and applications.

Elastic MapReduce: Creating a job flow to process your data in a simple and quick way.

CloudFront: Allow public access to your S3 buckets

CloudFormation: pre installed Drupal, Joomla or wordpress

RDS: hosted Relational Database Systems like MySQL / Oracle

ElastiCache: a web service that makes it easier to launch, manage, and scale a distributed in-memory cache in the cloud.

SQS: Simple Queue Service offers a reliable, highly scalable, hosted queue for storing messages as they travel between applications.

IAM: Identity and Access Management (IAM) enables you to manage Users, groups and permissions within your AWS account.

SNS: Simple Notification Service (SNS) is a web service that makes it easy to set up, operate, and send notifications in the cloud.

SES: Simple Email Service (SES) allows you to send emails.

Route 53: Allows you to manage your domain name and sub-domains.

DynamoDB: a fully managed non-relational fast database service that comes with seamless scalability.

Storage Gateway: An on-premises virtual appliance that provides seamless and secure integration between your appliances and AWS's storage infrastructure.

Labels:


February 10, 2012

 

Import, Export and SQLize DynamoDB

You can use Hive to export data from DynamoDB to S3 bucket using Hadoop

In order to use hadoop, you need Elastic MapReduce instance. Click on "Create New Job Flow" and use defauls to create a new job called "My Job Flow". You will now get the Master Public DNS Name to connect to using "hadoop" as username. Once connected, type "hive" to get the command prompt where you can link to DynamoDB table.

hive> CREATE EXTERNAL TABLE hivereply (col1 string, col2 string, col3 string, col4 string)
STORED BY 'org.apache.hadoop.hive.dynamodb.DynamoDBStorageHandler'
TBLPROPERTIES ("dynamodb.table.name" = "reply",
"dynamodb.column.mapping" = "col1:Id,col2:ReplyDateTime,col3:Message,col4:PostedBy");

You can now use the table hivereply just like any other MySQL table. For e.g.
hive> select * from hivereply where col2 = '2012-01-31 23:09:46';

Amazon DynamoDB#DynamoDB Thread 2 2012-01-31 23:09:46 null

You can call the INSERT OVERWRITE command to write the data to an external directory. You can use this to create an archive of your Amazon DynamoDB data in Amazon S3.

hive> INSERT OVERWRITE DIRECTORY 's3://php-sdk-getting-started-aki-19/' SELECT * FROM hiveTableName;

Labels: , ,


 

Using Comparision Operator in dynamodb

If you want to query the data, you need to index it on Range key. For e.g. in the following "reply" table we have a primary key that is combination of Hash key and a Range key. Range key is nothing but the timestamp as on the reply was received. We can write a query that will return all the replies those are older than 7 days.
In this case the column "ReplyDateTime" column is indexed and we can use Comparison Operator.


require_once 'sdk.class.php';

$ten_days_ago = date('Y-m-d H:i:s', strtotime("-10 days"));

// Instantiate the class
$dynamodb = new AmazonDynamoDB();

$add_response = $dynamodb->put_item(array(
'TableName' => 'reply',
'Item' => array(
'Id' => array( AmazonDynamoDB::TYPE_STRING => 'Amazon DynamoDB#DynamoDB Thread 2' ), // Hash Key
'ReplyDateTime' => array( AmazonDynamoDB::TYPE_STRING => $ten_days_ago ), // Range Key
'Message' => array( AmazonDynamoDB::TYPE_STRING => 'DynamoDB Thread 2 Reply 1 text' ),
'PostedBy' => array( AmazonDynamoDB::TYPE_STRING => 'User A' ),
)
));

// Success?
print_r($add_response);


$seven_days_ago = date('Y-m-d H:i:s', strtotime("-7 days"));

$response = $dynamodb->query(array(
'TableName' => 'reply',
'HashKeyValue' => array( AmazonDynamoDB::TYPE_STRING => 'Amazon DynamoDB#DynamoDB Thread 2' ),
// optional parameters
'AttributesToGet' => array( 'ReplyDateTime', 'Message', 'PostedBy' ),
'ConsistentRead' => true,
'RangeKeyCondition' => array(
'ComparisonOperator' => AmazonDynamoDB::CONDITION_LESS_THAN_OR_EQUAL,
'AttributeValueList' => array(
array( AmazonDynamoDB::TYPE_STRING => $seven_days_ago )
)
)
));

// 200 response indicates Success
print_r($response);

Labels: , ,


 

NoSQL support by AWS

Click on "Create Table" button on "DynamoDB" tab while using AWS web Management Console.

https://console.aws.amazon.com/dynamodb/home

Type table name "testme1" and choose Number as Primary Key Type. Hash attribute name can be "Id". Assuming that you have correctly installed AWS SDK, the following PHP code should add a key - value to the table testme1 table.

require_once 'sdk.class.php';

// Instantiate the class
$dynamodb = new AmazonDynamoDB();

$add_response = $dynamodb->put_item(array(
'TableName' => 'reply',
'Item' => array(
'Id' => array( AmazonDynamoDB::TYPE_STRING => 'Amazon DynamoDB#DynamoDB Thread 2' ), // Hash Key
'ReplyDateTime' => array( AmazonDynamoDB::TYPE_STRING => '2012-01-31 23:28:40' ), // Range Key
'Message' => array( AmazonDynamoDB::TYPE_STRING => 'DynamoDB Thread 2 Reply 1 text' ),
'PostedBy' => array( AmazonDynamoDB::TYPE_STRING => 'User A' ),
)
));

// Success?
print_r($add_response);

_____

get item is obviously similar to put_item as shown above:

$get_response = $dynamodb->get_item(array(
'TableName' => 'reply',
'Key' => array(
'HashKeyElement' => array( AmazonDynamoDB::TYPE_STRING => 'Amazon DynamoDB#DynamoDB Thread 2' )
)
));

_____

In order to drop the table, use the following:

$delete_response = $dynamodb->delete_table(array(
'TableName' => 'reply'
)
);

_____

The following code will create a table called "reply":

$create_response = $dynamodb->create_table(array(
'TableName' => 'reply',
'KeySchema' => array(
'HashKeyElement' => array(
'AttributeName' => 'Id',
'AttributeType' => AmazonDynamoDB::TYPE_STRING
),
'RangeKeyElement' => array(
'AttributeName' => 'ReplyDateTime',
'AttributeType' => AmazonDynamoDB::TYPE_STRING
)
),
'ProvisionedThroughput' => array(
'ReadCapacityUnits' => 10,
'WriteCapacityUnits' => 5
)
));

_____

Here are 3 easy steps you may need to follow if you have not already downloaded and configured Software Development kit from Amazon.

# Download AWS SDK for PHP
svn co http://svn.github.com/amazonwebservices/aws-sdk-for-php.git AWSSDKforPHP
cd AWSSDKforPHP
_____

# copy sample to config file
mv config-sample.inc.php config.inc.php
_____

# add your actual key and secret found on "security credentials" page that can be found here...
# https://aws-portal.amazon.com/gp/aws/securityCredentials

vi config.inc.php

'key' => 'ABCD',
'secret' => 'XYZ',
'default_cache_config' => '/tmp/',

Labels: , ,


Archives

June 2001   July 2001   January 2003   May 2003   September 2003   October 2003   December 2003   January 2004   February 2004   March 2004   April 2004   May 2004   June 2004   July 2004   August 2004   September 2004   October 2004   November 2004   December 2004   January 2005   February 2005   March 2005   April 2005   May 2005   June 2005   July 2005   August 2005   September 2005   October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006   January 2007   February 2007   March 2007   April 2007   June 2007   July 2007   August 2007   September 2007   October 2007   November 2007   December 2007   January 2008   February 2008   March 2008   April 2008   July 2008   August 2008   September 2008   October 2008   November 2008   December 2008   January 2009   February 2009   March 2009   April 2009   May 2009   June 2009   July 2009   August 2009   September 2009   October 2009   November 2009   December 2009   January 2010   February 2010   March 2010   April 2010   May 2010   June 2010   July 2010   August 2010   September 2010   October 2010   November 2010   December 2010   January 2011   February 2011   March 2011   April 2011   May 2011   June 2011   July 2011   August 2011   September 2011   October 2011   November 2011   December 2011   January 2012   February 2012   March 2012   April 2012   May 2012   June 2012   July 2012   August 2012   October 2012   November 2012   December 2012   January 2013   February 2013   March 2013   April 2013   May 2013   June 2013   July 2013   September 2013   October 2013   January 2014   March 2014   April 2014   May 2014   July 2014   August 2014   September 2014   October 2014   November 2014   December 2014   January 2015   February 2015   March 2015   April 2015   May 2015   June 2015   July 2015   August 2015   September 2015   January 2016   February 2016   March 2016   April 2016   May 2016   June 2016   July 2016   August 2016   September 2016   October 2016   November 2016   December 2016   January 2017   February 2017   April 2017   May 2017   June 2017   July 2017   August 2017   September 2017   October 2017   November 2017   December 2017   February 2018   March 2018   April 2018   May 2018   June 2018   July 2018   August 2018   September 2018   October 2018   November 2018   December 2018   January 2019   February 2019   March 2019   April 2019   May 2019   July 2019   August 2019   September 2019   October 2019   November 2019   December 2019   January 2020   February 2020   March 2020   April 2020   May 2020   July 2020   August 2020   September 2020   October 2020   December 2020   January 2021   April 2021   May 2021   July 2021   September 2021   March 2022   October 2022   November 2022   March 2023   April 2023   July 2023   September 2023   October 2023   November 2023   April 2024   May 2024   June 2024   August 2024   September 2024   October 2024   November 2024   December 2024  

This page is powered by Blogger. Isn't yours?