mylvmbackup — a utility for creating MySQL backups using LVM snapshots
mylvmbackup [OPTIONS]
mylvmbackup is a tool for quickly creating backups of MySQL server's data files. To perform a backup, mylvmbackup obtains a read lock on all tables and flushes all server caches to disk, makes an LVM snapshot of the volume containing the MySQL data directory, and unlocks the tables again. The snapshot process takes only a small amount of time. When it is done, the server can continue normal operations, while the actual file backup proceeds.
The LVM snapshot is mounted to a temporary directory and all data is backed up using the tar program. By default, the archive file is created using a name of the form backup-YYYYMMDD_hhmmss_mysql.tar.gz, where YYYY, MM, DD, hh, mm, and ss represent the year, month, day, hour, minute, and second of the time at which the backup occurred. The prefix backup, date format and file suffix may be modified. The use of timestamped archive names allows you to run mylvmbackup many times without danger of overwriting old archives.
Alternatively, instead of tar, you may use rsync. This process is nearly identical, with the exception that the file suffix is not used. Currently, the rsync backup is primarily developed for performing local backups. You can configure it for backing up to a remote rsync server, but this is not fully tested in all possible configurations yet and may change at some point.
Additionally, a backup type none is provided for cases where the user wants to use mylvmbackup only for creating the snapshots and intends to perform the actual backup by using the appropriate hooks. (Or for cases where the snapshot itself is considered to be the backup).
It is required to run mylvmbackup on the same host where the MySQL server runs. If your MySQL daemon is not listening on localhost or using the default socket location, you must specify --host or --socket. Even though mylvmbackup communicates with the server through a normal client connection to obtain the read lock and flush data, it performs the actual backup by accessing the file system directly. It is also a requirement that the MySQL server's data directory resides on an LVM volume. (It is, however, a good idea to do the LVM backup to a different partition than the one where the data directory resides. Otherwise, there is a good chance that LVM will run out of undo space for LVM snapshot maintenance and the backup will fail.)
The user who invokes mylvmbackup must have sufficient filesystem permissions to create the LVM snapshot and mount it. This includes read/write access to the backup directory.
If you plan to back up InnoDB tables using LVM snapshots, be advised that it is not sufficient to lock the tables and issue the FLUSH TABLES command to get the table files into a consistent state. When starting the MySQL server from these restored files, InnoDB will detect these tables as being in an inconsistent state and will perform a recovery run before the tables can be accessed again. As this can potentially take some time (which you may not want to spend after restoring a server and trying to get it back on its feet as fast as possible), consider using the option --innodb_recover, which will perform the recovery operation on the backup snapshot prior to archiving it.
The recovery operation is performed by spawning a second mysqld instance that uses the snapshot volume as the data directory. Note that this functionality currently assumes the default InnoDB configuration - it does not work properly if you use options like --innodb-file-per-table, --innodb-data-home-dir, --innodb-data-file-path or --innodb-log-group-home-dir that modify the default file layout for InnoDB tables.
Also note that this only works when using writable LVM snapshots and thus requires LVM2 (the script performs a test for this and will disable log recovery in the case it finds an LVM1 system). This will prolong the time needed to perform the actual backup, but will save you precious time when you eventually have to restore from this backup set.
If you use InnoDB tables exclusively, you may also want to consider to include the option --skip_flush_tables, to avoid the probably time-consuming and in this case unnecessary flushing of buffers. But don't enable this option when MyISAM tables are involved!
It is possible to run arbitrary external programs or scripts (hooks) at various stages of the backup process, to perform additional actions as part of the backup process.
These scripts or symbolic links to executables should be placed in the directory that the hooksdir configuration option points to (/usr/share/mylvmbackup by default). They should return zero upon successful completion, any non-zero return value will be considered a failure which will be logged.
The names of the scripts or symbolic links reflect the stage in which the hook will be called. Currently, the following stages exist:
These hooks are optional and will only be called if a file for the particular stage exists and is executable. The execution of all hooks can be supressed by passing the --skip_hooks option or by setting the skip_hooks configuration option to 1;
mylvmbackup supports the following command line options. The same options can also be defined in the /etc/mylvmbackup.conf configuration file (omitting the leading dashes, of course).
For proper operation mylvmbackup requires Perl 5 with the DBI and DBD::mysql modules. It also needs the Config::IniFiles to read the global configuration file of the program and Sys::Syslog in case you want to enable the syslog log facility. Date::Format is required to create the time stamp used in the backup file names. In addition, it utilizes Getopt::Long, File::Basename and File::Temp, which usually are part of the default Perl distribution.
It also requires several other external programs: GNU tar and gzip to back up the data, LVM utilities (lvcreate, lvremove and lvs) to create and remove the LVM snapshot, and the system utilities mount and umount.
rsync may be used in place of tar and gzip.
This program was initially written by Aleksey "Walrus" Kishkin from MySQL AB, with suggestions from Peter Zaitsev and Lenz Grimmer.
It is currently maintained by Lenz Grimmer, <lenz@grimmer.com>
Main web site: http://www.lenzg.org/mylvmbackup/ Mailing list: http://www.freelists.org/list/mylvmbackup/ Source code, bug tracker: https://launchpad.net/mylvmbackup/
Several people have contributed to the script since it has been released. See the ChangeLog for more details.
Robin H. Johnson from the Gentoo project cleaned up the code and added several useful features.
Fred Blaise contributed the initial support to use an external configuration file and logging via syslog.
Eric Bergen provided the code that performs the InnoDB recovery prior to performing the backup of LVM2 snapshot volumes. He also fixed the broken handling of default options.
Kristian Köhntopp for suggesting the --pidfile option and suggesting to add the output of the LVM statistics for being able to better tune the snapshot size.