admin-utils.sh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
  2. #
  3. # Permission to use, copy, modify, and/or distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. # AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. # PERFORMANCE OF THIS SOFTWARE.
  14. # This is an utility script that is being included by other scripts.
  15. # There are two ways of calling this method.
  16. # mysql_execute SQL_QUERY - This call is simpler, but requires db_user,
  17. # db_password and db_name variables to be bet.
  18. # mysql_execute SQL_QUERY PARAM1 PARAM2 .. PARAMN - Additional parameters
  19. # may be specified. They are passed directly to mysql. This one is
  20. # more convenient to use if the script didn't parse db_user db_password
  21. # and db_name.
  22. #
  23. # It saves mysql command exit status both to the env variable _ADMIN_STATUS
  24. # as well as returning it as $? to the caller.
  25. mysql_execute() {
  26. if [ $# -gt 1 ]; then
  27. QUERY="$1"
  28. shift
  29. mysql -N -B $* -e "${QUERY}"
  30. retcode=$?
  31. else
  32. mysql -N -B --user=$db_user --password=$db_password -e "${1}" $db_name
  33. retcode="$?"
  34. fi
  35. return $retcode
  36. }
  37. mysql_version() {
  38. mysql_execute "SELECT CONCAT(version,\".\",minor) FROM schema_version" "$@"
  39. return $?
  40. }