MySQL and MariaDB include a built-in backup utility that simplifies the process to create a backup of a database. With mysqldump, you can create a logical backup, only if your database is accessible and running.
This guide will show you how to create a backup of MySQL and MariaDB database using mysqldump utility.
Prerequisites
This tutorial assumes that you have a working MySQL or MariaDB installation, including a non-root user with sudo privileges.Manual Database Backup
Login into your database server, type below command to create a backup of an entire Database Management System (DBMS):You can create a specific database backup with below command:
Make sure you replace username with a user that has access to the database and exampleDB with the name of the database you intend to back up:
You can even create a single table backup from any database:
Automate Database Backup
This section will show you how to schedule a backup task using cronjob to regularly create database backups.Create a file .mylogin.cnf under your user's home directory to hold the login credentials of the MySQL or MariaDB root user. Note that the system user whose home directory this file is stored in can be unrelated to any MySQL users.
Add the MySQL or MariaDB database root credentials, make sure you replace the password field with your database root user's password:
Save and close the editor.
Make .mylogin.cnf file read-only with below command:
Next, create the cron job file to back up the entire database management system every day at 2:00AM:
Add the following line, make sure you replace highlighted text with yours:
Save and close the editor.
Restore Database Backup
This section demonstrates, how to restore an entire database management system (DBMS) backup.Type below command to restore entire DBMS from the backup:
This will prompt you for the MySQL root user’s password:
To restore a single database dump, make sure an empty or old destination database must already exist to import the data into it, and the MySQL user you’re running the command must have write access to that database:
No comments: