Register
It is currently Sat Aug 30, 2014 6:20 am

mySQL backup script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Fri Feb 25, 2011 2:48 am   

Joined: Fri Feb 25, 2011 12:49 am
Posts: 1
Hi, I'm trying to write a bash script to export each database from a mysql database using mysql dump for backup purposes. I've got what I have below, however the output is a bit odd to me, and I'm having trouble with a couple of things. Note for this particular server the script is running under cygwin, but I'm hoping to have one script to also work on ubuntu.

script thus far:
Code:
#!/bin/bash

# call this script with two parameters
# backupmysql.sh [backupto] [mysqlexe]
# backup from should be an ssh user login location
# backup to should be a local path with no trailing slash
#
# eg.
#     ./backupmysql.sh /cygwin/c/brian "/cygdrive/c/Program\\ Files/MySQL/MySQL\\ Server\\ 5.1/bin/"
#

# define where mysql is
mysqlCmd='/cygdrive/c/SWSoft/Plesk/Databases/MySQL/bin/mysql'
mysqlCmd='/cygdrive/c/Program\\ Files/MySQL/MySQL\ Server\ 5.1/bin/mysql'
mysqlCmd=$2

# logon details
user='username'
pass='password'

# other variables
outputPath=$1
outputPath="/cygdrive/d/backup"
exportFailure="no result"

echo "---- variables ----"
echo "  mysqlCmd     = $mysqlCmd"
echo "  user         = $user"
echo "  outputPath   = $outputPath"
echo ""
echo ""

# get databases command
echo "getting database list..."
#databases=$(/cygdrive/c/Program\ Files/MySQL/MySQL\ Server\ 5.1/bin/mysql -u $user -p$pass -h localhost -Bse 'show databases')
databases=$(mysql -u $user -p$pass -h localhost -Bse 'show databases')
echo "done"
echo ""

# loop around the databases
echo "backing up databases to $outputPath now ... "
for database in $databases
do
   dbExportPath=$outputPath
   dbExportPath+="/"
   dbExportPath+=$database
   echo "****"
   echo "$database"
   echo "$dbExportPath"
   echo "  backing up database $dbExportPath now ... "
   mkdir -p "$outputPath"
   echo "  backup path $dbExportPath"
   mysqldump -u $user -p$pass -h localhost $database > "$dbExportPath"
   exportExitCode=$?
   echo "  backup exit code $exportExitCode "
   if [ $exportExitCode != 0 ]; then
      exportFailure=true
   fi
   echo "  done"
done
echo "finished all databases"
echo ""

echo "checking for export failures..."
echo "export failure: $exportFailure"
if [[ $exportFailure == "no result" ]]; then
   exportFailure=false
   echo "no export failure found"
else
   echo "export failure found"
   exit 1
fi
echo ""

exit 0


first thing is the output, the lines in the foreach loop get jumbled up a bit:

Code:
$ ./backupmysql.sh /cygwin/c/brian "/cygdrive/c/Program\ Files/MySQL/MySQL\ Server\ 5.1/bin/"
---- variables ----
  mysqlCmd     = /cygdrive/c/Program\ Files/MySQL/MySQL\ Server\ 5.1/bin/
  user         = root
  outputPath   = /cygdrive/d/backup


getting database list...
done

backing up databases to /cygdrive/d/backup now ...
****
information_schema
.sqldrive/d/backup/information_schema
.sql now ... database /cygdrive/d/backup/information_schema
.sqlckup path /cygdrive/d/backup/information_schema
' when selecting the databasencorrect database name 'information_schema
  backup exit code 2
  done
finished all databases

checking for export failures...
export failure: true
export failure found


the "now ..." bit gets put at the start of the line even though it should be at the end.
likewise the ".sql" gets put at the start of the line.
Is there a way arround this? it mess's up the path I want to then export to.

Also, I was trying to pass the path of the mysql executable in, but if I did that I couldn't work out how to get it to execute (from the passed in variable).


Top
 Profile  
 PostPosted: Fri Feb 25, 2011 5:09 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
To execute the contents of a variable as a command;
Code:
~]# mycommand=/path/to/stuff
~]# ${mycommand}
~]# ${mycommand} --thisswitch --thatswitch
~]#
~]# mycomplexcommand='/path/to/stuff --with --switches'
~]# ${mycomplexcommand}


Variable substitution occurs before command processing.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP