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.

105 lines
2.4 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|11|12)
  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. echo "Error: could not install mysql client" >&2
  33. exit 1
  34. }
  35. }
  36. if ! [ -e "/root/.my.cnf" ]; then
  37. cat <<EOF > ~/.my.cnf
  38. [client]
  39. password=${MYSQL_ROOT_PASSWORD}
  40. EOF
  41. chmod 600 ~/.my.cnf
  42. fi
  43. ##
  44. ## installation of the mysql-backup script
  45. ##
  46. apt-get install -y kal-shlib-{core,pretty,common} </dev/null
  47. ln -sf "${PWD}/resources/bin/mysql-backup" /usr/local/sbin/mysql-backup
  48. ##
  49. ## Connection to cron
  50. ##
  51. depends cron
  52. cat <<EOF > /etc/cron.d/mysql-backup
  53. SHELL=/bin/bash
  54. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  55. 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
  56. EOF
  57. ##
  58. ## Connection with backup
  59. ##
  60. if type -p mirror-dir >/dev/null 2>&1; then
  61. [ -d "/etc/mirror-dir" ] || {
  62. echo "'mirror-dir' is installed but no '/etc/mirror-dir' was found." >&2
  63. exit 1
  64. }
  65. depends shyaml
  66. if ! sources=$(shyaml get-values default.sources < /etc/mirror-dir/config.yml); then
  67. echo "Couldn't query 'default.sources' in '/etc/mirror-dir/config.yml'." >&2
  68. exit 1
  69. fi
  70. if ! echo "$sources" | grep "^/var/backups/mysql$" 2>/dev/null; then
  71. sed -i '/sources:/a\ - /var/backups/mysql' /etc/mirror-dir/config.yml
  72. cat <<EOF >> /etc/mirror-dir/config.yml
  73. /var/backups/mysql:
  74. exclude:
  75. - "/*.inprogress"
  76. EOF
  77. fi
  78. else
  79. echo "warn: 'mirror-dir' not installed, backup won't be sent" >&2
  80. fi