Home » Linux » Using rsnapshot for daily MySQL backups

Using rsnapshot for daily MySQL backups

Now that I’m using a CloudServer I figured I had to do something about backups as well. I’ve been using rsnapshot at work for a few years now and it’s an ideal backup solution. Because rsnapshot (which uses rsync as its base) makes use of hard links backups are very efficient. Basically it means that if a file isn’t changed it’s not duplicated, but a hard link is created instead. This way only changed and new files get copied. Deleted files will eventually be removed as well as soon as one of the backups expires.

Using rsnapshot for file backups is easy and the configuration explains this easy enough. Backing up MySQL databases however is a different story. When using MyISAM tables you might get away with copying and pasting the database files. With InnoDB not so much. Proper backups can be made with mysqldump. In my case I don’t have many databases so I want one big SQL backup file.

First, lets set up our backup script. I’ve stored it under /usr/local/bin/backup_mysql.sh with permissions set at 0700. The reason for these permissions is that I store my password inside the script and only want root to be able to open it. It also needs to be executable. I know it’s better to use a configuration file and use source to include the variables, but for my current use it’ll do (do note at work I’ve done this the proper way!). I also use the root user instead of a special read-only user. I know this is bad practice, but again I’ve done this the proper way at work. It’s an exercise for you to do it properly (hehe, that’s another way to put that I’m lazy). Anyway, here’s the script.


Nothing exciting. Change the credentials as required. Again, use source if you want to store the credentials inside a proper configuration file. Now, calling this script ./backup_mysql.sh will backup all databases inside the file mysqldump.sql in the current work directory.

Next is setting up /etc/rsnapshot.conf which is easy as well. At the end of this file you’ll find all the instructions of which directories to backup. Enter the next line. Do note that rsnapshot.conf uses a tab to separate values.

  1. backup_script   /usr/local/bin/backup_mysql.sh  mysql/

To enable daily backups uncomment the next line:

  1. #interval       hourly  6
  2. interval        daily   7
  3. #interval       weekly  4
  4. #interval       monthly 3

To make rsnapshot run every day update /etc/cron.d/rsnapshot:


Finally, try running rsnapshot. After it has run check your backup directory, which on Ubuntu 10.04 defaults to /.snapshots/. Be sure to mount /.snapshots/ on a separate drive. Because what use is it if your data drive contains your backup and decides to commit suicide?

$ sudo rsnapshot daily

Your MySQL backup file should now be located at /.snapshots/daily.0/mysql/mysqldump.sql. You can restore this file with the MySQL commandline client, or through MySQL Administrator from the MySQL GUI Tools.

Posted in Linux, Ubuntu and tagged as , , , , ,

One comment on “Using rsnapshot for daily MySQL backups

  • For some reason my syntax highlighter won’t show a double dash. The mysqldump -all-databases option should of course be –all-databases.