Skip to content

Commit c193b54

Browse files
committed
Fixed paramenter usage
1 parent 7e026c1 commit c193b54

File tree

1 file changed

+57
-58
lines changed

1 file changed

+57
-58
lines changed

application/backup.sh

+57-58
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@
22

33
# Initialize logging with timestamp
44
log() {
5-
local level="${1:-INFO}"
6-
local message="$2"
7-
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ${level}: ${message}"
5+
local level="$1";
6+
local message="$2";
7+
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ${level}: ${message}";
88
}
99

1010
# Sentry reporting with validation and backwards compatibility
1111
error_to_sentry() {
12-
local error_message="$1"
13-
local db_name="$2"
14-
local status_code="$3"
12+
local error_message="$1";
13+
local db_name="$2";
14+
local status_code="$3";
1515

1616
# Check if SENTRY_DSN is configured - ensures backup continues
1717
if [ -z "${SENTRY_DSN:-}" ]; then
18-
log "DEBUG" "Sentry logging skipped - SENTRY_DSN not configured"
19-
return 0
18+
log "DEBUG" "Sentry logging skipped - SENTRY_DSN not configured";
19+
return 0;
2020
fi
2121

2222
# Validate SENTRY_DSN format
2323
if ! [[ "${SENTRY_DSN}" =~ ^https://[^@]+@[^/]+/[0-9]+$ ]]; then
24-
log "WARN" "Invalid SENTRY_DSN format - Sentry logging will be skipped"
25-
return 0
24+
log "WARN" "Invalid SENTRY_DSN format - Sentry logging will be skipped";
25+
return 0;
2626
fi
2727

2828
# Attempt to send event to Sentry
@@ -31,84 +31,83 @@ error_to_sentry() {
3131
--level error \
3232
--tag "database:${db_name}" \
3333
--tag "status:${status_code}"; then
34-
log "DEBUG" "Successfully sent error to Sentry - Message: ${error_message}, Database: ${db_name}, Status: ${status_code}"
34+
log "DEBUG" "Successfully sent error to Sentry - Message: ${error_message}, Database: ${db_name}, Status: ${status_code}";
3535
else
36-
log "WARN" "Failed to send error to Sentry, but continuing backup process"
36+
log "WARN" "Failed to send error to Sentry, but continuing backup process";
3737
fi
3838

39-
return 0
39+
return 0;
4040
}
4141

42-
MYNAME="postgresql-backup-restore"
43-
STATUS=0
42+
MYNAME="postgresql-backup-restore";
43+
STATUS=0;
4444

45-
log "${MYNAME}: backup: Started"
45+
log "INFO" "${MYNAME}: backup: Started";
46+
log "INFO" "${MYNAME}: Backing up ${DB_NAME}";
4647

47-
log "${MYNAME}: Backing up ${DB_NAME}"
48-
49-
start=$(date +%s)
50-
$(PGPASSWORD=${DB_USERPASSWORD} pg_dump --host=${DB_HOST} --username=${DB_USER} --create --clean ${DB_OPTIONS} --dbname=${DB_NAME} > /tmp/${DB_NAME}.sql) || STATUS=$?
51-
end=$(date +%s)
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);
5251

5352
if [ $STATUS -ne 0 ]; then
54-
error_message="${MYNAME}: FATAL: Backup of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
55-
log "${error_message}"
56-
error_to_sentry "${error_message}" "${DB_NAME}" "${STATUS}"
57-
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;
5857
else
59-
log "${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).";
6059
fi
6160

6261
# Compression
63-
start=$(date +%s)
64-
gzip -f /tmp/${DB_NAME}.sql || STATUS=$?
65-
end=$(date +%s)
62+
start=$(date +%s);
63+
gzip -f /tmp/${DB_NAME}.sql || STATUS=$?;
64+
end=$(date +%s);
6665

6766
if [ $STATUS -ne 0 ]; then
68-
error_message="${MYNAME}: FATAL: Compressing backup of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
69-
log "${error_message}"
70-
error_to_sentry "${error_message}" "${DB_NAME}" "${STATUS}"
71-
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;
7271
else
73-
log "${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.";
7473
fi
7574

7675
# S3 Upload
77-
start=$(date +%s)
78-
s3cmd put /tmp/${DB_NAME}.sql.gz ${S3_BUCKET} || STATUS=$?
79-
end=$(date +%s)
76+
start=$(date +%s);
77+
s3cmd put /tmp/${DB_NAME}.sql.gz ${S3_BUCKET} || STATUS=$?;
78+
end=$(date +%s);
8079
if [ $STATUS -ne 0 ]; then
81-
error_message="${MYNAME}: FATAL: Copy backup to ${S3_BUCKET} of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
82-
log "${error_message}"
83-
error_to_sentry "${error_message}" "${DB_NAME}" "${STATUS}"
84-
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;
8584
else
86-
log "${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.";
8786
fi
8887

8988
# Backblaze B2 Upload
9089
if [ "${B2_BUCKET}" != "" ]; then
91-
start=$(date +%s)
90+
start=$(date +%s);
9291
s3cmd \
93-
--access_key=${B2_APPLICATION_KEY_ID} \
94-
--secret_key=${B2_APPLICATION_KEY} \
95-
--host=${B2_HOST} \
96-
--host-bucket='%(bucket)s.'"${B2_HOST}" \
97-
put /tmp/${DB_NAME}.sql.gz s3://${B2_BUCKET}/${DB_NAME}.sql.gz
98-
STATUS=$?
99-
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);
10099
if [ $STATUS -ne 0 ]; then
101-
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."
102-
log "${error_message}"
103-
error_to_sentry "${error_message}" "${DB_NAME}" "${STATUS}"
104-
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;
105104
else
106-
log "${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.";
107106
fi
108107
fi
109108

110-
echo "postgresql-backup-restore: backup: Completed"
109+
echo "postgresql-backup-restore: backup: Completed";
111110

112-
log "${MYNAME}: backup: Completed"
111+
log "INFO" "${MYNAME}: backup: Completed";
113112

114-
exit $STATUS
113+
exit $STATUS;

0 commit comments

Comments
 (0)