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.

49 lines
1.7 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 | xargs echo) || true
  23. ## Warning: 'docker -v' will use HOST directory even if launched from
  24. ## 'cron' container.
  25. file_put "$DST" <<EOF
  26. $schedule root lock $label -D -p 10 -c "\
  27. docker run --rm \
  28. -e PGHOST=\$POSTGRES_PORT_5432_TCP_ADDR \
  29. -e exclude_dbs=\"$exclude_dbs\" \
  30. -v /root/.pgpass:/root/.pgpass \
  31. -v \"$BASE_CHARM_PATH/resources/bin/pg-backup:/usr/sbin/pg-backup\" \
  32. -v \"$SERVICE_DATASTORE/var/backups/pg:/var/backups/pg\" \
  33. --entrypoint pg-backup \
  34. \"$DOCKER_BASE_IMAGE\"" 2>&1 | ts '\%F \%T \%Z' >> /var/log/cron/pg-backup_script.log
  35. EOF
  36. chmod +x "$DST"
  37. config-add "\
  38. $MASTER_TARGET_SERVICE_NAME:
  39. links:
  40. - $MASTER_BASE_SERVICE_NAME
  41. "