Mysql: script taking dump of all dbs

 
 
#!/bin/sh 
set -o pipefail
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin 
#---------------------------------------------------
# (1) set up all the mysqldump variables 
FILE=$1 
USER=$2
PASS=`echo "$3" | base64 --decode`


 ### file should be in gz format  
filename=$(basename "$FILE") 
directory=$(dirname "$FILE") 
extension="${filename##*.}" 
if [ "$extension" != "gz" ]     
    then               
  echo "** You should specify a gz file"             
    exit 1
 fi
  if [ ! -d "$directory" ]      
   then           
      # max 1 level    
             mkdir "$directory"               
  if [ "$?" -ne "0" ]          
       then                       
  echo "** Error: something went wrong in creating directory : ${directory}"                     
    exit 1              
   fi 
fi   
# (2) in case you run this more than once a day, remove the previous version of the file unalias rm     2> /dev/null
rm ${FILE}     2> /dev/null 
rm ${FILE}.gz  2> /dev/null
# (3) do the mysql database backup (dump) 
# use this command for a database server on localhost. add other options if need be. 
if [ ! "${PASS}" ]     
    then           
      mysqldump --opt --user=${USER} --all-databases | gzip  > ${FILE}   
              if [ "$?" -eq 0 ]        
                 then            
                     echo "** Success"                  
       else                
                 echo "** Mysqldump encountered a problem look in database.err for information"    
                             exit 1   
             fi       
  else              
   mysqldump --opt --user=${USER} --password=${PASS} --all-databases |gzip > ${FILE}       
          if [ "$?" -eq 0 ]      
                   then            
                     echo "** Success"           
              else                  
               echo "** Mysqldump encountered a problem look in database.err for information"    
                     exit 1       
          fi 
fi  
 ## check size 
FILESIZE=`stat -c%s "${FILE}"` 
if [ "${FILESIZE}" -le "100000"  ] 
then               
  echo "** Size of generated file quite small. Please check"                 ls -l ${FILE}        
         exit 1 
fi  
# (5) show the user the result echo "${FILE} was created:"
 ls -l ${FILE}   


Share your love