Create Physical Backups of your MariaDB or MySQL Databases

Updated by Linode Written by Linode

Contribute on GitHub

Report an Issue | View File | Edit File

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.

The steps in this guide require root privileges. Log in as the root user with su - before you begin.

Create a Backup

  1. If you are not running in recovery mode (a Finnix session), stop the mysql service:

    systemctl stop mysql
  2. Locate your database directory. It should be /var/lib/mysql/ on most systems but if that directory doesn’t exist, examine /etc/mysql/my.cnf for a path to the data directory.

  3. Create a directory to store your backups. This guide will use /opt/db-backups but you can alter this to suit your needs:

    mkdir /opt/db-backups
  4. Copy MySQL’s data directory to a storage location. The cp command, rsync, or other methods will work fine, but we’ll use tar to 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/*
  5. Restart the MySQL service:

    systemctl restart mysql

Restore a Backup

  1. 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:

  2. Stop the mysql service:

    systemctl stop mysql
  3. Extract 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 .
  4. Move the current contents of /var/lib/mysql to another location if you want to keep them for any reason, or delete them entirely. Create a new empty mysql folder to restore your backed up DMBS into.

    mv /var/lib/mysql /var/lib/mysql-old
    mkdir /var/lib/mysql
  5. Copy the backed up database system to the empty folder:

    mv ~/var/lib/mysql/* /var/lib/mysql
  6. Set the proper permissions for the files you just restored:

    chown -R mysql:mysql /var/lib/mysql
  7. Restart 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.

comments powered by Disqus

This guide is published under a CC BY-ND 4.0 license.