Wednesday 31 December 2014

Taking Backup of MySQL database

MySQL is a very popular and widely used open source database. Most of the software engineers might have used mysql database atleast once. Keeping a backup of a database or migrating the tables/databases from one database server to another is a very important task. In mysql this task can be performed in a very simple way.
MySQL is providing a commandline utility to perform this operation. The command is mysqldump.
For getting more details about mysqldump, type the following command
>mysqldump --help

For taking the backup of a complete database, type the following command.
>mysqldump --databases [DB1 Name] ....[DBn Name] -u <username> -h <hostname> -p

This will print the entire database dump to the console. For storing the output of this in a file, do the following operation.
 >mysqldump --databases [DB1 Name] ....[DBn Name] -u <username> -h <hostname> -p >> dump.sql

Here the contents will be written to a file locally.

If you want to take the back up of all the contents in a mysql server, we can back up all databases.
The command to perform this operation is given below.
>mysqldump --all-databases -u <username> -h <hostname> -p >> dump.sql

In the above commands, <username> is the mysql username and <hostname> is the hostname or ip address of the machine where the mysql server is running.

Now your database is backed up. The next step is how to load this dump file to another mysql instance.

This is a very simple task.
  • Copy the dump file to the machine where you are planning to perform the following operations
  • Login to mysql instance with valid credentials
  • In the mysql prompt, type the following command.
mysql> source <path to the dump file>

This will load the contents inside the dump file to the new mysql instance.
If you want to know more about the mysql dump file, open the file in a text file editor and read the contents. It contents the DDLs and DMLs that recreates your databases and tables.

Changing the Hostname of Redhat and CentOS machines

Sometimes, we need to change the hostname of machines. Hostname is just like our name which is a name assigned to machines. For changing the hostname of a machine, we need root access.
We can change the hostname through several ways. One of the method to change the hostname is listed below.

In linux OS, almost every configuration are present inside some files.
The hostname is configured in the following file

If we change the value in this file with some other name, the hostname will be changed.

So for changing hostname of a machine, edit the /proc/sys/kernel/hostname file and add the desired hostname.

Linux commands to get IP address and Hostname of a machine

Knowing the IP address and Host name of a machine are some very common requirements.

The command to get the IP address of a linux machine is
eth0      Link encap:Ethernet  HWaddr 0A:90:74:4A:62:42
          inet addr:  Bcast:  Mask:
          inet6 addr: fe80::890:74ff:fe4a:6242/64 Scope:Link
          RX packets:13139146 errors:0 dropped:0 overruns:0 frame:0
          TX packets:12665241 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:10433927585 (9.7 GiB)  TX bytes:16516355937 (15.3 GiB)

lo        Link encap:Local Loopback
          inet addr:  Mask:
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:40162017 errors:0 dropped:0 overruns:0 frame:0
          TX packets:40162017 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0

          RX bytes:6184024837 (5.7 GiB)  TX bytes:6184024837 (5.7 GiB)

The command to get the hostname of a linux machine is

Python code to find the md5 checksum of a file

Checksum calculation is an unavoidable and very important step in places where we transfer files/data. The simplest way to ensure whether a file reached the destination properly or not is by comparing the checksum of source and target files. Checksum can be calculated in several ways. One is by calculating the checksum by keeping the entire file as a single block. Another way is multipart checksum calculation, where we calculate the checksum of multiple small chunks in the file and finally calculating the aggregated checksum.
Here I am explaining about the calculation of checksum of a file using the simplest way. I am using the hashlib library in python for calculating the checksum.
Suppose I have a zip file located in the location /home/coder/ The checksum of the file can be calculated as follows.

import hashlib
file_name = ‘/home/coder/’
checksum = hashlib.md5(open(file_name).read()).hexdigest()
print checksum

One common mistake I have seen among people is passing the file name directly without opening the file
Eg: hashlib.md5(file_name).hexdigest()

This will also return a checksum. But it will be calculating the checksum of the file name, not the checksum calculated based on the contents of the file.  So always use the checksum calculation as follows


This will return the exact checksum. 

In linux, you can calculate the md5sum using a commandline utility also.
> md5sum file_name

Python Code Snippet to get the hostname and IP address of a machine

The following python code snippet will return the IP address of a machine

import socket
ip_address = socket.gethostbyname(socket.gethostname())
print ip_address
host_name = socket.getfqdn()
print host_name

The IP address may not be proper in all the cases. If the /etc/hosts file of the unix machine contains entries with hostname mapped to, will return as IP address. So for ensuring proper working, it will be better to use the fully qualified hostname method. 

How to check the memory utilization of cluster nodes in a Kubernetes Cluster ?

 The memory and CPU utilization of a Kubernetes cluster can be checked by using the following command. kubectl top nodes The above command...