MySQL db importer script

Public

Imports the sql dump to the database from command line. Handles the gzipped archive too.

Get raw version
bash
  1. #! /bin/bash
  2.  
  3. # Init variables
  4. user="";
  5. pass="";
  6. db="";
  7. host="localhost";
  8.  
  9. echo "Drop all tables...";
  10. mysqldump -h$host -u$user -p$pass --add-drop-table --no-data $db | grep ^DROP | mysql -h$host -u$user -p$pass $db;
  11. echo "All tables dropped!";
  12.  
  13. if [ $# -eq 1 ]
  14. then
  15.  
  16. importFile=$(basename $1 .gz);
  17. if [ $importFile != $1 ]
  18. then
  19. echo "Unzipping file...";
  20. gunzip $1;
  21. echo "The file is unzipped.";
  22. fi
  23.  
  24. echo "Start importing...";
  25. mysql -h$host -u$user -p$pass $db < $importFile;
  26. echo "Import finished!";
  27. fi
  28.  
  29. exit 0;