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