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).