Backup
Recovery
SQL

Data Backup and Recovery: Implement the Golden Rule in Your Database

intro

In this blog, we dig into the so-called “golden backup rule” for databases. Implement the perfect data backup and recovery strategy. Join in!

Tools used in the tutorial
Tool Description Link

Backing up data is of crucial importance to everyone as it’s an essential step to adhere to many regulations as well as a part of various solutions to privacy and security problems. If your aim is to understand how, why and when you should back up data, you came to the right place.

Why Back Up Data?

Data backup and recovery is an essential step of your software engineering journey. No matter your reason for storing data (your application probably stores data because of the way it’s used by your users, visitors, or customers, but there can be other reasons), data has to be backed up.

Your backups have to be tested and verified that they work because if they cannot be restored, once you find yourself in hot water, you may be in even deeper trouble than it seems.

When and How to Back Up Data?

Once you understand that you must back up data to not cause headaches either for yourself or those around you, understand that the way you back up data has a direct impact on many things. The approach you follow for data backup has an impact on:

  1. How much space on the disk the backups are going to occupy
  2. How much time completing the backup will necessitate
  3. What data will be backed up and how long will it take to restore that data

Choosing a way to back up data in the first place is of key importance. Understand that backups can be either logical (we can back up statements that recreate the data) or physical (we can backup files that, when restored, will restore the data in the database), and also the phases of the backups can differ quite significantly: we can have incremental backups, full backups, or differential backups.

The ways we go about implementing and running the backups in those phases can also vary quite significantly depending on our data backup and recovery strategy.

So, back to the question — when and how best to backup data?

The Beginning of the Golden Backup Rule — Plan

Plans are the backbone of everything. The same goes for backups — the more well-executed your backup plan is, the better for everyone around you. You need a reliable data backup and recovery strategy — and the golden backup rule is a great place to get started.

The golden backup rule refers to the fact that you should always have at least three (yes, 3) copies of backups of your data: one copy should act as the primary copy, and the others should act as backups of the copy of the primary copy of the data. The reasons behind that are as follows:

  • In your data backup and recovery strategy, a primary copy of your data is necessary so that you possess a copy of your data to begin with.
  • A second copy of the data is necessary so that if restoring the primary copy fails for any reason whatsoever, you have a second (backup) copy of the primary data set.
  • A third copy of the data should be stored in a remote, off-site location so that if you lose the primary backup of your data and for any reasons are not able to restore the second copy, you have something to resort to. That’s a common practice in disaster recovery.

Of course, it goes without saying that backups should be tested. In other words, make sure that the restoration process of all of the backups in question is completed successfully before calling them reliable.

Implementing the Golden Backup Rule

That’s all cool and roses, but how do you actually implement the golden backup rule? Worry not — just follow the steps outlined below:

  1. Identify the type of your database management system. Although one can take backups similarly across many database management systems, there are certain differences you need to adhere to before starting. Those differences will most likely pertain to the database management system you find yourself using.
  2. Decide on the type of backup you’re going to take. As mentioned before, logical backups back up SQL statements that recreate data while physical backups have to do with files that are then read by your database. Also decide whether you’d need a partial or a full backup (partial backups, as the name suggests, only back up a part of the entire data set and full backups either back up all of the data in your database or tables as necessary.)
  3. Decide on your backup frequency. Backups should be taken frequently, but just how frequently depends on your specific use case. Consider it — for some, taking backups once a month may cut to the chase, others may want weekly or even daily backups.
  4. Test your backups. In other words, once you back data up, see if it can be restored without any hassle or issues.
  5. Automate backups. Backing up data is cool and all, but if you need backups to be completed frequently (and you probably do, otherwise, why even bother?), you must automate them, otherwise the entire concept goes out the window. The good part is that it isn’t super hard to do!

How to Implement the Golden Rule for Data Backup and Recovery in MySQL

If you’re a user of MySQL, the golden backup rule may be as simple as:

  1. Identifying areas of your database infrastructure that need to be backed up, then backing them up by using mysqldump or mydumper .
  2. Letting mysqldump or mydumper run automatically at a scheduled time.
  3. Removing the last X amount of backups after a certain amount of time once they’re no longer necessary.

No need to sweat — just set up a simple Bash script like so and you will be good to go:

Copy
        
1 backupfolder=/tmp/backups # backup directory 2 [backup_email=your@email.com](mailto:backup_email=your@email.com) # email to notify once backup is completed 3 user=mysql_username 4 password=complex_passwordhere # MySQL details 5 keep_days=7 # the number of days to keep the backup 6 sqlfile=$backupfolder/all-databases-$(date +%d-%m-%Y_%H-%M-%S).sql # setting the name of the backup 7 sudo mysqldump -u $user -p$password --all-databases > $sqlfile # executing the backup 8 mailx -s 'Backup was successfully created' $recipient_email # mailing 9 find $backupfolder -mtime +$keep_days -delete # deleting unnecessary backups

The contents of the bash script in question may differ depending on your specific use case, but you get the idea. For more information, refer to our guide on MySQL backup and recovery best practices.

Summary

Implementing a proper data backup and recovery plan for your use case isn’t at all difficult if you know your way around databases and have at least basic knowledge of Bash scripting. Remember key concepts — have at least three copies of your data (one local, a backup of the local copy, and at least one copy off-site), automate backups when necessary, and keep a cool head on your shoulders.

FAQ

What is a good data backup and recovery strategy?

A concrete answer to that question would be “it depends.” It really depends because backups are influenced by many factors such as the strain on your database when you’re taking the backup, the size of your data, how often you take backups in the first place, etc.

Automate the way you take backups, use built-in tools that help you back up data instead of relying on ghetto solutions, and you will be good to go.

Where can I learn more about backing up data?

To learn more about backups, indexing, performance, security, and databases in general make sure to visit our blog to keep updated on the latest trends. Specifically, refer to:

Alternatively, consider reading books on the subject to help you stay on track. The book Hacking MySQL: Breaking, Optimizing, and Securing MySQL for Your Use Case is a good place to start.

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

A Complete Guide to an SQL Recursive Query Expression

author Lukas Vileikis tags MySQL SQL 5 min 2025-04-16
title

A Guide to the SQL Standard Deviation Functions

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-04-15
title

SQL EXISTS: Syntax and Use Cases with Examples

author Leslie S. Gyamfi tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-04-14
title

Welcoming Tables with Foreign Keys Into Your Database

author Lukas Vileikis tags MySQL SQL 5 min 2025-04-10
title

Dealing With NULL in SQL: Complete Guide

author Leslie S. Gyamfi tags SQL 7 min 2025-04-09
title

How to Delete Table Data in SQL: A Complete Guide

author Lukas Vileikis tags SQL 5 min 2025-04-03
title

SQL Query Optimization: Everything You Need to Know

author Leslie S. Gyamfi tags OPTIMIZATION SQL 9 min 2025-04-02
title

A Complete Guide to the Different Types of DBMS

author Antonello Zanini tags DBMS NOSQL SQL 8 min 2025-03-31
title

MySQL Indexing Mistakes 101

author Lukas Vileikis tags MySQL Search SQL 10 min 2025-03-27
title

SQL TRUNCATE TABLE: A Complete Guide

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-03-26

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.