Friday, February 22, 2013

MySQL Backup and Recovery

If your site manages it's data with MySQL, then you obviously need to make sure the data is safe. In this blog post, I will show how to create a daily backup automatically. I will also show a continuous data protection plan for MySQL databases. This blog post uses the previous backup server configured in my Secure Backup & Recovery with rsnapshot, rssh and OpenSSH article.
In order to understand this blog, let's define some important terms :
  • Backup server's hostname :
  • First MySQL server's hostname :
  • Second MySQL server's hostname :

Backup Server Setup (part 1 of 2)

The first thing we need to do on the backup server is to install the required software.

sudo yum -y openssh-clients rsnapshot mysql vim

Then configure a directory structure where the MySQL backups will be stored. Ideally, you want to create a seperate file system for this directory structure. And manage the file system under LVM2 so that you can increase it's size dynamically in the future. I'll skip the LVM2 setup for now.

sudo mkdir /export/backup/{conf,data,log,run,scripts}
sudo chown -R root:root /backup

Create two wrapper scripts to help the process.

sudo vim /export/backup/scripts/
sudo vim /export/backup/scripts/

Make sure both scripts are executable and that they don't have any syntax errors in them.

sudo chmod a+x /backup/scripts/*.sh
sudo sh -n /export/backup/scripts/
sudo sh -n /export/backup/scripts/

Configure both MySQL backup configuration files. WARNING : rsnapshot is very sensitive with spaces and tabs. DO NOT USE ANY SPACES IN THE CONFIGURATION FILE! You have been warned :)

sudo vim /export/backup/conf/rdbms.mysql.daily
sudo vim /export/backup/conf/rdbms.mysql.hourly

Make sure our backup log files don't consume too much disk space.

sudo vi /etc/logrotate.d/backup

And make sure our new logrotate configuration is still valid.

sudo logrotate -d /etc/logrotate.conf

Create the MySQL two backup scripts. Notice that in each of these two scripts, the variable MYSQL_HOST_LIST  is a space seperated list of all FQDN machines running MySQL. The beauty of this is that you can backup all your MySQL machines with a single script!

WARNING : be sure to change the user's password in both scripts!

sudo vim /export/backup/scripts/

sudo vim /export/backup/scripts/

Protect those scripts because they hold the MySQL backup user's password.

sudo chown root:root /export/backup/scripts/mysql_backup_*.sh
sudo chmod 700 /export/backup/scripts/mysql_backup_*.sh

We now need to configure the MySQL clients with the proper database backup user.

MySQL Client Configuration

Connect to each MySQL machines in order to create the backup user in their mysql database. Again, don't forget to update the user's password in the SQL commands. Let's start by our first MySQL server.

mysql -u root -p
mysql> create user 'backup'@'' identified by 'change_me';
mysql> grant all on *.* to 'backup'@'';
mysql> flush privileges;
mysql> exit;

And now do the same with the other MySQL machine.

mysql -u root -p
mysql> create user 'backup'@'' identified by 'change_me';
mysql> grant all on *.* to 'backup'@'';
mysql> flush privileges;
mysql> exit;

Backup Server Setup (part 2 of 2)

Back on the backup server, execute the mysql command to test if the new user can connect?

mysql -u backup -p -h
mysql -u backup -p -h

Once that is done, we can configure root's crontab to execute both of these scripts.

sudo crontab -e

Once the backups are done, you will now have the following data in your data folder.

sudo ls -AlFR /export/backup/data/rdbms.mysql/

total 20
drwxr-xr-x  3 root root 4096 Feb 22 13:30 daily.0/
drwxr-xr-x  4 root root 4096 Feb 22 14:00 hourly.0/
drwxr-xr-x  3 root root 4096 Feb 22 13:46 hourly.1/

total 12
drwxr-xr-x 2 root root 4096 Feb 22 13:30 all_servers/

total 292
-rw------- 1 root root 139900 Feb 22 13:30
-rw------- 1 root root    985 Feb 22 13:30
-rw------- 1 root root 138348 Feb 22 13:30
-rw------- 1 root root   2247 Feb 22 13:30

total 16
drwxr-xr-x 2 root root 4096 Feb 22 14:00 all_servers/
drwxr-xr-x 2 root root 4096 Feb 22 13:46 prod/

total 152
-rw------- 1 root root    985 Feb 22 14:00
-rw------- 1 root root 138354 Feb 22 14:00
-rw------- 1 root root   2254 Feb 22 14:00

total 152
-rw------- 2 root root    985 Feb 22 13:46
-rw------- 2 root root 138356 Feb 22 13:46
-rw------- 2 root root   2255 Feb 22 13:46

total 12
drwxr-xr-x 2 root root 4096 Feb 22 13:46 prod/

total 152
-rw------- 2 root root    985 Feb 22 13:46
-rw------- 2 root root 138356 Feb 22 13:46
-rw------- 2 root root   2255 Feb 22 13:46


Should you ever need to recover (and you should try this before you really have to use this!) simply use one of the SQL scripts generated. For example, if we need to restore the entire database on host, we would do this :

mysql -u backup -p -h <

That is assuming the host was reinstalled as a result of a catastrophic failure or security beach. If you already have your databases on the host, make sure to drop them all before you do that.




  1. Links are dead for MySQL backup configuration

    1. Indeed, sorry for that. It should be fixed now.