Shantanu's Blog

Corporate Consultant

April 07, 2014


Logical Snapshots

1) In order to use LVM we need to store the mysql data on LVM partition. So the /etc/my.cnf file should point to new data directory and not the default /var/lib/mysql

vi /etc/my.cnf

2) The /data folder should be hosted by lvm managed disk. In order to create partition in LVM format, use the following steps when you add a new disk or when you format the server for the first time.

# make sure the new disk is detected
server1:~# fdisk -l

# change the format of the new disk - use the options shown below when ever prompted
fdisk /dev/xvdj


# check the new name of the LVM disk
fdisk -l

# add it to LVM
pvcreate /dev/xvdj1

# name the LVM
vgcreate lvm /dev/xvdj1

# create logical volume
lvcreate --name=mysql_data   --size=2000M lvm

# format the newly created volume
mkfs.ext3 /dev/lvm/mysql_data
# mount the volume
mkdir /data
mount /dev/lvm/mysql_data /data

# make sure that the data directory is pointing to the /data folder in my.cnf
# restore all the data from backup sql files

3) Create a lvm snapshot when mysql service is stopped for a few minutes:

# check the VG status

# create a snapshot of data directory
lvcreate --name=mysql_snap  --snapshot --size=2100M  /dev/lvm/mysql_data

# mount the snapshot
mkdir /mnt/mysql-snap
mount /dev/mapper/lvm-mysql_data /mnt/mysql-snap/

4) Create snapshots with timestamp and also mount them for easy access.

lvcreate --name=mysql_snap_7apr14  --snapshot --size=2100M  /dev/lvm/mysql_data

mkdir /mnt/mysql-snap_7apr14
mount /dev/mapper/lvm-mysql_snap_7apr14 /mnt/mysql-snap_7apr14/

It is however not possible to take a snapshot of snapshot.

# lvcreate --name=mysql_snap_7apr14b  --snapshot --size=24000M  /dev/lvm/mysql_snap1
  Snapshots of snapshots are not supported yet.

So you can only take a snapshot of the original drive.

# lvcreate --name=mysql_snap_7apr14a  --snapshot --size=21000M  /dev/lvm/mysql_data
  Logical volume "mysql_snap_7apr14a" created

//compress and unzip data

umount /original_data
dd if=/dev/mapper/lvm_new-mysql_original_data | gzip > snap01.gz

lvcreate --name=mysql_data_new_pratik   --size=20000M lvm_new
time gzip -d -c snap01.gz | dd of=/dev/mapper/mysql_data_new_pratik


// change the disk allocated to any volume

# vgchange -an lvm
  Can't deactivate volume group "lvm" with 3 open logical volume(s)

# Unmount any snapshots in order to disable them
umount /mnt/mysql-snap

# lvextend  -L+10G /dev/mapper/lvm-mysql_data
  Extending logical volume mysql_data to 111.56 GiB
  Logical volume mysql_data successfully resized

March 01, 2014


OCR in Google Drive

Google drive supports OCR (Optical Character Recognition)

But you need to enable it from settings. Here is how to enable and use it to search the text found in your image.

January 21, 2014


Did you know google code Downloads are disabled?

When I tried to upload a file to my google code project, I got this message:

Google Code will not support creating new downloads starting January 15th, 2014. See here for more information.

The "here" word links to google blog that google expects us to read often.

I can see that the download option was disabled with sufficient notice. But google should have sent an email about the change. A pop-up while browsing the google code would have avoided the confusion.
I wasted a lot of time figuring out what is going wrong.

After killing Google reader for no apparent reason, this is the next casualty. I do not know who is the next?

Labels: ,

January 13, 2014


Copy a file to s3 using boto

Boto has built-in utility to upload a file to s3

/usr/local/bin/s3put -d 3 -a XXXXXXXXX -s XXXXXXX -b mtld-sql -k latest/  /mnt/data_backup/db/mowser.tar.gz

That should copy the file
From Local: /mtn/data_backup/db/mowser.tar.gz
To Cloud: mtd-sql/latest/mnt/data_backup/db/mowser.tar.gz

Here's an example using the -k and -p options:

python /tmp/s3put -d 3 -a XXXXXXXXX -s XXXXXXX  -b mtld-sql -k latest/  -p /mnt/data_backup/db/ /mnt/data_backup/db/mowser.tar.gz

The -k gives you mtld-sql/latest/
the -p removes /mtn/data_backup/db/ "KEY" prior to copying the to S3

Result copy:
From Local: /mtn/data_backup/db/mowser.tar.gz
To Cloud:  /mtd-sql/latest/mowser.tar.gz 


A typical command would look like this:
 /usr/local/bin/s3put -a xxx -s yyy -b glaciercopy -p `pwd` inter.sql

-p switch will remove the /home/ubuntu subfolder while saving to s3
-d 3 debug level 3 for verbose output

Labels: ,

January 12, 2014


Review Database

Here is how to quickly review the database. We can query the information_schema database to find if the column types are as per internal guidelines.

mysql> select DATA_TYPE, COUNT(*) AS CNT, GROUP_CONCAT(DISTINCT(COLUMN_TYPE)) from information_schema.COLUMNS
where TABLE_SCHEMA = 'recharge_db' GROUP BY DATA_TYPE;

| DATA_TYPE | CNT | GROUP_CONCAT(DISTINCT(COLUMN_TYPE))                                                    |
| bigint    |  15 | bigint(20)                                                                             |
| datetime  |   6 | datetime                                                                               |
| double    |   3 | double                                                                                 |
| int       |  24 | int(11),int(2)                                                                         |
| text      |   2 | text                                                                                   |
| varchar   |  16 | varchar(100),varchar(500),varchar(10),varchar(20),varchar(15),varchar(255),varchar(25) |
6 rows in set (0.01 sec)

The suggestions would be:
1) Change double to decimal
2) Change int(2) to tinyint
3) change text to varchar(1000) if possible

mysql> select IS_NULLABLE, COUNT(*) AS CNT from information_schema.COLUMNS
| NO          |   1 |
| YES         |  52 |
2 rows in set (0.00 sec)

Most of the columns are nullable those should be changed to "NOT NULL".
Certain columns can not be changed to "NOT NULL" and they are candidate for further normalization.

The following query will list all the columns those can be linked to the table that has declared it as Primary Key.

select COLUMN_NAME, COUNT(*) AS CNT, GROUP_CONCAT(IF(COLUMN_KEY = 'PRI', concat(TABLE_NAME, '_primary_key'), TABLE_NAME) order by COLUMN_KEY != 'PRI', TABLE_NAME) as tbl_name
from information_schema.COLUMNS
where TABLE_SCHEMA = 'recharge_db'
group by COLUMN_NAME HAVING CNT > 1 AND tbl_name like '%_primary_key%';

Find missing primary key:

SELECT table_schema, table_name
FROM information_schema.tables
WHERE (table_catalog, table_schema, table_name) NOT IN
(SELECT table_catalog, table_schema, table_name
FROM information_schema.table_constraints
WHERE constraint_type in ('PRIMARY KEY', 'UNIQUE'))
AND table_schema = 'recharge_db';

// Check if the column names are consistent and as per standard

select column_name, count(*) as cnt
from information_schema.columns where TABLE_SCHEMA = 'recharge_db'
GROUP BY column_name;


Data Normalization tips:
1) Data should be normalized
2) There should be no NULL values in any column
3) There should be no need to use "update" statement

Labels: ,


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  

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