#!/bin/bash ## Author: Richard S.W. Palusaar ## E-mail: mysqldumper@sublogic.se ## GTalk: generic477@gmail.com ## MSN: generic477@hotmail.com ## GoogleWave: generic477@googlewave.com ## www: www.sublogic.se ## IRC: lazarus477 freenode.net ## YY-MM-DD ## 2009-10-10 # # Module name: MySQL Dumper # This script backs up MySQL databases to indevidual files with # file names matching the indevidual databases along with a .sql # suffix appended: database.sql. # # The script will backup all databases to which the current user # executing this script has access to. # # Settings # Directory to dump databases to: BACKUPDIR=./backups ## Internal variables # Script arguments. ARGS=("$@") PNAME="MySQL Dumper" PVER=1.0 # Databases to dump. DBASES=(`mysql --batch --skip-column-names -e "show databases"`) # Number of databases to dump. NDBASES=${#DBASES[@]} ####### Function definitions ############ # Variables for chk functions. If this variable has a non-zero value # it indicates a critical check has failed and results in program termination upon call to chkend(). CHK_OK=0 CHK_WARNING=1 CHK_CRITICAL=2 CHK_STATUS=$CHK_OK # Outputs a message and the text OK in color along with a new line. # Arg 1 is the message and should describe what succeeded. pok () { printf "%s" "${1}" tput setaf 2 printf " OK\n" tput sgr0 return 0 } # Outputs a message and the text FAILED! in color along with a new line. # arg1 is the message and should describe what failed. # arg2 can be either 0, 1 or 2 to indicate the importance of the failure. # 0 indicates that it is ok to proceed with execution, 1 indicates a warning but the program # can still function normally and 2 indicates the program cannot proceed to function # normally and must be terminated. pfailed () { printf "%s" "${1}" tput setaf 1 printf " FAILED!\n" tput sgr0 case ${2} in $CHK_OK ) return 0;; $CHK_WARNING ) return -1;; $CHK_CRITICAL ) exit 1;; esac } # Check functions. These are used for ensuring the environment meets the required # parameters for proper operation of this program. # Checks if program arg1 is available and returns 0 if found or -1 if not. # arg 2 indicates importance, see pfailed(). chkprog () { if type -P "${1}" > /dev/null then pok "${1}" return 0 else pfailed "${1}" "${2}" return -1; fi } # Checks if directory arg1 exsists. Returns 0 if found or -1 if not. # arg 2 indicats importance, see pfailed(). # A message describing the purpus of the directory is passed as arg3. chkdir () { if [ -d "${1}" ] then pok "${3}" return 0 else pfailed "${3}" "${2}" return -1; fi } # Checks if a critical check has failed and if so terminates program. chkend () { case $CHK_STATUS in $CHK_OK) printf "\nAll checks ended with success.\n";; $CHK_WARNING) printf "\nChecks ended with one or more warnings!\n";; $CHK_CRITICAL) printf "\nOne or more checks ended with critical failure!!!\n";; esac } # Message printing functions. pmsgh () { tput smul ul printf "\n${1}\n" tput sgr0 } pmsg () { printf "${1}\n" } ########### Main logic ############# ## Clear terminal. For testing only! # clear ## Print welcome text if type -P tput &> /dev/null; then tput setaf 2; fi if which figlet &> /dev/null then figlet -t $PNAME printf "\n" else printf "%s" "$PNAME\n" fi tput sgr0 printf "\nversion %s\n\n" "$PVER" ## Do checks and and display results. pmsgh "Performing checks" chkprog mysqldump $CHK_CRITICAL chkdir "$BACKUPDIR" $CHK_CRITICAL "backup directory" chkend # Print settings pmsgh "Settings" pmsg "Backup directory is: $BACKUPDIR" # Do backups pmsgh "Backing up MySQL databases" pmsg "There are a total of $NDBASES databases to backup." for (( i=0; i < $NDBASES; i++ )) do dbase=${DBASES[$i]} if mysqldump --opt $dbase > "$BACKUPDIR/$dbase".sql; then pok "$dbase"; else pfailed "$dbase"; fi done