NAME

mysqlblasy - MySQL backup for lazy sysadmins


SYNOPSIS

mysqlblasy [OPTIONS]


DESCRIPTION

mysqlblasy is a Perl script for automating MySQL database backups. It uses `mysqldump` for dumping mysql databases to the filessytem. It was written with automated usage in mind, e.g. it is very silent during operation and only produces noise on errors/problems. It rotates backups automatically to avoid that the backup disk gets full when the administrator is on vacation (or is lazy). All necessary information for producing backups can be specified in a configuration file, which eliminates the need to hide command line options from the process table (like passwords).

Each database gets dumped into a separate file, after which all the dumps get tarred/compressed and placed into the specified backup directory. Old files in the backup directory get deleted, and the number you specify newest files are kept (default 7).

Backups get filenames containing the hostname, the date and the time (accuracy: seconds).

Optionally, after the dumping has completed, mysqlblasy can run 'OPTIMIZE TABLE' on each table in the backup set.

The verbosity of the output can be specified using the loglevel configuration key. The recommended value for the loglevel is 2 (WARN).


CONFIGURATION FILE(S)

Configuration files: /etc/mysqlblasy.conf, $HOME/.mysqlblasyrc

Allowed config keys and values:

   backupdir           = directory for placing the backup
   databases           = comma separated list of db's to backup (default all)
   exclude databases   = comma separated list of db's to NOT backup
   defaults-extra-file = path to an alternative my.cnf config file
   dbusername          = mysql username (it is recommended  to use defaults-extra-file)
   dbpassword          = password for user (it is recommended  to use defaults-extra-file)
   dbhost              = hostname of database server, this is used for the backup filename too.
                         If you use a defaults-extra-file, this can be used to set the filename
                         of the backup-file!
   optimize_tables     = yes or no or 1 or 0 (default no)
   loglevel            = NOP(0) ERR(1) WARNING(2) NOTICE(3) INFO(4) DEBUG(5), default 2
   mysql               = absolute path to the mysql binary (default from $PATH)
   mysqldump           = absolute path to the mysqldump binary (default from $PATH)
   use compression     = yes or no or 1 or 0 (default no)
   compression tool    = see below
   keep                = number of backup files to keep in backupdir
   use syslog          = yes or no or 1 or 0 (default yes)
   tar                 = see below

Some of these configuration values may require special attention: 'compression tool' and 'tar' can be specified with their absolute filenames or with only the basename of the executable. If the the specified value cannot be resolved, mysqlblasy does NOT fall back to a default tool!

All configuration keys consisting of a filename/path can have a tilde '~' for specifying these HOME directory of the user running mysqlblasy.

Concerning security and mysql password disclosure in the process table, the recommended way to either use the mysql built-in config file called ~/.my.cnf and leave out the username and password or to specify an alternative config file with the defaults-extra-file option.

Example:

   [client]
   host     = localhost
   user     = bob
   password = y0uRp4s5w0r6
   socket   = /var/run/mysqld/mysqld.sock


OPTIONS

-c, --config-file file

Specify an alternative config file. The system-wide configuration is still read, but settings in the specified config file will override the system-wide ones.

-h, --help

Displays this help

-V, --version

Display version and exit


NOTES

mysqlblasy will try to use native command line utilities for tarring and compressing. If no adequate tools are found, it will try to use Perl native routines for tarring and compressing (requires some modules to be installed). The command lines tools are preferred for perfomance reasons (especially memory usage).


LICENSE

This program is distributed under the terms of the BSD Artistic License.


AUTHOR

Copyright (c) 2003-2008 Paul Kremer.


BUGS

Please send patches in unified GNU diff format to <pkremer[at]spurious[dot]biz>


SEE ALSO

mysql, mysqldump, Sys::Syslog, tar, gzip, bzip2, Archive::Tar, Achive::Zip


Function documentation

bootinit()

bootinit() will initialize some variables as well as environmental variables and load the required perl modules.

cfginit()

cfginit() will initialize the configuration, depending on command line parameters.

try_to_use(use_string)

will eval ``use use_string'' and return undef if it worked. Otherwise returns the exception text.

graceful_die(string message)

graceful_die() will log the given message as error and die.

_logString(mixed message)

_logString() will add debugging information to message and return it. If message is not a string, the variable will be preprocessed by Data::Dumper::Dumper().

_logIt(mixed message, int atlevel)

will format and log the message if atlevel is smaller or equal than the configured loglevel. Returns the logged message if it got logged to the logging backend.

log*(mixed message)

