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.

127 lines
4.5KB

  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]"
  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
  36. }
  37. Export()
  38. {
  39. docker exec -it $CONTAINER mysqldump --single-transaction -u $USERNAME -p$PASSWORD $DATABASE > $EXPORT_SQL_FILENAME`date +"%Y%m%d"`.sql
  40. }
  41. Import()
  42. {
  43. docker exec -i $CONTAINER mysql -u $USERNAME -p$PASSWORD $DATABASE < $IMPORT_SQL_DIRECTORY/$IMPORT_SQL_FILENAME
  44. }
  45. Table_export()
  46. {
  47. docker exec -it $CONTAINER bash -c "mysql -u $USERNAME -p$PASSWORD $DATABASE --batch -e 'SELECT * FROM $TABLE'" > $EXPORT_TXT_DIRECTORY/$EXPORT_TXT_FILENAME.txt
  48. }
  49. Table_import()
  50. {
  51. docker cp $IMPORT_TXT_FILE $CONTAINER:/tmp/import_file
  52. 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;'"
  53. }
  54. ############################################################
  55. ############################################################
  56. # main program #
  57. ############################################################
  58. ############################################################
  59. # set variables
  60. CONTAINER=mariadb
  61. DATABASE=toolkit
  62. USERNAME=xxxxxxxx
  63. PASSWORD=xxxxxxxx
  64. EXPORT_SQL_FILENAME=toolkit_db_
  65. IMPORT_SQL_DIRECTORY="/Users/ad7588/Downloads"
  66. IMPORT_SQL_FILENAME=toolkit_db.sql
  67. EXPORT_TXT_DIRECTORY="./db_exports"
  68. EXPORT_TXT_FILENAME=$2`date +"%Y%m%d"`
  69. # error message for no flags
  70. if (( $# == 0 )); then
  71. Help
  72. exit 1
  73. fi
  74. # get the options
  75. while getopts ":hleicv" flag; do
  76. case $flag in
  77. l) # display License
  78. License
  79. exit;;
  80. h) # display Help
  81. Help
  82. exit;;
  83. e) # export whole database
  84. Export
  85. exit;;
  86. i) # import database from file
  87. Import
  88. exit;;
  89. c) # export single table as tab-delimited txt
  90. if [ -z "$2" ]
  91. then
  92. echo "-c requires a table name as an argument"
  93. echo
  94. echo "Syntax: database_functions.sh -c [table name]"
  95. else
  96. TABLE=$2
  97. Table_export
  98. exit 1
  99. fi;;
  100. v) # import single tab-delimited txt to table
  101. if [ -z "$2" ] || [ -z "$3" ]
  102. then
  103. echo "-v requires a table name as an argument and the file to be imported"
  104. echo
  105. echo "Syntax: database_functions.sh -v [table name] [file]"
  106. else
  107. TABLE=$2
  108. IMPORT_TXT_FILE=$3
  109. Table_import
  110. exit 1
  111. fi;;
  112. \?) # Invalid option
  113. Help
  114. exit;;
  115. esac
  116. done