You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
5.1KB

  1. #!/bin/bash
  2. # @name: database_functions.sh
  3. # @creation_date: 2022-11-02
  4. # @license: The MIT License <https://opensource.org/licenses/MIT>
  5. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  6. # @purpose: Runs database functions for the ExPub Compendium
  7. # @acknowledgements:
  8. # https://www.redhat.com/sysadmin/arguments-options-bash-scripts
  9. ############################################################
  10. # subprograms #
  11. ############################################################
  12. License()
  13. {
  14. echo 'Copyright 2022 Simon Bowie <ad7588@coventry.ac.uk>'
  15. echo
  16. echo 'Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:'
  17. echo
  18. echo 'The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.'
  19. echo
  20. echo 'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.'
  21. }
  22. Help()
  23. {
  24. # Display Help
  25. echo "This script performs database functions for the ExPub Compendium"
  26. echo
  27. echo "Syntax: database_functions.sh [-l|h|e|i|c|v|d]"
  28. echo "options:"
  29. echo "l Print the MIT License notification."
  30. echo "h Print this Help."
  31. echo "e Export whole database."
  32. echo "i Import whole database."
  33. echo "c Export single table as tab-delimited txt."
  34. echo "v Import tab-delimited txt file to table."
  35. echo "d Drop table."
  36. echo
  37. }
  38. Export()
  39. {
  40. docker exec -it $CONTAINER mysqldump --single-transaction -u $USERNAME -p$PASSWORD $DATABASE > $EXPORT_DIRECTORY/$EXPORT_SQL_FILENAME`date +"%Y%m%d"`.sql
  41. }
  42. Import()
  43. {
  44. docker cp $IMPORT_SQL_FILE $CONTAINER:/tmp/import_file
  45. docker exec -i $CONTAINER mysql -u $USERNAME -p$PASSWORD $DATABASE < /tmp/import_file
  46. }
  47. Table_export()
  48. {
  49. docker exec -it $CONTAINER bash -c "mysql -u $USERNAME -p$PASSWORD $DATABASE --batch -e 'SELECT * FROM $TABLE'" > $EXPORT_TXT_DIRECTORY/$EXPORT_TXT_FILENAME.txt
  50. }
  51. Table_import()
  52. {
  53. docker cp $IMPORT_TXT_FILE $CONTAINER:/tmp/import_file
  54. docker exec -i $CONTAINER bash -c "mysql -u $USERNAME -p$PASSWORD $DATABASE -e 'LOAD DATA LOCAL INFILE '\''/tmp/import_file'\'' REPLACE INTO TABLE $TABLE FIELDS TERMINATED BY '\''\t'\'' LINES TERMINATED BY '\''\n'\'' IGNORE 1 ROWS;'"
  55. }
  56. Drop_table()
  57. {
  58. docker exec -i $CONTAINER bash -c "mysql -u $USERNAME -p$PASSWORD $DATABASE -e 'DROP TABLE IF EXISTS $TABLE;'"
  59. }
  60. ############################################################
  61. ############################################################
  62. # main program #
  63. ############################################################
  64. ############################################################
  65. # set variables
  66. CONTAINER=mariadb
  67. DATABASE=toolkit
  68. USERNAME=xxxxxxxx
  69. PASSWORD=xxxxxxxx
  70. EXPORT_DIRECTORY="./db_exports"
  71. EXPORT_SQL_FILENAME=toolkit_db_
  72. EXPORT_TXT_FILENAME=$2`date +"%Y%m%d"`
  73. # error message for no flags
  74. if (( $# == 0 )); then
  75. Help
  76. exit 1
  77. fi
  78. # get the options
  79. while getopts ":hleicvd" flag; do
  80. case $flag in
  81. l) # display License
  82. License
  83. exit;;
  84. h) # display Help
  85. Help
  86. exit;;
  87. e) # export whole database
  88. Export
  89. exit;;
  90. i) # import database from file
  91. if [ -z "$2" ]
  92. then
  93. echo "-i requires a file as an argument"
  94. echo
  95. echo "Syntax: database_functions.sh -i [file]"
  96. else
  97. IMPORT_SQL_FILE=$2
  98. Import
  99. exit 1
  100. fi;;
  101. c) # export single table as tab-delimited txt
  102. if [ -z "$2" ]
  103. then
  104. echo "-c requires a table name as an argument"
  105. echo
  106. echo "Syntax: database_functions.sh -c [table name]"
  107. else
  108. TABLE=$2
  109. Table_export
  110. exit 1
  111. fi;;
  112. v) # import single tab-delimited txt to table
  113. if [ -z "$2" ] || [ -z "$3" ]
  114. then
  115. echo "-v requires a table name as an argument and the file to be imported"
  116. echo
  117. echo "Syntax: database_functions.sh -v [table name] [file]"
  118. else
  119. TABLE=$2
  120. IMPORT_TXT_FILE=$3
  121. Table_import
  122. exit 1
  123. fi;;
  124. d) # drop table
  125. if [ -z "$2" ]
  126. then
  127. echo "-d requires a table name as an argument"
  128. echo
  129. echo "Syntax: database_functions.sh -d [table name]"
  130. else
  131. TABLE=$2
  132. Drop_table
  133. exit 1
  134. fi;;
  135. \?) # Invalid option
  136. Help
  137. exit;;
  138. esac
  139. done