BIG DATA
MySQL
OPTIMIZATION
SECURITY
SQL

MySQL Backup and Recovery Best Practices: A Guide

intro

Every developer working with databases has taken a MySQL backup. In this guide, we’re walking you through a couple of best practices related to backup and recovery of this RDBMS.

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT
mariadb MARIADB
THE MARIADB DATABASE
MySQL logo MySQL
THE MYSQL DATABASE

Are you a database guru? If so, you’ve probably heard of DB-Engines. DB-Engines is a portal ranking database management systems by their popularity, and MySQL is ranked second. As such, MySQL backup procedures are of crucial importance to all DBAs and software engineers.

There are numerous backup appliances and strategies in MySQL you can follow. The strategy chosen has a direct impact on the future of both your application and your database. Let’s explore them all!

How to Backup MySQL?

For many, backing up MySQL is an easy task. What is there to think about? Just open phpMyAdmin, select the necessary options, and click the “Export” button. Here you go—you’ve downloaded a MySQL backup file. Easy, right?

Exporting databases from phpMyAdmin
Exporting databases from phpMyAdmin

That’s our exported file:

The exported MySQL backup
The exported MySQL backup

So many options, right? We know:

  • The server the backup happened on (in this case, 127.0.0.1).
  • The time the backup happened (Generation Time).
  • The server version (in this case, we’re running MariaDB 10.10.2. Attentive readers will know that MariaDB is not MySQL—it’s a fork of it—but the process is the same in MariaDB too).
  • The PHP version of our server (8.0.26).
  • The time zone our server is using.
  • The character set and collation for every database in the server.

And we didn’t even have to scroll! Awesome. Frightening.

Judging from the screenshots above, you’d think that issuing a MySQL backup is very easy. We have bad news—what you saw is a logical backup taken by one of the tools that can be used. There are multiple types of backups in MySQL, and consequentially, many tools to help you take a backup.

MySQL Backup Methods

We’re starting from methods to help you take a MySQL backup. In the MySQL world, backup methods are as follows:

Backup MethodAbout
Logical BackupsThese MySQL backups are backups that backup statements (SQL queries) recreating the data in our database.
Physical BackupsThis backup method involves backing up physical files related to MySQL. Such a method is usually faster to restore, but usually much more costly in terms of disk space.
Online and Offline—”Hot” and “Cold”—BackupsOnline—or “hot”—backups are performed once the server that is “receiving” the backup is online and accessible, frequently used, by users.
Offline—or “cold”—backups are performed when the server is down and inaccessible to users.
Full BackupsFull backups mean “comprehensive backups of everything a MySQL instance contains.” They include everything in the entire MySQL Server instance and can sometimes be very time-consuming and tedious to maintain.
Incremental BackupsIncremental backups record changes between two points in time. Incremental backups are recovered using point-in-time recovery methods.
SnapshotsA snapshot backup is a logical backup of the files in the file system at any given point in time. These kinds of backup methods don’t require a physical copy of the files existing on the database server.

See how many options we have to choose from? Our next steps are as follows:

  1. We choose an option—say, logical backups.
  2. We choose an appropriate tool for that option (e.g. mysqldump, Percona XtraBackup, etc.).
  3. We use the tool to logically (incrementally, physically, etc.) back up our database.

Before you ask—no, backup methods are not only unique to MySQL Server or its counterparts like MariaDB or Percona Server. These backup methods are applicable to various kinds of database management systems from PostgreSQL and TimescaleDB to MongoDB.

These kinds of backup methods are like “golden rules” of database management systems, but for them to work in a golden—secure and fast—manner, we need to follow a couple of best practices, too.

MySQL Backup Best Practices

Before embarking on your MySQL backup journey, decide what tool are you going to use to back up your MySQL instances:

Backup ApplianceAbout
mysqldumpA CLI-based tool used to back up databases or tables within them for a transfer into another database server. Only takes logical backups.
Percona XtraBackupA fully-fledged backup appliance built by Percona. This backup appliance can backup data from InnoDB, XtraDB, MyISAM, and MyRocks-based tables. Only performs hot—online—backups.
MySQL Enterprise BackupAn enterprise-grade tool providing enterprise-grade backup and recovery for MySQL Server-based databases. Comes with ways to take all kinds of backups—full, incremental, partial, comes with encryption, backup validation, and compression capabilities, and much more. Learn more about MySQL Enterprise Backup on its official webpage.

We’ve listed the top three choices for MySQL engineers across the globe—now we’ll walk you through each of those tools and provide you with some information on what SQL clients like the one provided by DbVisualizer are capable of, too.

Follow the information outlined below:

Backup ApplianceBest Practices
mysqldumpAn ordinary use case of mysqldump looks like so:

mysqldump [options] > file.sql Options include, but are not limited to (all options are preceded by “--”): • all-databases that dumps all tables in all databases. • add-locks ”wraps” each dumped table around LOCK TABLES and UNLOCK TABLES SQL statements that prevent actions on that specific database. • allow-keywords allows the creation of tables that are keywords (that’s usually prohibited.) • default-character-set lets us specify a default character set for the database dump. All options are preceded by “--”. The full list of options can be found here. • To restore such a backup, either re-import it through phpMyAdmin, or back it up via the CLI: mysql -u[user] database_name < backup.sql | | Percona XtraBackup | • To create a backup, run Percona XtraBackup after specifying the --backup option like so: xtrabackup --backup --target-dir=/backups/ where --backup invokes Percona XtraBackup and --target-dir specifies the directory which will “receive” (store) the backup. • Once backups are made, they need to be prepared to be able to be restored. • To prepare a backup, run the following statement: xtrabackup --prepare --target-dir=/backups/ after replacing target-dir with your backup directory. • Finally, to restore a backup, run --copy-back : xtrabackup --copy-back --target-dir=/backups/ Learn more in Percona’s documentation of XtraBackup. | | MySQL Enterprise Backup | • Create a backup like so (we don’t specify a password here for MySQL Enterprise Backup to grab it from my.cnf instead): mysqlbackup --user=dbvisualizer --password --port=3306 [options] [backup|backup-to-image] • Available options include backup-to-image (creates an “image” of a backup), copy-back (restores — “copies back” — a backup), copy-back-and-apply-log (the same as copy-back just with updates to log files), and validate (validates a backup.) • Aside from the available options, we also have abilities to take incremental backups, exclude certain tables from being backed up, encrypt our backups, and more. • To take a full backup, specify a --backup-image and a --backup-dir. Tell the Enterprise Backup solution to --backup-to-image too. `• To take an incremental backup, specify a--defaults-file(location towards my.cnf), the--incremental-start-lsn(Log Sequence Number), add--with-timestamp, and provide a--incremental-backup-dir`. • Other ways to take backups using MySQL Enterprise Backup are available in the documentation. |

Aside from best practices within MySQL backup and recovery tools, you must glance at SQL clients, too. Most of them aren’t exclusive to MySQL and provide support for various database management systems such as PostgreSQL, MsSQL, and SQLite, as well as database appliances like ClickHouse, Cassandra, and others.

DbVisualizer is a top-notch SQL client that also offers a free 21-day trial so you can try their various features for yourself. DbVisualizer provides you with the ability to enhance your workflow, a nice overview of your data and everything related to your database, whatever database you elect to use (hence the name), and it is also built to withstand security breaches.

Exploring columns within a MySQL 8 table in DbVisualizer
Exploring columns within a MySQL 8 table in DbVisualizer
Exploring a table in DbVisualizer
Exploring a table in DbVisualizer

Select a table, and DbVisualizer will also provide you with the DDL necessary to recreate the table structure on any database infrastructure, too:

Exploring a table in DbVisualizer
Exploring a table in DbVisualizer

DbVisualizer is also known for its ability to extensively document the references of your tables:

Exploring references in DbVisualizer
Exploring references in DbVisualizer

Grab a free trial of DbVisualizer, set up the SQL commander permissions, then have fun with your database. Make sure to visit our blog to learn more about database management systems and SQL clients!

SQL commander permissions in DbVisualizer
SQL commander permissions in DbVisualizer

Summary

The MySQL backup process isn’t a piece of cake, but it isn’t exactly rocket science either. We hope that this blog post has opened your eyes to what’s possible with the backup procedures in MySQL, and until next time!

FAQ

What tool should I use to backup MySQL?

It all depends on what kind of backups you want to take. The easiest option for logical backups would be mysqldump, but you can always use Percona XtraBackup or MySQL Enterprise Backup.

Why are there so many backup methods in MySQL?

It makes sense to have many MySQL backup methods because no two infrastructures are exactly the same. Plus, different backup methods back up different things inside of your database. So, having different backup methods at hand helps those dealing with bigger data sets or in other tricky situations.

What MySQL flavor should I use when performing a backup in MySQL?

It doesn’t matter much if you use MySQL Server, Percona Server, or MariaDB Server as the backup MySQL process is pretty much the same for all of them. However, bear in mind that Percona Server and MariaDB do offer features MySQL does not, such as XtraDB (unique to Percona Server) or other storage engines.

Dbvis download link img
About the author
LukasVileikisPhoto
Lukas Vileikis
Lukas Vileikis is an ethical hacker and a frequent conference speaker. He runs one of the biggest & fastest data breach search engines in the world - BreachDirectory.com, frequently speaks at conferences and blogs in multiple places including his blog over at lukasvileikis.com.
The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

SQL Upsert: Inserting a Record If It Does Not Exist

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2024-12-10
title

A Complete Guide to the MySQL COUNT Function

author Antonello Zanini tags MySQL 9 min 2024-12-09
title

MySQL Nullable Columns: Everything You Need to Know

author Antonello Zanini tags MySQL 6 min 2024-12-03
title

A Complete Guide to the MySQL GROUP_CONCAT Function

author Antonello Zanini tags Data aggregation MySQL 8 min 2024-12-02
title

How and Why to Add an Index in SQL

author Lukas Vileikis tags MySQL POSTGRESQL Search SQL SQL SERVER 8 min 2024-11-28
title

MySQL 9.0 and the New EXPLAIN Features

author Lukas Vileikis tags MySQL SQL 9 min 2024-11-27
title

Massive SQL DELETE Operations and Database Purging Explained

author Lukas Vileikis tags DELETE SQL 4 min 2024-11-26
title

MySQL CAST: A Guide to the Data Conversion Function

author Antonello Zanini tags MySQL 3 min 2024-11-25
title

Postgres Create Array of Numbers: Complete Guide

author Lukas Vileikis tags POSTGRESQL SQL 7 min 2024-11-21
title

List of SQL Reserved Words in All Major Databases

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 16 min 2024-11-20

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.

Cookie policy

We use cookies to ensure that we give you the best experience on our website. However you can change your cookie settings at any time in your browser settings. Please find our cookie policy here ↗