Create Physical Backups of your MariaDB or MySQL Databases
Updated by Linode Written by Linode
While the mysqldump tool is the preferred backup method for a MariaDB or MySQL database or database system it only works when the database server is accessible and running. If the database cannot be started or the host system is inaccessible, the database can still be copied directly.
A physical backup is often necessary in situations when you only have access to a recovery environment (such as Finnix) where you mount your system’s disks as external storage devices. If you want to read about logical backups using mysqldump, see our guide on the topic.
For simplification, the name MySQL will be used throughout this guide but the instructions will work for both MySQL and MariaDB.
NoteThe steps in this guide require root privileges. Log in as the root user withsu -before you begin.
Create a Backup
If you are not running in recovery mode (a Finnix session), stop the
mysqlservice:systemctl stop mysqlLocate your database directory. It should be
/var/lib/mysql/on most systems but if that directory doesn’t exist, examine/etc/mysql/my.cnffor a path to the data directory.Create a directory to store your backups. This guide will use
/opt/db-backupsbut you can alter this to suit your needs:mkdir /opt/db-backupsCopy MySQL’s data directory to a storage location. The
cpcommand,rsync, or other methods will work fine, but we’ll usetarto recursively copy and gzip the backup at one time. Change the database directory, backup filename, and target directory as needed; the-$(date +%F)addition to the command will insert a timestamp into the filename.tar cfvz /opt/db-backups/db-$(date +%F).tar.gz /var/lib/mysql/*Restart the MySQL service:
systemctl restart mysql
Restore a Backup
Change your working directory to a place where you can extract the tarball created above. The current user’s home directory is used in this example:
cdStop the
mysqlservice:systemctl stop mysqlExtract the tarball to the working directory. Change the tarball’s filename in the command to the one with the date you want to restore to.
tar zxvf /opt/db-backups/db-archive.tar.gz -C .Move the current contents of
/var/lib/mysqlto another location if you want to keep them for any reason, or delete them entirely. Create a new emptymysqlfolder to restore your backed up DMBS into.mv /var/lib/mysql /var/lib/mysql-old mkdir /var/lib/mysqlCopy the backed up database system to the empty folder:
mv ~/var/lib/mysql/* /var/lib/mysqlSet the proper permissions for the files you just restored:
chown -R mysql:mysql /var/lib/mysqlRestart the MySQL service:
systemctl restart mysql
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
Join our Community
Find answers, ask questions, and help others.
This guide is published under a CC BY-ND 4.0 license.