45 lines
1.6 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. . lib/common
  9. set -e
  10. ## XXXvlab: should use container name here so that it could support
  11. ## multiple postgres
  12. label=${SERVICE_NAME}-pg-backup
  13. DST=$CONFIGSTORE/$TARGET_SERVICE_NAME/etc/cron/$label
  14. schedule=$(relation-get schedule)
  15. if ! echo "$schedule" | egrep '^\s*(([0-9/,*-]+\s+){4,4}[0-9/,*-]+|@[a-z]+)\s*$' >/dev/null 2>&1; then
  16. err "Unrecognized schedule '$schedule'."
  17. exit 1
  18. fi
  19. exclude_dbs=$(relation-get exclude-dbs 2>/dev/null) || true
  20. exclude_dbs=$(echo "$exclude_dbs" | shyaml get-values 2>/dev/null |
  21. nspc) || true
  22. ## Warning: 'docker -v' will use HOST directory even if launched from
  23. ## 'cron' container.
  24. file_put "$DST" <<EOF
  25. COMPOSE_LAUNCHER_OPTS=$COMPOSE_LAUNCHER_OPTS
  26. $schedule root lock $label -D -p 10 -c "\
  27. docker run --rm \
  28. -e PGHOST=${SERVICE_NAME} \
  29. --network ${PROJECT_NAME}_default \
  30. -e exclude_dbs=\"$exclude_dbs\" \
  31. -v \"$LOCAL_DB_PASSFILE\":/root/.pgpass \
  32. -v \"$HOST_CHARM_STORE/${CHARM_REL_PATH#${CHARM_STORE}/}/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"