log*() will log the message at the corresponding LOGLEVEL and return the logged message, if it was logged.

_logToConsole(string message)

_logToConsole() will send message to the console (STDERR). Always returns 1.

_logToSyslog(string message, int atlevel)

_logToSyslog() will send message to the system syslog facility at the specified level. Always returns 1.

getConfigValue(string key)

getConfigValue() returns a string for the given configuration key. It fails on error. Note that this function uses the global config container, which means that configuration needs to be initialized before using this method.

setConfigValue(string key, value)

setConfigValue() sets the value for the given configuration key. It fails on error.

ldb_databases()

ldb_databases() will return a list of databases on the mysql server. It fails on error.

ldb_database_tables()

ldb_database_tables() will return a list of tables in the given database on the mysql server. It fails on error.

ldb_databases()

ldb_databases() will return a list of databases on the mysql server. It fails on error.

_system(arrayref params, string out)

_system is a wrapper for the perl system() function. It handles exit codes gracefully and can redirect STDOUT and STDERR of the executed program to the file specified by out. It returns true on success and false on error. It never fails. By default, STDOUT and STDERR are redirected to the null device, to disable output redirection, specify the empty string as output target. _system() never forks a shell (/bin/sh).

_hostname

_hostname() is a safe wrapper for Sys::Hostname::hostname(). It returns the hostname on success and the empty string on failure.

tmpDir()

tmpDir() will return the system-wide temporary directory name. See File::Spec->tmpdir() for details.

mkstempt()

mkstempt() will return a temporary file name. The file will have been created by the time the method returns. This prevents any possibility of opening up an identical file.

workDir()

workDir() will create a temporary directory and return the full path. If the directory already exists, it will choose a different path. If the directory was created from within the same process, it simply returns it. It fails on error.

hostDir()

hostDir() will create a directory inside workDir(). The name of the subdirectory if based on the 'dbhost' config value, or the local hostname if omitted. It returns the full path. If the directory was created from within the same process, it simply returns it. It fails on error.

getPath

getPath() will take the environment variable called PATH and parse it into an ARRAY of which it will return a reference. In case the environment variable cannot be parsed to a valid paths, it returns []. Every directory contained will be checked for existence. If no directory can be found, it will return undef.

findInPath(what, [where])

findInPath will try to find the file specified by what in the list of paths specified by where. It checks if the file is existing, readable and executable. If found, it returns the full name of the file, otherwise it returns undef.

fetchFile(string filename)

fetchFile() will read the contents of file filename into a scalar and return it. It fails on error.

expand_tilde( string filename )

expand_tilde() will expand any correctly used tilde in a path to the home directory of the current user E.g.:

   ~user
   ~user/blah
   ~
   ~/blah
getPreferences( config file )

getPreferences() will read preferences from the system wide and private configuration files. It returns a hash reference or fails on error. Optionally, a config file to be read can be specified as a parameter. If an optional config file is specified, only the system-wide config file is read and then the specified one, while omitting the config file in the user's HOME.

fuFile (string filename, \string buffer)

fuFile() will fetch the content from the file specified by filename, unlink it and put the data into the buffer specified. It will return true on success, false if it could not get the data, and fails if it can't unlink the file.

mydump( {db => string, all => bool, file => string} )

mydump() will dump the database(s) to the specified file.

makeNativeZip( directory, workdir, zipfilename)

makeNativeZip() will create the zip file using native Perl routines/libraries. Returns undef on failure or the filename of the created archive on success.

makeNativeTar( [files], workdir, tarfilename)

makeNativeTar() will create the tar file using native Perl routines/libraries. Returns undef on failure or the filename of the created archive on success.

makeExternalTar( [files], workdir, tarfilename)

makeNativeTar() will create the tar file using external command line utilities. Returns undef on failure or the filename of the created archive on success.

expandExecutableName(param, seed)

expandExecutableName() will try to expand the given name of an executable to a fully qualified, absolute filename. It takes two parameters: the first one is usually a user-supplied path or filename and the second parameter is a system default bare executable name in case no user supplied value is available.

   e.g.: expandExecutableName('/home/galaxy/bin/gzip', undef );
         expandExecutableName(undef, 'gzip');
         both examples make sense
makeTar(directory, [files], workdir, tarfilename)

makeTar() will create a tar file. TODO: more POD

purgeOldFiles(directory, number_of_files_to_keep)

purgeOldFiles() will delete all files older than the 'number_of_files_to_keep' files in the specified 'directory'.

version

Prints version information and exits.

help

Feeds this script to `perldoc`