Skip to content

Commit 7fa4459

Browse files
committed
Fix for Sentry
1 parent fc21306 commit 7fa4459

2 files changed

Lines changed: 95 additions & 29 deletions

File tree

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ RUN apk update \
1212
postgresql14-client \
1313
py3-magic \
1414
py3-dateutil \
15-
s3cmd
15+
s3cmd \
16+
curl \
17+
jq
1618

1719
COPY application/ /data/
1820
WORKDIR /data

application/backup.sh

Lines changed: 92 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,108 @@
11
#!/usr/bin/env bash
22

3+
# Initialize logging with timestamp
4+
log() {
5+
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
6+
}
7+
8+
# Generate UUID v4
9+
generate_uuid() {
10+
if [ -f /proc/sys/kernel/random/uuid ]; then
11+
cat /proc/sys/kernel/random/uuid
12+
else
13+
date +%s%N | sha256sum | head -c 32
14+
fi
15+
}
16+
17+
# Parse Sentry DSN
18+
parse_sentry_dsn() {
19+
local dsn=$1
20+
# Extract components using basic string manipulation
21+
local project_id=$(echo "$dsn" | sed 's/.*\///')
22+
local key=$(echo "$dsn" | sed 's|https://||' | sed 's/@.*//')
23+
local host=$(echo "$dsn" | sed 's|https://[^@]*@||' | sed 's|/.*||')
24+
echo "$project_id|$key|$host"
25+
}
26+
327
# Function to send error to Sentry
4-
send_error_to_sentry() {
28+
error_to_sentry() {
529
local error_message="$1"
630
local db_name="$2"
731
local status_code="$3"
832

9-
if [ -n "${SENTRY_DSN}" ]; then
10-
wget -q --header="Content-Type: application/json" \
11-
--post-data="{
12-
\"message\": \"${error_message}\",
13-
\"level\": \"error\",
14-
\"extra\": {
15-
\"database\": \"${db_name}\",
16-
\"status_code\": \"${status_code}\",
17-
\"hostname\": \"$(hostname)\"
18-
}
19-
}" \
20-
-O - "${SENTRY_DSN}"
33+
# Check if SENTRY_DSN is set
34+
if [ -z "${SENTRY_DSN:-}" ]; then
35+
log "ERROR: SENTRY_DSN not set"
36+
return 1
2137
fi
38+
39+
# Parse DSN
40+
local dsn_parts=($(parse_sentry_dsn "$SENTRY_DSN" | tr '|' ' '))
41+
local project_id="${dsn_parts[0]}"
42+
local key="${dsn_parts[1]}"
43+
local host="${dsn_parts[2]}"
44+
45+
# Generate event ID and timestamp
46+
local event_id=$(generate_uuid)
47+
local timestamp=$(date -u +"%Y-%m-%dT%H:%M:%S.000Z")
48+
49+
# Create JSON payload
50+
local payload=$(cat <<EOF
51+
{
52+
"event_id": "${event_id}",
53+
"timestamp": "${timestamp}",
54+
"level": "error",
55+
"message": "${error_message}",
56+
"logger": "postgresql-backup",
57+
"platform": "bash",
58+
"environment": "production",
59+
"tags": {
60+
"database": "${db_name}",
61+
"status_code": "${status_code}",
62+
"host": "$(hostname)"
63+
},
64+
"extra": {
65+
"script_path": "$0",
66+
"timestamp": "${timestamp}"
67+
}
68+
}
69+
EOF
70+
)
71+
72+
# Send to Sentry
73+
local response
74+
response=$(curl -s -X POST \
75+
"https://${host}/api/${project_id}/store/" \
76+
-H "Content-Type: application/json" \
77+
-H "X-Sentry-Auth: Sentry sentry_version=7, sentry_key=${key}, sentry_client=bash-script/1.0" \
78+
-d "${payload}" 2>&1)
79+
80+
if [ $? -ne 0 ]; then
81+
log "ERROR: Failed to send event to Sentry: ${response}"
82+
return 1
83+
fi
84+
85+
log "Error event sent to Sentry: ${error_message}"
2286
}
2387

