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.

86 lines
1.9 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 || apt-get install -y mysql-client </dev/null
  17. if ! [ -e "/root/.my.cnf" ]; then
  18. cat <<EOF > ~/.my.cnf
  19. [client]
  20. password=${MYSQL_ROOT_PASSWORD}
  21. EOF
  22. chmod 600 ~/.my.cnf
  23. fi
  24. ##
  25. ## installation of the mysql-backup script
  26. ##
  27. apt-get install -y kal-shlib-{core,pretty,common} </dev/null
  28. cp -f resources/bin/mysql-backup /usr/local/sbin/mysql-backup
  29. ##
  30. ## Connection to cron
  31. ##
  32. depends cron
  33. cat <<EOF > /etc/cron.d/mysql-backup
  34. SHELL=/bin/bash
  35. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  36. 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
  37. EOF
  38. ##
  39. ## Connection with backup
  40. ##
  41. if type -p mirror-dir >/dev/null 2>&1; then
  42. [ -d "/etc/mirror-dir" ] || {
  43. echo "'mirror-dir' is installed but no '/etc/mirror-dir' was found." >&2
  44. exit 1
  45. }
  46. depends shyaml
  47. if ! sources=$(shyaml get-values default.sources < /etc/mirror-dir/config.yml); then
  48. echo "Couldn't query 'default.sources' in '/etc/mirror-dir/config.yml'." >&2
  49. exit 1
  50. fi
  51. if ! echo "$sources" | grep "^/var/backups/mysql$" 2>/dev/null; then
  52. sed -i '/sources:/a\ - /var/backups/mysql' /etc/mirror-dir/config.yml
  53. cat <<EOF >> /etc/mirror-dir/config.yml
  54. /var/backups/mysql:
  55. exclude:
  56. - "/*.inprogress"
  57. EOF
  58. fi
  59. else
  60. echo "warn: 'mirror-dir' not installed, backup won't be sent" >&2
  61. fi