Skip to content

Instantly share code, notes, and snippets.

@thomaschaplin
Last active April 23, 2025 12:12
Show Gist options
  • Select an option

  • Save thomaschaplin/f6a7795ad3c5b0ae505b2c61dc420937 to your computer and use it in GitHub Desktop.

Select an option

Save thomaschaplin/f6a7795ad3c5b0ae505b2c61dc420937 to your computer and use it in GitHub Desktop.
A simple Bash script to export all incidents from PagerDuty month by month basis.

PagerDuty Incident Exporter

A simple Bash script to export all incidents from PagerDuty month by month basis.

It:

  • Queries the PagerDuty Incidents API v2
  • Handles pagination (for large result sets)
  • Outputs one JSON file per month like pagerduty-incident-export-12-2021.json, pagerduty-incident-export-01-2022.json, etc.
  • Compatible with macOS (uses BSD date syntax)

⚙️ Prerequisites

  • Bash shell (default on macOS/Linux)
  • jq installed (brew install jq on macOS)
  • A valid PagerDuty API token with read access to incidents

🔧 Configuration

Open the script and update:

API_TOKEN="TOKEN_GOES_HERE"

Replace with your actual PagerDuty API token.


🚀 Usage

chmod +x export-incidents.sh
./export-incidents.sh

This will generate .json files in the same directory, one for each month in the range.


📦 Output Example

Each file will look like this:

[
  {
    "id": "PABC123",
    "title": "Database latency alert",
    "status": "resolved",
    "urgency": "high",
    "created_at": "2022-03-15T03:42:00Z",
    "service": "Primary Database"
  },
  ...
]

You can customize the fields extracted by modifying the jq command in the script.


🛑 Troubleshooting

Error: date: illegal option -- d → You're probably using macOS — this script is already compatible using date -v. Just make sure you're running it with the macOS-native shell (not inside Docker or a Linux container).

#!/bin/bash
API_TOKEN="TOKEN_GOES_HERE"
API_URL="https://api.pagerduty.com/incidents"
LIMIT=100
# Start and end date
START_YEAR=2021
START_MONTH=12
END_YEAR=2025
END_MONTH=4
YEAR=$START_YEAR
MONTH=$START_MONTH
while [ $YEAR -lt $END_YEAR ] || { [ $YEAR -eq $END_YEAR ] && [ $MONTH -le $END_MONTH ]; }; do
# Format month and next month
MONTH_PADDED=$(printf "%02d" $MONTH)
NEXT_MONTH=$(($MONTH + 1))
NEXT_YEAR=$YEAR
if [ $NEXT_MONTH -gt 12 ]; then
NEXT_MONTH=1
NEXT_YEAR=$(($YEAR + 1))
fi
NEXT_MONTH_PADDED=$(printf "%02d" $NEXT_MONTH)
SINCE="${YEAR}-${MONTH_PADDED}-01T00:00:00Z"
UNTIL=$(date -v+1m -j -f "%Y-%m" "${YEAR}-${MONTH_PADDED}" "+%Y-%m-01T00:00:00Z")
UNTIL=$(date -v-1d -j -f "%Y-%m-%dT%H:%M:%SZ" "$UNTIL" "+%Y-%m-%dT23:59:59Z")
FILE_NAME="pagerduty-incident-export-${MONTH_PADDED}-${YEAR}.json"
echo "Exporting incidents for $MONTH_PADDED/$YEAR -> $FILE_NAME"
offset=0
all_incidents="[]"
while : ; do
response=$(curl -s -G "$API_URL" \
-H "Authorization: Token token=$API_TOKEN" \
-H "Accept: application/vnd.pagerduty+json;version=2" \
-H "Content-Type: application/json" \
--data-urlencode "since=$SINCE" \
--data-urlencode "until=$UNTIL" \
--data-urlencode "limit=$LIMIT" \
--data-urlencode "offset=$offset")
incidents=$(echo "$response" | jq '.incidents')
all_incidents=$(echo "$all_incidents $incidents" | jq -s 'add')
more=$(echo "$response" | jq -r '.more')
if [ "$more" != "true" ]; then
break
fi
offset=$((offset + LIMIT))
done
echo "$all_incidents" | jq '.' > "$FILE_NAME"
# Move to next month
if [ $MONTH -eq 12 ]; then
MONTH=1
YEAR=$((YEAR + 1))
else
MONTH=$((MONTH + 1))
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment