Skip to content

Instantly share code, notes, and snippets.

@lukewertz
Created April 8, 2014 15:13
Show Gist options
  • Save lukewertz/10140355 to your computer and use it in GitHub Desktop.
Save lukewertz/10140355 to your computer and use it in GitHub Desktop.
Do some mysql stuff with options to clear drupal cache dumps before inserting.
#!/bin/bash
mysql_user='root'
dbname=$1
sql_file=$2
cc=false
cont=true
# Create the database if it doesn't exist
mysql -u $mysql_user -e 'CREATE DATABASE IF NOT EXISTS '$dbname';'
TABLES=$(mysql -u $mysql_user $dbname -e 'show tables' | grep -v '^Tables');
if [ -n "$TABLES" ]
then
while true; do
read -p "Database has tables. Drop and continue? " tables_yn
case $tables_yn in
[Yy]* ) $(mysql -u $mysql_user -e 'DROP DATABASE '$dbname'; CREATE DATABASE '$dbname';'); break;;
[Nn]* ) unset $cont; cont=false; break;;
* ) echo "Please answer yes or no.";;
esac
done
fi
if [ "$cont" = true ]; then
# Clear cache?
while true; do
read -p "Would you like to clear caches before inserting? " cc_yn
case $cc_yn in
[Yy]* ) sed '/^INSERT INTO `cache_/d' $sql_file > /tmp/cc.sql; unset sql_file; sql_file='/tmp/cc.sql'; break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
# Do the import
echo "Importing the sql file"
mysql -u $mysql_user $dbname < $sql_file
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment