-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from silinternational/feature-sentrycli-bash
Added sentrycli bash
- Loading branch information
Showing
2 changed files
with
61 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,114 +1,135 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Send error to Sentry | ||
# Initialize logging with timestamp | ||
log() { | ||
local level="${1:-INFO}" | ||
local message="$2" | ||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ${level}: ${message}" | ||
} | ||
|
||
# Sentry reporting with validation and backwards compatibility | ||
error_to_sentry() { | ||
local error_message="$1" | ||
local db_name="$2" | ||
local status_code="$3" | ||
|
||
if [ ! -z "${SENTRY_DSN}" ]; then | ||
wget -q --header="Content-Type: application/json" \ | ||
--post-data="{ | ||
\"message\": \"${error_message}\", | ||
\"level\": \"error\", | ||
\"extra\": { | ||
\"database\": \"${db_name}\", | ||
\"status_code\": \"${status_code}\", | ||
\"hostname\": \"${HOSTNAME}\" | ||
} | ||
}" \ | ||
-O - "${SENTRY_DSN}" | ||
fi | ||
# Check if SENTRY_DSN is configured - ensures backup continues | ||
if [ -z "${SENTRY_DSN:-}" ]; then | ||
log "DEBUG" "Sentry logging skipped - SENTRY_DSN not configured" | ||
return 0 | ||
fi | ||
|
||
# Validate SENTRY_DSN format | ||
if ! [[ "${SENTRY_DSN}" =~ ^https://[^@]+@[^/]+/[0-9]+$ ]]; then | ||
log "WARN" "Invalid SENTRY_DSN format - Sentry logging will be skipped" | ||
return 0 | ||
fi | ||
|
||
# Attempt to send event to Sentry | ||
if sentry-cli send-event \ | ||
--message "${error_message}" \ | ||
--level error \ | ||
--tag "database:${db_name}" \ | ||
--tag "status:${status_code}"; then | ||
log "DEBUG" "Successfully sent error to Sentry - Message: ${error_message}, Database: ${db_name}, Status: ${status_code}" | ||
else | ||
log "WARN" "Failed to send error to Sentry, but continuing backup process" | ||
fi | ||
|
||
return 0 | ||
} | ||
|
||
STATUS=0 | ||
|
||
echo "mysql-backup-restore: backup: Started" | ||
log "INFO" "mysql-backup-restore: backup: Started" | ||
|
||
for dbName in ${DB_NAMES}; do | ||
echo "mysql-backup-restore: Backing up ${dbName}" | ||
log "INFO" "mysql-backup-restore: Backing up ${dbName}" | ||
|
||
start=$(date +%s) | ||
mysqldump -h ${MYSQL_HOST} -u ${MYSQL_USER} -p"${MYSQL_PASSWORD}" ${MYSQL_DUMP_ARGS} ${dbName} > /tmp/${dbName}.sql | ||
STATUS=$? | ||
end=$(date +%s) | ||
|
||
if [ $STATUS -ne 0 ]; then | ||
echo "*** START DEBUGGING FOR NON-ZERO STATUS ***" | ||
log "ERROR" "*** START DEBUGGING FOR NON-ZERO STATUS ***" | ||
|
||
# Display update | ||
uptime | ||
echo | ||
log "INFO" "" | ||
|
||
# display free drive space (in megabytes) | ||
df -m | ||
echo | ||
log "INFO" "" | ||
|
||
# display free memory in megabytes | ||
free -m | ||
echo | ||
log "INFO" "" | ||
|
||
# display swap information | ||
swapon | ||
echo | ||
log "INFO" "" | ||
|
||
echo "*** END DEBUGGING FOR NON-ZERO STATUS ***" | ||
log "ERROR" "*** END DEBUGGING FOR NON-ZERO STATUS ***" | ||
|
||
error_message="MySQL backup failed for database ${dbName}" | ||
error_to_sentry "$error_message" "$dbName" "$STATUS" | ||
echo "mysql-backup-restore: FATAL: Backup of ${dbName} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." | ||
log "ERROR" "mysql-backup-restore: FATAL: Backup of ${dbName} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." | ||
exit $STATUS | ||
else | ||
echo "mysql-backup-restore: Backup of ${dbName} completed in $(expr ${end} - ${start}) seconds, ($(stat -c %s /tmp/${dbName}.sql) bytes)." | ||
log "INFO" "mysql-backup-restore: Backup of ${dbName} completed in $(expr ${end} - ${start}) seconds, ($(stat -c %s /tmp/${dbName}.sql) bytes)." | ||
fi | ||
|
||
# Compression | ||
start=$(date +%s) | ||
gzip -f /tmp/${dbName}.sql | ||
STATUS=$? | ||
end=$(date +%s) | ||
if [ $STATUS -ne 0 ]; then | ||
error_message="Compression failed for database ${dbName} backup" | ||
error_to_sentry "$error_message" "$dbName" "$STATUS" | ||
echo "mysql-backup-restore: FATAL: Compressing backup of ${dbName} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." | ||
log "ERROR" "mysql-backup-restore: FATAL: Compressing backup of ${dbName} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." | ||
exit $STATUS | ||
else | ||
echo "mysql-backup-restore: Compressing backup of ${dbName} completed in $(expr ${end} - ${start}) seconds." | ||
log "INFO" "mysql-backup-restore: Compressing backup of ${dbName} completed in $(expr ${end} - ${start}) seconds." | ||
fi | ||
|
||
# S3 Upload | ||
start=$(date +%s) | ||
s3cmd put /tmp/${dbName}.sql.gz ${S3_BUCKET} | ||
STATUS=$? | ||
end=$(date +%s) | ||
if [ $STATUS -ne 0 ]; then | ||
error_message="S3 copy failed for database ${dbName} backup" | ||
error_to_sentry "$error_message" "$dbName" "$STATUS" | ||
echo "mysql-backup-restore: FATAL: Copy backup to ${S3_BUCKET} of ${dbName} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." | ||
log "ERROR" "mysql-backup-restore: FATAL: Copy backup to ${S3_BUCKET} of ${dbName} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds." | ||
exit $STATUS | ||
else | ||
echo "mysql-backup-restore: Copy backup to ${S3_BUCKET} of ${dbName} completed in $(expr ${end} - ${start}) seconds." | ||
log "INFO" "mysql-backup-restore: Copy backup to ${S3_BUCKET} of ${dbName} completed in $(expr ${end} - ${start}) seconds." | ||
fi | ||
|
||
# Backblaze B2 Upload (Optional) | ||
if [ "${B2_BUCKET}" != "" ]; then | ||
start=$(date +%s) | ||
s3cmd \ | ||
--access_key=${B2_APPLICATION_KEY_ID} \ | ||
--secret_key=${B2_APPLICATION_KEY} \ | ||
--host=${B2_HOST} \ | ||
--host-bucket='%(bucket)s.'"${B2_HOST}" \ | ||
put /tmp/${dbName}.sql.gz s3://${B2_BUCKET}/${dbName}.sql.gz | ||
--access_key=${B2_APPLICATION_KEY_ID} \ | ||
--secret_key=${B2_APPLICATION_KEY} \ | ||
--host=${B2_HOST} \ | ||
--host-bucket='%(bucket)s.'"${B2_HOST}" \ | ||
put /tmp/${dbName}.sql.gz s3://${B2_BUCKET}/${dbName}.sql.gz | ||
STATUS=$? | ||
end=$(date +%s) | ||
if [ $STATUS -ne 0 ]; then | ||
error_message="Backblaze B2 copy failed for database ${dbName} backup" | ||
error_to_sentry "$error_message" "$dbName" "$STATUS" | ||
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." | ||
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." | ||
exit $STATUS | ||
else | ||
echo "mysql-backup-restore: Copy backup to Backblaze B2 bucket ${B2_BUCKET} of ${dbName} completed in $(expr ${end} - ${start}) seconds." | ||
log "INFO" "mysql-backup-restore: Copy backup to Backblaze B2 bucket ${B2_BUCKET} of ${dbName} completed in $(expr ${end} - ${start}) seconds." | ||
fi | ||
fi | ||
|
||
done | ||
|
||
echo "mysql-backup-restore: backup: Completed" | ||
|
||
exit $STATUS |