24/7/365 Support

Setting MySQL backups in Ubuntu

In this recipe, we will learn how to back up the MySQL database.

Getting ready

You will need administrative access to the MySQL database.

How to do it…

Follow these steps to set up the backups:

Backing up the MySQL database is the same as exporting data from the server. Use the mysqldump tool to back up the MySQL database as follows:

$ mysqldump -h localhost -u admin -p mydb > mydb_backup.sql

You will be prompted for the admin account password. After providing the password, the backup process will take time depending on the size of the database.

To back up all databases, add the --all-databases flag to the preceding command:

$ mysqldump --all-databases -u admin -p alldb_backup.sql

Next, we can restore the backup created with the mysqldump tool with the following command:

$ mysqladmin -u admin -p create mydb

$ mysql -h localhost -u admin -p mydb < mydb_backup.sql

To restore all databases, skip the database creation part:

$ mysql -h localhost -u admin -p < alldb_backup.sql

How it works…

MySQL provides a very general tool, mysqldump, to export all data from the database server. This tool can be used with any type of database engine, be it MyISAM or InnoDB or any other. To perform an online backup of InnoDB tables, mysqldump provides the --single-transaction option. With this option set, InnoDB tables will not be locked and will be available to other applications while backup is in progress.

Oracle provides the MySQL Enterprise backup tool for MySQL Enterprise edition users. This tool includes features such as incremental and compressed backups. Alternatively, Percona provides an open source utility known as Xtrabackup. It provides incremental and compressed backups and many more features.

Some other backup methods include copying MySQL table files and the mysqlhotcopy script for InnoDB tables. For these methods to work, you may need to pause or stop the MySQL server before backup.

You can also enable replication to mirror all data to the other server. It is a mechanism to maintain multiple copies of data by automatically copying data from one system to another. In this case, the primary server is called Master and the secondary server is called Slave. This type of configuration is known as Master-Slave replication. Generally, applications communicate with the Master server for all read and write requests. The Slave is used as a backup if the Master goes down. Many times, the Master-Slave configuration is used to load balance database queries by routing all read requests to the Slave server and write requests to the Master server. Replication can also be configured in Master-Master mode, where both servers receive read-write requests from clients.

See also

MySQL backup methods at http://dev.mysql.com/doc/refman/5.6/en/backup-methods.html

Percona XtraBackup at https://www.percona.com/doc/percona- xtrabackup/2.2/index.html

MySQL binary log at http://dev.mysql.com/doc/refman/5.6/en/binary-log.html

Help Category:

What Our Clients Say