Skip to content

Commit dfc3022

Browse files
authored
Merge pull request #38 from silinternational/feature-sentrycli-bash
Added sentrycli bash
2 parents ad9e467 + 9c0c3ec commit dfc3022

File tree

2 files changed

+61
-39
lines changed

2 files changed

+61
-39
lines changed

Dockerfile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ RUN apk update \
1212
py3-dateutil \
1313
py3-six \
1414
s3cmd \
15-
curl
16-
17-
# Install Sentry CLI as a separate RUN command
15+
curl \
16+
jq
17+
18+
# Install sentry-cli
1819
RUN curl -sL https://sentry.io/get-cli/ | bash
1920

2021
COPY application/ /data/

application/backup.sh

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,135 @@
11
#!/usr/bin/env bash
22

3-
# Send error to Sentry
3+
# Initialize logging with timestamp
4+
log() {
5+
local level="${1:-INFO}"
6+
local message="$2"
7+
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ${level}: ${message}"
8+
}
9+
10+
# Sentry reporting with validation and backwards compatibility
411
error_to_sentry() {
512
local error_message="$1"
613
local db_name="$2"
714
local status_code="$3"
815

9-
if [ ! -z "${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}"
21-
fi
16+
# Check if SENTRY_DSN is configured - ensures backup continues
17+
if [ -z "${SENTRY_DSN:-}" ]; then
18+
log "DEBUG" "Sentry logging skipped - SENTRY_DSN not configured"
19+
return 0
20+
fi
21+
22+
# Validate SENTRY_DSN format
23+
if ! [[ "${SENTRY_DSN}" =~ ^https://[^@]+@[^/]+/[0-9]+$ ]]; then
24+
log "WARN" "Invalid SENTRY_DSN format - Sentry logging will be skipped"
25+
return 0
26+
fi
27+
28+
# Attempt to send event to Sentry
29+
if sentry-cli send-event \
30+
--message "${error_message}" \
31+
--level error \
32+
--tag "database:${db_name}" \
33+
--tag "status:${status_code}"; then
34+
log "DEBUG" "Successfully sent error to Sentry - Message: ${error_message}, Database: ${db_name}, Status: ${status_code}"
35+
else
36+
log "WARN" "Failed to send error to Sentry, but continuing backup process"
37+
fi
38+
39+
return 0
2240
}
2341

2442
STATUS=0
2543

26-
echo "mysql-backup-restore: backup: Started"
44+
log "INFO" "mysql-backup-restore: backup: Started"
2745

2846
for dbName in ${DB_NAMES}; do
29-
echo "mysql-backup-restore: Backing up ${dbName}"
47+
log "INFO" "mysql-backup-restore: Backing up ${dbName}"
3048

3149
start=$(date +%s)
3250
mysqldump -h ${MYSQL_HOST} -u ${MYSQL_USER} -p"${MYSQL_PASSWORD}" ${MYSQL_DUMP_ARGS} ${dbName} > /tmp/${dbName}.sql
3351
STATUS=$?
3452
end=$(date +%s)
3553

3654
if [ $STATUS -ne 0 ]; then
37-
echo "*** START DEBUGGING FOR NON-ZERO STATUS ***"
55+
log "ERROR" "*** START DEBUGGING FOR NON-ZERO STATUS ***"
3856

3957
# Display update
4058
uptime
41-
echo
59+
log "INFO" ""
4260

4361
# display free drive space (in megabytes)
4462
df -m
45-
echo
63+
log "INFO" ""
4664

4765
# display free memory in megabytes
4866
free -m
49-
echo
67+
log "INFO" ""
5068

5169
# display swap information
5270
swapon
53-
echo
71+
log "INFO" ""
5472

55-
echo "*** END DEBUGGING FOR NON-ZERO STATUS ***"
73+
log "ERROR" "*** END DEBUGGING FOR NON-ZERO STATUS ***"
5674

5775
error_message="MySQL backup failed for database ${dbName}"
5876
error_to_sentry "$error_message" "$dbName" "$STATUS"
59-
echo "mysql-backup-restore: FATAL: Backup of ${dbName} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
77+
log "ERROR" "mysql-backup-restore: FATAL: Backup of ${dbName} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
6078
exit $STATUS
6179
else
62-
echo "mysql-backup-restore: Backup of ${dbName} completed in $(expr ${end} - ${start}) seconds, ($(stat -c %s /tmp/${dbName}.sql) bytes)."
80+
log "INFO" "mysql-backup-restore: Backup of ${dbName} completed in $(expr ${end} - ${start}) seconds, ($(stat -c %s /tmp/${dbName}.sql) bytes)."
6381
fi
6482

83+
# Compression
6584
start=$(date +%s)
6685
gzip -f /tmp/${dbName}.sql
6786
STATUS=$?
6887
end=$(date +%s)
6988
if [ $STATUS -ne 0 ]; then
7089
error_message="Compression failed for database ${dbName} backup"
7190
error_to_sentry "$error_message" "$dbName" "$STATUS"
72-
echo "mysql-backup-restore: FATAL: Compressing backup of ${dbName} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
91+
log "ERROR" "mysql-backup-restore: FATAL: Compressing backup of ${dbName} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
7392
exit $STATUS
7493
else
75-
echo "mysql-backup-restore: Compressing backup of ${dbName} completed in $(expr ${end} - ${start}) seconds."
94+
log "INFO" "mysql-backup-restore: Compressing backup of ${dbName} completed in $(expr ${end} - ${start}) seconds."
7695
fi
7796

97+
# S3 Upload
7898
start=$(date +%s)
7999
s3cmd put /tmp/${dbName}.sql.gz ${S3_BUCKET}
80100
STATUS=$?
81101
end=$(date +%s)
82102
if [ $STATUS -ne 0 ]; then
83103
error_message="S3 copy failed for database ${dbName} backup"
84104
error_to_sentry "$error_message" "$dbName" "$STATUS"
85-
echo "mysql-backup-restore: FATAL: Copy backup to ${S3_BUCKET} of ${dbName} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
105+
log "ERROR" "mysql-backup-restore: FATAL: Copy backup to ${S3_BUCKET} of ${dbName} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
86106
exit $STATUS
87107
else
88-
echo "mysql-backup-restore: Copy backup to ${S3_BUCKET} of ${dbName} completed in $(expr ${end} - ${start}) seconds."
108+
log "INFO" "mysql-backup-restore: Copy backup to ${S3_BUCKET} of ${dbName} completed in $(expr ${end} - ${start}) seconds."
89109
fi
90110

111+
# Backblaze B2 Upload (Optional)
91112
if [ "${B2_BUCKET}" != "" ]; then
92113
start=$(date +%s)
93114
s3cmd \
94-
--access_key=${B2_APPLICATION_KEY_ID} \
95-
--secret_key=${B2_APPLICATION_KEY} \
96-
--host=${B2_HOST} \
97-
--host-bucket='%(bucket)s.'"${B2_HOST}" \
98-
put /tmp/${dbName}.sql.gz s3://${B2_BUCKET}/${dbName}.sql.gz
115+
--access_key=${B2_APPLICATION_KEY_ID} \
116+
--secret_key=${B2_APPLICATION_KEY} \
117+
--host=${B2_HOST} \
118+
--host-bucket='%(bucket)s.'"${B2_HOST}" \
119+
put /tmp/${dbName}.sql.gz s3://${B2_BUCKET}/${dbName}.sql.gz
99120
STATUS=$?
100121
end=$(date +%s)
101122
if [ $STATUS -ne 0 ]; then
102123
error_message="Backblaze B2 copy failed for database ${dbName} backup"
103124
error_to_sentry "$error_message" "$dbName" "$STATUS"
104-
echo "mysql-backup-restore: FATAL: Copy backup to Backblaze B2 bucket ${B2_BUCKET} of ${dbName} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
125+
log "ERROR" "mysql-backup-restore: FATAL: Copy backup to Backblaze B2 bucket ${B2_BUCKET} of ${dbName} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
105126
exit $STATUS
106127
else
107-
echo "mysql-backup-restore: Copy backup to Backblaze B2 bucket ${B2_BUCKET} of ${dbName} completed in $(expr ${end} - ${start}) seconds."
128+
log "INFO" "mysql-backup-restore: Copy backup to Backblaze B2 bucket ${B2_BUCKET} of ${dbName} completed in $(expr ${end} - ${start}) seconds."
108129
fi
109130
fi
110-
111131
done
112132

113133
echo "mysql-backup-restore: backup: Completed"
134+
114135
exit $STATUS

0 commit comments

Comments
 (0)