Simple guide to database backups: MySQL, PostgreSQL and SQL Server

A database backup is a special copy of data that can be used to restore the database to a specific point in time. While SQL databases are quite similar in terms of their language, the tools for backup differ for different types of DBMS (Database Management Systems). In this article, we will look at how to create backups for three popular DBMS: MySQL, PostgreSQL and MSSQL Server.

Why are backups necessary?

First and foremost, backups are essential for data recovery in the event of a failure. If your database is damaged or corrupted for some reason, you can use the backup to restore it to a normal state.

An alternative use for backups is data migration. With a backup, you can create new databases that are an exact copy of the original.

How to create backups and restore them

Both backup and restore operations are performed using the tools provided by the DBMS itself, and these tools differ for each DBMS.

SQL Server

The primary way to create backups for SQL Server is by executing the following SQL command, which can be run directly in SQL Server Management Studio:

BACKUP DATABASE [database_name]
TO DISK = 'C:\Backup\backup_file.bak'
WITH INIT

To restore a SQL Server database, use a different SQL command:

RESTORE DATABASE [database_name]
FROM DISK = 'C:\Backup\backup_file.bak'
WITH RECOVERY

There are also other ways to create SQL Server backups, such as using PowerShell modules or exporting the database. For more details:

Complete guide to SQL Server backup and restore using the command line

How to backup SQL Server databases on Windows

MySQL

The classic tool for backing up a MySQL database is the mysqldump utility. This utility is installed alongside the MySQL server or MySQL client. It creates a logical backup of the database in the form of an SQL script:

mysqldump --user root --password --all-databases > all-databases.sql

To restore a backup, the mysql utility is used. You simply execute the SQL script with the following command:

mysql --user root --password mysql < all-databases.sql

There are also other methods for backing up a MySQL database, such as using the xtrabackup, mysqlbackup, and mysqlsh utilities. For more details:

How to backup MySQL database on Windows

MySQL command line backup: Top 5 CLI utilities

PostgreSQL

For PostgreSQL backups, the pg_dump utility is used, which also creates a logical backup in the form of an SQL script.

su - postgres

pg_dump -U postgres db_name | gzip > backup_file.gz

This command creates a backup and compresses it immediately.

You can restore the backup using the psql utility like this:

gunzip -c /backups/postgresql/db_name.gz | psql -U postgres db_name

More details on PostgreSQL backups

General backup tips

Where to store backups

It’s a good practice to keep at least two copies of your backups — one in a local environment and another in cloud storage. The more copies you have, the better, though this typically requires more time and financial resources.

Encrypt your backups

Since a backup contains the entire database, it becomes a prime target for hackers who might want to steal your data. Encryption can protect against theft. You can encrypt your backups using an archiver, built-in database management system (DBMS) tools, or third-party products like SqlBak and SqlBackupAndFTP, which offer this option.

SQL Server backup encryption: why and how?

Automate your backups

To protect your data, backups should be performed regularly, so it’s essential to automate the process. There are many ways to automate database backups, from writing a simple shell or BAT script to using third-party software. Here are some articles on automating backups:

How to automate PostgreSQL database backups in Linux

How to automate MySQL database backups in Windows

Automating SQL Server backups on Windows: Why you need it and how to do it

Testing

The purpose of backups is to facilitate restoration. After setting up an automated backup process, try performing a test restore, for example, on a test server, or simply restore the database under a different name. Thoroughly document the steps required to restore the backup and keep this guide alongside your backups.

Why can’t you just copy database files?

When you perform an INSERT operation containing several fields, it may seem like the operation is atomic, and it is — at the logical database level. However, physically, the data is written to the disk in several low-level operations, and if you copy the database files, you might end up with an incomplete (corrupted) row in the table. And even this is an over-simplification.

In reality, databases are complex, with indexes, metadata, and transaction logs constantly being updated in the database files. Simply copying and pasting the files back can lead to unpredictable consequences and database errors. Most likely, the database will fail to start.

Third-party backup tools

If you don’t want to bother with manual backup creation and restoration, you can use third-party backup tools. Some offer automatic backup creation, data encryption, cloud storage, and other useful features. For example:

  1. SqlBackupAndFTP — A Windows application installed on the server, allowing you to automate backups for SQL Server, MySQL and PostgreSQL.
  2. SqlBak — Provides a dashboard for monitoring and managing SQL Server, MySQL and PostgreSQL backups.

In conclusion

Remember, backups are an essential part of data security, so it’s important to give this process the attention it deserves. Automate your backup creation process, store them in secure locations, and regularly check their integrity.

Leave a Reply

Your email address will not be published. Required fields are marked *