BACKUP AND RECOVERY IN MySQL (Article -17).

Share via:

BACKUP AND RECOVERY IN MySQL.

What is backup ?

Backup is nothing copying the data into another location .

Why we need backup ?

If you’re storing anything in MySQL databases that you do not want to lose, it is very important to make regular backups of your data to protect it from loss. This tutorial will show you two easy ways to backup and restore the data in your MySQL database. You can also use this process to move your data to a new web server.

In MySQL we have two types of backup’s. They are

1.Logical Backup’s

2.Physical backup(Raws)

 

LOGICAL BACKUPS :

Logical backups save information represented as logical database structure (CREATE DATABASE, CREATE TABLE statements) and content (INSERT statements or delimited-text files). This type of backup is suitable for smaller amounts of data where you might edit the data values or table structure, or recreate the data on a different machine architecture.

 

  • Logical backup methods have these characteristics:
  • The backup is done by querying the MySQL server to obtain database structure and content information.
  • Backup is slower than physical methods because the server must access database information and convert it to logical format. If the output is written on the client side, the server must also send it to the backup program.
  • Output is larger than for physical backup, particularly when saved in text format.
  • Backup and restore granularity is available at the server level (all databases), database level (all tables in a particular database), or table level. This is true regardless of storage engine.
  • The backup does not include log or configuration files, or other database-related files that are not part of databases.
  • Backups stored in logical format are machine independent and highly portable.
  • Logical backups are performed with the MySQL server running. The server is not taken offline.
  • Logical backup tools include the mysqldump program and the SELECT … INTO OUTFILE statement. These work for any storage engine, even MEMORY.
  • To restore logical backups, SQL-format dump files can be processed using the mysql client. To load delimited-text files, use the LOAD DATA INFILE statement or the mysqlimport client.

 

PHYSICAL BACKUPS :

  • Physical backups consist of raw copies of the directories and files that store database contents. This type of backup is suitable for large, important databases that need to be recovered quickly when problems occur.
  • The backup consists of exact copies of database directories and files. Typically this is a copy of all or part of the MySQL data directory.
  • Physical backup methods are faster than logical because they involve only file copying without conversion.
  • Output is more compact than for logical backup.
  • Because backup speed and compactness are important for busy, important databases, the MySQL Enterprise Backup product performs physical backups. For an overview of the MySQL
  • Backup and restore granularity ranges from the level of the entire data directory down to the level of individual files. This may or may not provide for table-level granularity, depending on storage engine. For example, InnoDB tables can each be in a separate file, or share file storage with other InnoDB tables; each MyISAM table corresponds uniquely to a set of files.
  • In addition to databases, the backup can include any related files such as log or configuration files.
  • Data from MEMORY tables is tricky to back up this way because their contents are not stored on disk. (The MySQL Enterprise Backup product has a feature where you can retrieve data from MEMORY tables during a backup.)
  • Backups are portable only to other machines that have identical or similar hardware characteristics.
  • Backups can be performed while the MySQL server is not running. If the server is running, it is necessary to perform appropriate locking so that the server does not change database contents during the backup. MySQL Enterprise Backup does this locking automatically for tables that require it.
  • Physical backup tools include the mysqlbackup of MySQL Enterprise Backup for InnoDB or any other tables, or file system-level commands (such as cp,scp, tar, rsync) for MyISAM tables.

 

In this article will cover Logical Backups.

In Logical backups we have two ways to export the data in MySQL.

1.mysqldump

2.SELECT … INTO OUTFILE

 

1.mysqldump

The mysqldump program is used to copy or back up tables and databases. It can write the table output either as a Raw Datafile or as a set of INSERTstatements that recreate the records in the table.

General syntax of mysqldump :

To take remote connections using mysqldump

Syntax :

To check what options available in mysqldump.

Let’s check the databases we have

Check objects available in trg database .

Create a directory to store backup.

Now will take backup using mysqldump

mysqldump stores sql statements.

Now the backup has completed.Whenever we take backup using mysqldump it takes sql statements in alphabetical order.

Lets read the content under backup file.

 

For better understanding we can grep what table structure available in dumpfile.

If we want to take multiple databases we use -databases option.

Lets read the content inside the backup file.

Whenever we are taking backup of multiple databases using mysqldump it takes in alphabetical order.

Once the first database completed the only it will start second database backup.

If want to know whether backup was successfully completed or not  we should check the last line in backup file .

If it shows DUMP COMPLETED ON DATE AND TIME  then only your backup successfully completed.

If it doesn’t shows not completed .

Your mysqldump can failure due to network issues.

If we want to take all the databases backup we use -all option.

How to take single table backup ?

we use tablename followed by databasename .

How to take only structure  of a database ?

We have an option called –no-data.

In the backup file we can’t  find insert statement.

Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates

KTEXPERTS is always active on below social media platforms.

Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform

 

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)
Loading...

Add Comment