User Tools

Site Tools


shell:mysql_wpb

MySQL_WPB (MySQL web application backup script)

MySQL_WPB is small and simple bash script with task to backup almost any web project developed with MySql. I have developed it for backuping joomla projects but it can be adjusted to backup almost any project which use MySQL.

This script requires you to have tar, bzip and mutt (optionally if you want it to send you email with backup attached) installed on your system. And usually it is run as cron job. In version 2 if you want to check are there some changes after last backup you will need awk, cat, find and md5 or md5sum. Script will try to find this requirements and if it can not find it will exit and give you error message.

This script is made to backup whole installation of your project (application). This way it is possible to restore your site in just few minutes. So for example if you are using it for backuping joomla it should backup joomla root directory. That can be quite large file depending on your site so be careful when using email option.

Download

Documentation

General Info

This script dumps database to a file and save it to joomla (or any other) directory. Then it tar and compress (bzip2) that whole directory and saves this compressed file to some other directory (or in the same directory). Optionally it can send compressed file by email and after that delete local copy.

Version 2

This version does everything as version 1 but it has support to check are there any changes happened after last backup. This check is checking are some files modification time newer from old backup file. It also compares md5 hash of old SQL dump file and new SQL dump file. This check is optional (see MD5CHECK variable).

Joomla users - do not be surprised if you did not change anything and this script is generating backup. Joomla is changing db records when visitors are just visiting your site.

Configuration

As this is bash script you should edit few variables before using it.

VariableVersionDescription
BACKUP_PREFIX 1 This will be the name of backup file. The date and time info will be added to this name
EMAIL 1 Email address where backup file will be sent
SUBJECT 1 Subject which will be set to email with backup file
MYSQLDUMP_DIR 1 Directory which contains mysqldump executable. If wrongly set script will try to find it using which
DUMP_FILE 1 name of file where database will be dumped
DUMP_USERNAME 1 db username which will be used by mysqldump to connect to database
DUMP_PASSWORD 1 db password for username (DUMP_USERNAME) which is used to connect to database
DUMP_DB_NAME 1 name of database wilch will be dumped by mysqldump
BZIP_DIR 1 Directory which contains bzip2 executable. If wrongly set script will try to find it using which
JOMMLA_ROOT 1 Directory which will be used to save dump file. It will be tar-ed and compressed with bzip2. So for joomla projects this will be joomla root directory so all file in it will be backuped. That means all joomla installation with all media files, joomla extensions and language files will be saved.
TAR_DIR 1 Directory which contains tar executable. If wrongly set script will try to find it using which
BACKUP_DIR 1 Directory where backup file will be saved. If script send email local file (in this directory) will be deleted at cleanup process
WHAT 1 This could be set to email to instruct script to send backup file by email and delete local created one. If set to anything else script will save backup file in BACKUP_DIR.
MUTT_DIR 1 Directory which contains mutt executable. If wrongly set script will try to find it using which
MD5CHECK 2 If set script will try to see if there were any change from last backup. It there were changes it will continue with backup and of there were no changes it will stop.
MD5FILE 2 File name where old md5 hash of sql dump file will be saved. Also it will contain path of old backup file
DUMP_DB_HOST 3 Database host (default localhost)

Usage

This script is intended to be used with cron but it will output info messages to stdout for easier setup. When you are satisfied with your setup you could suppress this messages by redirecting stdout to /dev/null because otherwise cron will send you email with this messages. Maybe you will like to receive such messages because thay will tell you that backup is done or that some error occurred and backup failed.

FAQ

Send your questions to boris@kodmasin.net.

  1. Can you only back up one database at a time? My site has two databases. If this can be done, then how?
    You can backup more then one databases but I have never tested it. Just set DUMP_DB_NAME=“-B db_name1 db_name2”. If you prefer you could replace “-B” with “–databases”.
  2. I would like to use this script, but I need to indicate dbhost… can you help me please ?
    Check version 3.
    Dirty fix for version 1 & 2 would be to set DUMP_DB_NAME=“-h db_host_name db_name”. I am not sure it will work correctly.
shell/mysql_wpb.txt · Last modified: d.m.Y H:i (external edit)