Back Up and Restore MySQL Databases with MySQLdump

Tram Ho

In this post I will explain about backing up and restoring mysql from the command line using mysqldump. You can use the mysqldump utility to transfer your MySQL database to another MySQL server. If you don’t back up your database, if that server dies, it’s a disaster. To save you a lot of time, I recommend backing up your MySQL database.

Mysqldump Command Syntax

mysqldump looks like this:

  • options you can see here
  • file.sql file name we will backup To use mysqldump, the MySQL server must be running.

Backup a Single MySQL Database

The most common use case of the mysqldump tool is to back up a database. We will backup the database “ex_database” with the root user and store it with the file name ex_database.sql. You use the following command:

After entering the password and successful authentication, the backup process starts to execute, the time it takes depends on the size of your database.

Backup Multiple MySQL Databases

Back up multiple MySQL databases with a command you need to use –database option followed by the list of databases you want to backup. Each database must be separated by space.

The above command will contain both databases in the databases_a_b.sql file.

Backup All MySQL Databases

The –all-databases option will back up all databases.

The above command will contain all databases troing file all_databases.sql.

Backup all MySQL databases to separate files

mysqldump does not support backing up all databases into files, but we can do that using the bash for loop.

The above command will create separate files for each database by using the database name as the file name.

Create a Compressed MySQL Database Backup

If the database size is too large, the best idea is to compress it. Use the gzip option to compress.

Create a Backup with Timestamp

Use timestamp to avoid duplicate file backup names.

Restoring a MySQL dump

You can restore the MySQL dump using mysql. The general syntax of the command is as follows:

You will need to create a database to import. If the database already exists, you need to delete it first, after creating the database you can start to restore the database.

Restore a Single MySQL Database from a Full MySQL Dump

If you have backed up all your databases with the -all-database option and you want to restore the database from a backup file that contains multiple databases, use the –one-database option like down here:

Export and Import a MySQL Database in One Command

Instead of dumping a database and then importing the backup into another MySQL database, you can use the following:

Before using the above command, make sure that the database exists on the remote server.

Automate Backups with Cron

Automating the database backup process is as simple as creating a cron job , which will run the mysqldump command at the specified time.

  1. Create .my.cnf file in the home user directory

Add the following text to my fiel my.cnf:

  1. Restrict file permissions so that only your users have access to it:

  1. Create backup folder:

  1. Open crontab file:

Adding the following routine job will create a backup copy of the mydb database name every day at 3 am:

This tutorial covers the basics, but it should be a good start for anyone who wants to learn how to create and restore a MySQL database from the command line using the mysqldump utility.

See you !!!

You can refer here: https://linuxize.com/post/how-to-back-up-and-restore-mysql-databases-with-mysqldump/#backup-all-mysql-databases-to- separate-files

Share the news now

Source : Viblo