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.

102 lines
2.3 KiB

  1. set -eux ## important for unbound variable ?
  2. ## Require these to be set
  3. # MYSQL_ROOT_PASSWORD=
  4. # MYSQL_CONTAINER=
  5. [ "${MYSQL_ROOT_PASSWORD}" ] || {
  6. echo "Error: you must set \$MYSQL_ROOT_PASSWORD prior to running this script." >&2
  7. exit 1
  8. }
  9. [ "${MYSQL_CONTAINER}" ] || {
  10. echo "Error: you must set \$MYSQL_CONTAINER prior to running this script." >&2
  11. exit 1
  12. }
  13. ##
  14. ## Init, to setup passwordless connection to mysql
  15. ##
  16. type -p mysql >/dev/null || {
  17. case $(lsb_release -is) in
  18. Debian)
  19. case $(lsb_release -rs) in
  20. 10)
  21. apt-get install -y default-mysql-client </dev/null
  22. ;;
  23. *)
  24. apt-get install -y mysql-client </dev/null
  25. ;;
  26. esac
  27. ;;
  28. Ubuntu)
  29. apt-get install -y mysql-client </dev/null
  30. ;;
  31. esac
  32. }
  33. if ! [ -e "/root/.my.cnf" ]; then
  34. cat <<EOF > ~/.my.cnf
  35. [client]
  36. password=${MYSQL_ROOT_PASSWORD}
  37. EOF
  38. chmod 600 ~/.my.cnf
  39. fi
  40. ##
  41. ## installation of the mysql-backup script
  42. ##
  43. apt-get install -y kal-shlib-{core,pretty,common} </dev/null
  44. ln -sf "${PWD}/resources/bin/mysql-backup" /usr/local/sbin/mysql-backup
  45. ##
  46. ## Connection to cron
  47. ##
  48. depends cron
  49. cat <<EOF > /etc/cron.d/mysql-backup
  50. SHELL=/bin/bash
  51. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  52. 0 * * * * root /usr/local/sbin/mysql-backup --host \$(docker-ip "$MYSQL_CONTAINER" 2>/dev/null | sed -r 's/ +/ /g' | cut -f 3 -d " ") | logger -t mysql-backup
  53. EOF
  54. ##
  55. ## Connection with backup
  56. ##
  57. if type -p mirror-dir >/dev/null 2>&1; then
  58. [ -d "/etc/mirror-dir" ] || {
  59. echo "'mirror-dir' is installed but no '/etc/mirror-dir' was found." >&2
  60. exit 1
  61. }
  62. depends shyaml
  63. if ! sources=$(shyaml get-values default.sources < /etc/mirror-dir/config.yml); then
  64. echo "Couldn't query 'default.sources' in '/etc/mirror-dir/config.yml'." >&2
  65. exit 1
  66. fi
  67. if ! echo "$sources" | grep "^/var/backups/mysql$" 2>/dev/null; then
  68. sed -i '/sources:/a\ - /var/backups/mysql' /etc/mirror-dir/config.yml
  69. cat <<EOF >> /etc/mirror-dir/config.yml
  70. /var/backups/mysql:
  71. exclude:
  72. - "/*.inprogress"
  73. EOF
  74. fi
  75. else
  76. echo "warn: 'mirror-dir' not installed, backup won't be sent" >&2
  77. fi