|
1 | 1 | #!/usr/bin/env bash
|
2 | 2 |
|
3 |
| -# Function to send error to Sentry |
4 |
| -send_error_to_sentry() { |
5 |
| - local error_message="$1" |
6 |
| - local db_name="$2" |
7 |
| - local status_code="$3" |
8 |
| - |
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}" |
21 |
| - fi |
| 3 | +# Initialize logging with timestamp |
| 4 | +log() { |
| 5 | + local level="$1"; |
| 6 | + local message="$2"; |
| 7 | + echo "[$(date '+%Y-%m-%d %H:%M:%S')] ${level}: ${message}"; |
22 | 8 | }
|
23 | 9 |
|
24 |
| -MYNAME="postgresql-backup-restore" |
25 |
| -STATUS=0 |
| 10 | +# Sentry reporting with validation and backwards compatibility |
| 11 | +error_to_sentry() { |
| 12 | + local error_message="$1"; |
| 13 | + local db_name="$2"; |
| 14 | + local status_code="$3"; |
26 | 15 |
|
27 |
| -echo "${MYNAME}: backup: Started" |
| 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 |
28 | 21 |
|
29 |
| -echo "${MYNAME}: Backing up ${DB_NAME}" |
| 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 |
30 | 27 |
|
31 |
| -start=$(date +%s) |
32 |
| -$(PGPASSWORD=${DB_USERPASSWORD} pg_dump --host=${DB_HOST} --username=${DB_USER} --create --clean ${DB_OPTIONS} --dbname=${DB_NAME} > /tmp/${DB_NAME}.sql) || STATUS=$? |
33 |
| -end=$(date +%s) |
| 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; |
| 40 | +} |
| 41 | + |
| 42 | +MYNAME="postgresql-backup-restore"; |
| 43 | +STATUS=0; |
| 44 | + |
| 45 | +log "INFO" "${MYNAME}: backup: Started"; |
| 46 | +log "INFO" "${MYNAME}: Backing up ${DB_NAME}"; |
| 47 | + |
| 48 | +start=$(date +%s); |
| 49 | +$(PGPASSWORD=${DB_USERPASSWORD} pg_dump --host=${DB_HOST} --username=${DB_USER} --create --clean ${DB_OPTIONS} --dbname=${DB_NAME} > /tmp/${DB_NAME}.sql) || STATUS=$?; |
| 50 | +end=$(date +%s); |
34 | 51 |
|
35 | 52 | if [ $STATUS -ne 0 ]; then
|
36 |
| - 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}" |
39 |
| - exit $STATUS |
| 53 | + error_message="${MYNAME}: FATAL: Backup of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."; |
| 54 | + log "ERROR" "${error_message}"; |
| 55 | + error_to_sentry "${error_message}" "${DB_NAME}" "${STATUS}"; |
| 56 | + exit $STATUS; |
40 | 57 | else
|
41 |
| - echo "${MYNAME}: Backup of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds, ($(stat -c %s /tmp/${DB_NAME}.sql) bytes)." |
| 58 | + log "INFO" "${MYNAME}: Backup of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds, ($(stat -c %s /tmp/${DB_NAME}.sql) bytes)."; |
42 | 59 | fi
|
43 | 60 |
|
44 |
| -start=$(date +%s) |
45 |
| -gzip -f /tmp/${DB_NAME}.sql || STATUS=$? |
46 |
| -end=$(date +%s) |
| 61 | +# Compression |
| 62 | +start=$(date +%s); |
| 63 | +gzip -f /tmp/${DB_NAME}.sql || STATUS=$?; |
| 64 | +end=$(date +%s); |
47 | 65 |
|
48 | 66 | if [ $STATUS -ne 0 ]; then
|
49 |
| - 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}" |
52 |
| - exit $STATUS |
| 67 | + error_message="${MYNAME}: FATAL: Compressing backup of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."; |
| 68 | + log "ERROR" "${error_message}"; |
| 69 | + error_to_sentry "${error_message}" "${DB_NAME}" "${STATUS}"; |
| 70 | + exit $STATUS; |
53 | 71 | else
|
54 |
| - echo "${MYNAME}: Compressing backup of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds." |
| 72 | + log "INFO" "${MYNAME}: Compressing backup of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds."; |
55 | 73 | fi
|
56 | 74 |
|
57 |
| -start=$(date +%s) |
58 |
| -s3cmd put /tmp/${DB_NAME}.sql.gz ${S3_BUCKET} || STATUS=$? |
59 |
| -end=$(date +%s) |
60 |
| - |
| 75 | +# S3 Upload |
| 76 | +start=$(date +%s); |
| 77 | +s3cmd put /tmp/${DB_NAME}.sql.gz ${S3_BUCKET} || STATUS=$?; |
| 78 | +end=$(date +%s); |
61 | 79 | if [ $STATUS -ne 0 ]; then
|
62 |
| - 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}" |
65 |
| - exit $STATUS |
| 80 | + error_message="${MYNAME}: FATAL: Copy backup to ${S3_BUCKET} of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."; |
| 81 | + log "ERROR" "${error_message}"; |
| 82 | + error_to_sentry "${error_message}" "${DB_NAME}" "${STATUS}"; |
| 83 | + exit $STATUS; |
66 | 84 | else
|
67 |
| - echo "${MYNAME}: Copy backup to ${S3_BUCKET} of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds." |
| 85 | + log "INFO" "${MYNAME}: Copy backup to ${S3_BUCKET} of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds."; |
68 | 86 | fi
|
69 | 87 |
|
| 88 | +# Backblaze B2 Upload |
70 | 89 | if [ "${B2_BUCKET}" != "" ]; then
|
71 |
| - start=$(date +%s) |
| 90 | + start=$(date +%s); |
72 | 91 | s3cmd \
|
73 |
| - --access_key=${B2_APPLICATION_KEY_ID} \ |
74 |
| - --secret_key=${B2_APPLICATION_KEY} \ |
75 |
| - --host=${B2_HOST} \ |
76 |
| - --host-bucket='%(bucket)s.'"${B2_HOST}" \ |
77 |
| - put /tmp/${DB_NAME}.sql.gz s3://${B2_BUCKET}/${DB_NAME}.sql.gz |
78 |
| - STATUS=$? |
79 |
| - end=$(date +%s) |
| 92 | + --access_key=${B2_APPLICATION_KEY_ID} \ |
| 93 | + --secret_key=${B2_APPLICATION_KEY} \ |
| 94 | + --host=${B2_HOST} \ |
| 95 | + --host-bucket='%(bucket)s.'"${B2_HOST}" \ |
| 96 | + put /tmp/${DB_NAME}.sql.gz s3://${B2_BUCKET}/${DB_NAME}.sql.gz; |
| 97 | + STATUS=$?; |
| 98 | + end=$(date +%s); |
80 | 99 | if [ $STATUS -ne 0 ]; then
|
81 |
| - 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}" |
84 |
| - exit $STATUS |
| 100 | + 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."; |
| 101 | + log "ERROR" "${error_message}"; |
| 102 | + error_to_sentry "${error_message}" "${DB_NAME}" "${STATUS}"; |
| 103 | + exit $STATUS; |
85 | 104 | else
|
86 |
| - echo "${MYNAME}: Copy backup to Backblaze B2 bucket ${B2_BUCKET} of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds." |
| 105 | + log "INFO" "${MYNAME}: Copy backup to Backblaze B2 bucket ${B2_BUCKET} of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds."; |
87 | 106 | fi
|
88 | 107 | fi
|
89 | 108 |
|
90 |
| -echo "${MYNAME}: backup: Completed" |
| 109 | +echo "postgresql-backup-restore: backup: Completed"; |
| 110 | + |
| 111 | +log "INFO" "${MYNAME}: backup: Completed"; |
| 112 | + |
| 113 | +exit $STATUS; |
0 commit comments