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.

82 lines
3.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]"
  28. echo "options:"
  29. echo "l Print the MIT License notification."
  30. echo "h Print this Help."
  31. echo "e Export."
  32. echo "i Import."
  33. echo
  34. }
  35. Export()
  36. {
  37. docker exec -it $CONTAINER mysqldump --single-transaction -u $USERNAME -p$PASSWORD $DATABASE > $EXPORT_FILENAME`date +"%Y%m%d"`.sql
  38. }
  39. Import()
  40. {
  41. docker exec -i $CONTAINER mysql -u $USERNAME -p$PASSWORD $DATABASE < $DIRECTORY/$IMPORT_FILENAME
  42. }
  43. ############################################################
  44. ############################################################
  45. # Main program #
  46. ############################################################
  47. ############################################################
  48. # Set variables
  49. CONTAINER=mariadb
  50. DATABASE=toolkit
  51. USERNAME=xxxxxxxx
  52. PASSWORD=xxxxxxxx
  53. EXPORT_FILENAME=toolkit_db_
  54. DIRECTORY="/Users/ad7588/Downloads"
  55. IMPORT_FILENAME=toolkit_db.sql
  56. # Get the options
  57. while getopts ":hlimzaespxdw" option; do
  58. case $option in
  59. l) # display License
  60. License
  61. exit;;
  62. h) # display Help
  63. Help
  64. exit;;
  65. e) # export whole database
  66. Export
  67. exit;;
  68. i) # import database from file
  69. Import
  70. exit;;
  71. \?) # Invalid option
  72. echo "Error: Invalid option"
  73. exit;;
  74. esac
  75. done