Guest User

Untitled

a guest
Aug 9th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. #!/bin/bash
  2. ####### Usage check
  3. [[ $# -ne 5 ]] && echo -e "Please provide the SQL scripts directory, username, hostname, database and password nUSAGE: ./ecs_test.sh /directory username hostname dbname password" && exit
  4.  
  5. ####### access / store db information
  6. cd $1
  7. user=$2
  8. host=$3
  9. database=$4
  10. pass=$5
  11.  
  12. ######## DB Version store
  13. mysql -u $user -h $host -p$pass -D $database -e "SELECT version FROM versionTable" > dbvers.out
  14. CURRENT_DB_VERSION=`cat dbvers.out | grep -o '[0-9]+'`
  15. highest_upgrade_version=`ls $(pwd) | grep -Eo '[0-9]+' | sort -rn | head -n 1 | awk 'NR' | sed 's/^0*//'`
  16.  
  17. ######### create list of scripts and order them
  18. ls $(pwd) | grep .sql | sort -n >> scripts_list.txt
  19.  
  20. while [[ $CURRENT_DB_VERSION -lt $highest_upgrade_version || $CURRENT_DB_VERSION -eq $highest_upgrade_version ]]
  21. do
  22. next_script_to_execute=`grep -Eo $CURRENT_DB_VERSION scripts_list.txt | sort -n | head -n 1`
  23. if [[ $next_script_to_execute -gt $CURRENT_DB_VERSION || -z $next_script_to_execute ]]
  24. then
  25. ((CURRENT_DB_VERSION++))
  26. elif [[ $CURRENT_DB_VERSION -lt 9 ]]
  27. then
  28. for i in $(ls $(pwd) | sort -n| grep -E "^[0]" | grep $CURRENT_DB_VERSION| head -1);
  29. do mysql -u $user -h $host -p$pass -D $database < $i
  30. echo $i "is currently being executed"
  31. ((CURRENT_DB_VERSION++))
  32. done
  33. else
  34. for i in $(ls $(pwd) | sed 's/^[1-9]*+ //' | grep -E $CURRENT_DB_VERSION | sort -n | head -n 1); do mysql -u $user -h $host -p$pass -D $database < $i
  35. ((CURRENT_DB_VERSION++))
  36. echo $i "is currently being executed"
  37. done
  38. fi
  39. done
  40.  
  41. ((CURRENT_DB_VERSION--))
  42. echo "Current version of the Database is: "$CURRENT_DB_VERSION
  43. mysql -u $user -h $host -p$pass -D $database -e "UPDATE versionTable SET version = $CURRENT_DB_VERSION"
  44.  
  45. ### cleanup temp files
  46. rm -rf scripts_list.txt
  47. rm -rf dbvers.out
Add Comment
Please, Sign In to add comment