Automatized online backup of MySQL databases using LVM snapshots

This is again a script that people could write easily if they understood some shell scripting, but it is quite well implemented with logging and all and maybe spares a couple of minutes/hours of your time. Smile

You should use it with crontab on a daily basis. For details take a look at the desc. of the MySQL backup script.

The main difference between this one and its predecessor is that this script creates a backup of an LVM volume (with everything on it ... eg. the complete MySQL data directory with all databases). Using LVM snapshots we can avoid to lock the database (tables) for minutes (while creating the backup), thus this is a suitable method for creating online backups (ie. save the database state while the system is running and is being used).

However, this is not equivalent to an SQL dump! The compressed form of the database directory cannot be moved between different architectures (eg. a 32 bit MySQL instance and a 64 bit version), while an SQL dump can be used even in this case.

The idea came from Dennis Guba's comment (the first on the page) on the backup page of the MySQL 5.0 documentation.

AttachmentSize
mysql_lvm_backup-1.0.0.zsh4.02 KB
mysql_lvm_backup-1.0.1.zsh3.99 KB
mysql_lvm_backup-1.1.0.zsh3.99 KB
mysql_lvm_backup-2.0.0.sh5.9 KB

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Automatized online backup of MySQL databases using LVM snapshots

Fixed in v1.0.1:
  • The umask setting was completely screwed up and backup files created by the script were world-writable. Of course the problem only affected those who had bad permissions on the backup directory too.

MySQL LVM backup v1.1.0

Added:
  • Bugfix: the "days" variable was not used properly in the cleanup code, find ... -mtime +${days} returned ${days}+2 items. Replaced it with an ls -1t + tail -n +$((days+1)) combo.
  • Bugfix: in case the directory specified by the "backupdir" variable was not writable, the script still tried to log an error message in that directory.

MySQL LVM backup v2.0.0

News:
  • Dropped Z shell (zsh) requirement. The script works now with the standard /bin/sh (Bourne) shell.
  • Added option to copy backups to a remote storage via SFTP.