2488
MYNAME="postgresql-backup-restore"
2589
STATUS=0
2690

27-
echo "${MYNAME}: backup: Started"
91+
log "${MYNAME}: backup: Started"
2892

29-
echo "${MYNAME}: Backing up ${DB_NAME}"
93+
log "${MYNAME}: Backing up ${DB_NAME}"
3094

3195
start=$(date +%s)
3296
$(PGPASSWORD=${DB_USERPASSWORD} pg_dump --host=${DB_HOST} --username=${DB_USER} --create --clean ${DB_OPTIONS} --dbname=${DB_NAME} > /tmp/${DB_NAME}.sql) || STATUS=$?
3397
end=$(date +%s)
3498

3599
if [ $STATUS -ne 0 ]; then
36100
error_message="${MYNAME}: FATAL: Backup of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
37-
echo "${error_message}"
38-
send_error_to_sentry "${error_message}" "${STATUS}" "${DB_NAME}"
101+
log "${error_message}"
102+
error_to_sentry "${error_message}" "${DB_NAME}" "${STATUS}"
39103
exit $STATUS
40104
else
41-
echo "${MYNAME}: Backup of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds, ($(stat -c %s /tmp/${DB_NAME}.sql) bytes)."
105+
log "${MYNAME}: Backup of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds, ($(stat -c %s /tmp/${DB_NAME}.sql) bytes)."
42106
fi
43107

44108
start=$(date +%s)
@@ -47,11 +111,11 @@ end=$(date +%s)
47111

48112
if [ $STATUS -ne 0 ]; then
49113
error_message="${MYNAME}: FATAL: Compressing backup of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
50-
echo "${error_message}"
51-
send_error_to_sentry "${error_message}" "${STATUS}" "${DB_NAME}"
114+
log "${error_message}"
115+
error_to_sentry "${error_message}" "${DB_NAME}" "${STATUS}"
52116
exit $STATUS
53117
else
54-
echo "${MYNAME}: Compressing backup of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds."
118+
log "${MYNAME}: Compressing backup of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds."
55119
fi
56120

57121
start=$(date +%s)
@@ -60,11 +124,11 @@ end=$(date +%s)
60124

61125
if [ $STATUS -ne 0 ]; then
62126
error_message="${MYNAME}: FATAL: Copy backup to ${S3_BUCKET} of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
63-
echo "${error_message}"
64-
send_error_to_sentry "${error_message}" "${STATUS}" "${DB_NAME}"
127+
log "${error_message}"
128+
error_to_sentry "${error_message}" "${DB_NAME}" "${STATUS}"
65129
exit $STATUS
66130
else
67-
echo "${MYNAME}: Copy backup to ${S3_BUCKET} of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds."
131+
log "${MYNAME}: Copy backup to ${S3_BUCKET} of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds."
68132
fi
69133

70134
if [ "${B2_BUCKET}" != "" ]; then
@@ -79,12 +143,12 @@ if [ "${B2_BUCKET}" != "" ]; then
79143
end=$(date +%s)
80144
if [ $STATUS -ne 0 ]; then
81145
error_message="${MYNAME}: FATAL: Copy backup to Backblaze B2 bucket ${B2_BUCKET} of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
82-
echo "${error_message}"
83-
send_error_to_sentry "${error_message}" "${STATUS}"
146+
log "${error_message}"
147+
error_to_sentry "${error_message}" "${DB_NAME}" "${STATUS}"
84148
exit $STATUS
85149
else
86-
echo "${MYNAME}: Copy backup to Backblaze B2 bucket ${B2_BUCKET} of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds."
150+
log "${MYNAME}: Copy backup to Backblaze B2 bucket ${B2_BUCKET} of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds."
87151
fi
88152
fi
89153

90-
echo "${MYNAME}: backup: Completed"
154+
log "${MYNAME}: backup: Completed"

0 commit comments

Comments
 (0)