Skip to content

Instantly share code, notes, and snippets.

@AydinHassan
Last active August 29, 2015 13:57
Show Gist options
  • Save AydinHassan/9408663 to your computer and use it in GitHub Desktop.
Save AydinHassan/9408663 to your computer and use it in GitHub Desktop.
Crude database ssh dump script
#!/bin/bash
if [ -z "$1" ]; then
echo "Please enter SSH host as first parameter. Eg aydin@192.168.0.1"
exit 1
fi
sshHost=$1
if [ -z "$2" ]; then
echo "Please pass database name as seond parameter"
exit 1
fi
dbName=$2
if [ -z "$3" ]; then
echo "Please pass database user as third parameter"
exit 1
fi
dbUser=$3
if [ -z "$4" ]; then
echo "Please pass database password as fourth parameter"
exit 1
fi
dbPass=$4
if [ -z "$5"]; then
outputFile="."
else
outputFile=$5
if [ ! -d "$outputFile" ]; then
echo "Directory $outputFile does not exist"
exit 1
fi
fi
fileName=$dbName-`date +%Y-%m-%d`.sql
error=false
ssh $sshHost "
if [ -e $fileName ]; then
echo 'File already exists'
error=true
exit
fi
mysqldump -u $dbUser -p$dbPass $dbName --ignore-table=$dbName.log_customer --ignore-table=$dbName.log_quote --ignore-table=$dbName.log_summary --ignore-table=$dbName.log_url --ignore-table=$dbName.log_url_info --ignore-table=$dbName.log_visitor --ignore-table=$dbName.log_visitor_info --ignore-table=$dbName.report.event --ignore-table=$dbName.report_viewed_product_index>$fileName
mysql $dbName -u $dbUser -p$dbPass -e 'show tables like \"log_%\"' | grep -v Tables_in | xargs mysqldump $dbName -u $dbUser -p$dbPass --no-data >>$fileName
gzip $fileName
dumpStatus=$?
if [ ! dumpStatus ]; then
echo 'Database dump failed'
error=true
exit
fi
"
if $error; then
exit 1
fi
scp $sshHost:$fileName.gz $outputFile
echo "Database dumped to $outputFile/$fileName.gz"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment