Skip to content

Instantly share code, notes, and snippets.

@brock
Created September 20, 2012 01:03
Show Gist options
  • Save brock/3753361 to your computer and use it in GitHub Desktop.
Save brock/3753361 to your computer and use it in GitHub Desktop.
Export DB, gzip and domain replacement.
#!/bin/bash
USER=""
PASSWORD=""
if [ -z $1 ]; then
echo -e "ERROR: No database name. Usage: export_db DATABASE_NAME"
exit;
fi
DB=$1
OUTPUT_FILE="$DB.sql"
if [ -f $OUTPUT_FILE ]; then
read -p "Do you want to replace the existing file $OUTPUT_FILE? [y/n]" yn
case $yn in
[Yy]* )
rm $OUTPUT_FILE;;
* )
timestamp=$(date +%m%d%y%H%M%S);
OUTPUT_FILE=$DB.$timestamp.sql;;
esac
fi
mysqldump --user=$ROOT --password=$PASSWORD $DB > $OUTPUT_FILE
if [ $? -ne 0 ]; then
echo "ERROR: Database $DB does not exist";
exit;
fi
read -p "Do you need to replace the domain on the db file? [y/n]" yn
case $yn in
[Yy]* )
read -p "Domain name to replace: " $FROM_DOMAIN
read -p "Replace domain name with: " $TO_DOMAIN
perl -pi -w -e "s#${FROM_DOMAIN}#${TO_DOMAIN}#g;" $OUTPUT_FILE;;
* ) ;;
esac
read -p "Gzip the file? [y/n]" yn
case $yn in
[Yy]* ) gzip $OUTPUT_FILE;;
* ) ;;
esac
echo "**** DB $1 EXPORTED ****"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment