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:
# 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
# ./backupmysql.sh /cygwin/c/brian "/cygdrive/c/Program\\ Files/MySQL/MySQL\\ Server\\ 5.1/bin/"
# define where mysql is
mysqlCmd='/cygdrive/c/Program\\ Files/MySQL/MySQL\ Server\ 5.1/bin/mysql'
# logon details
# other variables
echo "---- variables ----"
echo " mysqlCmd = $mysqlCmd"
echo " user = $user"
echo " outputPath = $outputPath"
# 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')
# loop around the databases
echo "backing up databases to $outputPath now ... "
for database in $databases
echo " backing up database $dbExportPath now ... "
mkdir -p "$outputPath"
echo " backup path $dbExportPath"
mysqldump -u $user -p$pass -h localhost $database > "$dbExportPath"
echo " backup exit code $exportExitCode "
if [ $exportExitCode != 0 ]; then
echo " done"
echo "finished all databases"
echo "checking for export failures..."
echo "export failure: $exportFailure"
if [[ $exportFailure == "no result" ]]; then
echo "no export failure found"
echo "export failure found"
first thing is the output, the lines in the foreach loop get jumbled up a bit:
$ ./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...
backing up databases to /cygdrive/d/backup now ...
.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
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).