fork 0k-charms
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.

50 lines
1.8 KiB

  1. #!/bin/bash
  2. ## When writing relation script, remember:
  3. ## - they should be idempotents
  4. ## - they can be launched while the dockers is already up
  5. ## - they are launched from the host
  6. ## - the target of the link is launched first, and get a chance to ``relation-set``
  7. ## - both side of the scripts get to use ``relation-get``.
  8. set -e
  9. ## XXXvlab: should use container name here so that it could support
  10. ## multiple postgres
  11. label=pg-backup-$SERVICE_NAME
  12. DST=$CONFIGSTORE/$TARGET_SERVICE_NAME/etc/cron/$label
  13. schedule=$(relation-get schedule)
  14. if ! echo "$schedule" | egrep '^\s*(([0-9/,*-]+\s+){4,4}[0-9/,*-]+|@[a-z]+)\s*$' >/dev/null 2>&1; then
  15. err "Unrecognized schedule '$schedule'."
  16. exit 1
  17. fi
  18. ## XXXvlab: how is this meant to works if there's multiples postgres (about PGHOST=?) ?
  19. ## Warning: using '\' in heredoc will be removed in the final cron file, which
  20. ## is totally wanted: cron does not support multilines.
  21. exclude_dbs=$(relation-get exclude-dbs 2>/dev/null) || true
  22. exclude_dbs=$(echo "$exclude_dbs" | shyaml get-values 2>/dev/null |
  23. nspc) || true
  24. ## Warning: 'docker -v' will use HOST directory even if launched from
  25. ## 'cron' container.
  26. file_put "$DST" <<EOF
  27. $schedule root lock $label -D -p 10 -c "\
  28. docker run --rm \
  29. -e PGHOST=\$POSTGRES_PORT_5432_TCP_ADDR \
  30. -e exclude_dbs=\"$exclude_dbs\" \
  31. -v /root/.pgpass:/root/.pgpass \
  32. -v \"$BASE_CHARM_PATH/resources/bin/pg-backup:/usr/sbin/pg-backup\" \
  33. -v \"$SERVICE_DATASTORE/var/backups/pg:/var/backups/pg\" \
  34. --entrypoint pg-backup \
  35. \"$DOCKER_BASE_IMAGE\"" 2>&1 | ts '\%F \%T \%Z' >> /var/log/cron/pg-backup_script.log
  36. EOF
  37. chmod +x "$DST"
  38. config-add "\
  39. $MASTER_TARGET_SERVICE_NAME:
  40. links:
  41. - $MASTER_BASE_SERVICE_NAME
  42. "