How to Backup MySQL Databases in Rescue mode.

Nothing is certain and permanent in this world and so is true for the technology. Every single object has a lifetime and it is vulnerable to fail several times while it exists. No matter how strongly built server you have, the failures will knock your door. Today we will focus the uncertain part of the online world, and we will discuss the database recovery method during a server crash.

How To Backup MySQL Databases in Rescue mode.The importance of database in business

The online databases are to store the important information in the form of bits. The information is always vital to the businesses. The data stored in the databases are further used to sort, filter and display information to run the business in appropriate manners.

What if the data disk fails, what if the hard disk is about to die. Simple, you can go out of the business if there is no recovery or backups of your important business data. Data loss is no less than the death of the business.

What is Mysql

Mysql is widely used database solution for the businesses. It is an open source relational database management system that uses SQL (structured query language) architecture. Mysql is well-known for its stability, speed and flexibility.

Rescue mode

In normal operations the operating system loads from the hard disk residing in the serve. However, if something goes wrong servers can not load the OS properly . They may report the error or completely go broke. At times if you fail to fix the problem, it may further harm your data.

Rescue mode is to built fix the technology problems in the server machines. The OS can be loaded from a tiny disk, CD or with the minimal OS environment. The rescue mode is just like the operating theater to fix the complex problems. Let’s say you are getting the disk failure warnings and it can die anytime. Rescue mode is the safest option to backup your data and replace the faulty disk.

How to boot into the Rescue mode

There are two common methods to boot into rescue mode:

1- Cd Rom

2- USB Flash

attach the cd rom or flash device to the server having the same OS image on it. Boot the server from cd rom or usb flash and not the hard disk. You can setup the first boot device in the bios of the server or you can select at the start of the server by pressing the function keys (F1, F8) depending upon the model of your server machine.

After booting the machine you will land to the command prompt.
To go into the rescue mode write the below command on the prompt:
Linux rescue now the system will ask you a few basic questions to prepare the environment for you. The simple questions would be like what language you want to use, where the OS image located (CD, USB, nfs, https, FTP) and which keyboard to load. Now the rescue mode will find the installation partition on your server to mount it under the directory /mnt/sysimage.

The rescue mode further asks you to load your installation disk as read-only version or write version so that you can make the necessary changes to fix the problem.

Mount the File System

Here you go, you have landed to the rescue mode. Now to reach our hard disk partition we will write the below command at the prompt.

sh-2.05b# chroot /mnt/sysimage

now you can access the disk partitions for the necessary changes. Now we will list the directory structure using the “ls” command.

# ls

we can backup our important files , fix the file system, change the configurations to fix the root cause of the server crash. Since our focus is the database backup and recovery method ,we will now discuss the mysql database backup and recovery strategy.

Backup mysql in rescue mode

The system is in rescue mode with mysql service turned off. We need to take the file system backup by locating the mysql data directory and archive the mysql files.

Finding the data directory

it is important to find the location of the database files. You can find the path by looking into the mysql configuration file named as my.cnf. The my.cnf file is usually located in ” /etc” directory you can find theconf file by using the below command.

locate my.cnf

Execute the below command to check the location of the mysql data directory within the my.cnf file.

grep datadir etc/mysql/my.cnf (assuming that my.cnf is in the etc/mysql/ directory)

usually the directory path is:

/var/lib/mysql

Make the files archive

Now we will execute the below command to make the mysql directory archive with database files in it.

tar czvf mysql-file-backup.tar.gz var/lib/mysql

explanation of the command:

tar – is the command line archive tool

c – create

v – display the results : Verbose

z – pack the files at .gz

f – archive file name

it will take some time depending upon the size of your database to archive the data.

At the end you will get the single backup file as mysql-file-backup.tar.gz in the current working directory.

The file backup of your database is complete, you can restore it to the remote server or the same server unzipping the archive.

You can unzip the gz archive with below command.

tar -zxvf mysql-file-backup.tar.gz

You can back up the other directories and important files using the same method.

Rounak Jain

Leave a Reply