From 03375b53514270636831423b231170a71cb9acc6 Mon Sep 17 00:00:00 2001 From: dweinholz Date: Mon, 7 Oct 2024 08:45:02 +0200 Subject: [PATCH] changed to GET request to Kuma EP --- backup/notify_uptime_kuma.sh | 16 ++++++++--- backup/rotate_backup.sh | 0 mongodb/install-packages.sh | 0 mongodb/mongodb-backup.sh | 6 +++-- mysql/install-packages.sh | 0 mysql/mysql-backup.sh | 6 +++-- postgresql/install-packages.sh | 0 postgresql/postgresql-backup.sh | 48 +++++++++++++++++++++++++++------ postgresql/postgresql-cron | 0 prepare-cron.sh | 0 s3/s3_backup.sh | 6 ++++- s3/s3_backup_rotation.sh | 0 s3/s3_notify_uptime_kuma.sh | 19 +++++++++---- 13 files changed, 79 insertions(+), 22 deletions(-) mode change 100644 => 100755 backup/notify_uptime_kuma.sh mode change 100644 => 100755 backup/rotate_backup.sh mode change 100644 => 100755 mongodb/install-packages.sh mode change 100644 => 100755 mongodb/mongodb-backup.sh mode change 100644 => 100755 mysql/install-packages.sh mode change 100644 => 100755 mysql/mysql-backup.sh mode change 100644 => 100755 postgresql/install-packages.sh mode change 100644 => 100755 postgresql/postgresql-backup.sh mode change 100644 => 100755 postgresql/postgresql-cron mode change 100644 => 100755 prepare-cron.sh mode change 100644 => 100755 s3/s3_backup.sh mode change 100644 => 100755 s3/s3_backup_rotation.sh mode change 100644 => 100755 s3/s3_notify_uptime_kuma.sh diff --git a/backup/notify_uptime_kuma.sh b/backup/notify_uptime_kuma.sh old mode 100644 new mode 100755 index 4ea3fff..d366cde --- a/backup/notify_uptime_kuma.sh +++ b/backup/notify_uptime_kuma.sh @@ -1,10 +1,18 @@ #!/bin/bash -KUMA_STATUS_ENDPOINT=${KUMA_STATUS_ENDPOINT} +# Set KUMA_STATUS_ENDPOINT variable from environment or default to empty string +KUMA_STATUS_ENDPOINT=${KUMA_STATUS_ENDPOINT:-} if [ -z "$KUMA_STATUS_ENDPOINT" ]; then - echo "KUMA_STATUS_ENDPOINT is not set. Skipping." + echo "Error: KUMA_STATUS_ENDPOINT is not set. Skipping." else - curl -X POST \\ - $KUMA_STATUS_ENDPOINT + # Use curl to make a GET request to the status endpoint + response=$(curl -s -X GET "$KUMA_STATUS_ENDPOINT") + + # Check if the request was successful + if [ $? -eq 0 ]; then + echo "Status endpoint responded successfully: $response" + else + echo "Error: Failed to push status from $KUMA_STATUS_ENDPOINT. Status code: $?" + fi fi \ No newline at end of file diff --git a/backup/rotate_backup.sh b/backup/rotate_backup.sh old mode 100644 new mode 100755 diff --git a/mongodb/install-packages.sh b/mongodb/install-packages.sh old mode 100644 new mode 100755 diff --git a/mongodb/mongodb-backup.sh b/mongodb/mongodb-backup.sh old mode 100644 new mode 100755 index 6612c1b..0751bd8 --- a/mongodb/mongodb-backup.sh +++ b/mongodb/mongodb-backup.sh @@ -27,6 +27,8 @@ if [ ! -s "$FILE" ] || [ $(stat -c%s "$FILE") -lt $MIN_SIZE ]; then exit 1 fi -/notify_uptime_kuma.sh || log "Failed to send notification" - +# Send a notification using the notify_uptime_kuma.sh script +if ! ./notify_uptime_kuma.sh; then + log "Failed to send notification" +fi log "Backup completed successfully" \ No newline at end of file diff --git a/mysql/install-packages.sh b/mysql/install-packages.sh old mode 100644 new mode 100755 diff --git a/mysql/mysql-backup.sh b/mysql/mysql-backup.sh old mode 100644 new mode 100755 index dfbd7d4..f48d10c --- a/mysql/mysql-backup.sh +++ b/mysql/mysql-backup.sh @@ -18,6 +18,8 @@ if [ ! -s "$FILE" ] || [ $(stat -c%s "$FILE") -lt $MIN_SIZE ]; then exit 1 fi -/notify_uptime_kuma.sh || log "Failed to send notification" - +# Send a notification using the notify_uptime_kuma.sh script +if ! ./notify_uptime_kuma.sh; then + log "Failed to send notification" +fi log "Backup completed successfully" \ No newline at end of file diff --git a/postgresql/install-packages.sh b/postgresql/install-packages.sh old mode 100644 new mode 100755 diff --git a/postgresql/postgresql-backup.sh b/postgresql/postgresql-backup.sh old mode 100644 new mode 100755 index 890bfb5..aeafbbc --- a/postgresql/postgresql-backup.sh +++ b/postgresql/postgresql-backup.sh @@ -1,22 +1,51 @@ #!/bin/sh +# Define a logging function log() { echo "[$(date +"%Y-%m-%d %H:%M:%S")] - $1" } + +# Set up an error trap to exit the script on any error trap 'log "Error occurred, exiting script"; exit 1' ERR -touch ~/.pgpass -log "Creating ~/.pgpass file" -echo ${POSTGRES_HOST}:${POSTGRES_PORT}:${POSTGRES_DB}:${POSTGRES_USER}:${POSTGRES_PASSWORD} > ~/.pgpass -chmod 600 ~/.pgpass +# Create the ~/.pgpass file if it doesn't exist +if [ ! -f "~/.pgpass" ]; then + touch ~/.pgpass + log "Created ~/.pgpass file" +fi + +# Set the PostgreSQL connection details as environment variables +POSTGRES_HOST=${POSTGRES_HOST:-} +POSTGRES_PORT=${POSTGRES_PORT:-5432} # default to 5432 if not set +POSTGRES_DB=${POSTGRES_DB:-} +POSTGRES_USER=${POSTGRES_USER:-} +POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-} + +# Check that all required environment variables are set +if [ -z "$POSTGRES_HOST" ] || [ -z "$POSTGRES_DB" ] || [ -z "$POSTGRES_USER" ] || [ -z "$POSTGRES_PASSWORD" ]; then + log "Error: Missing PostgreSQL connection details, exiting script" + exit 1 +fi +# Set the PGPASSFILE environment variable to point to the ~/.pgpass file export PGPASSFILE='/root/.pgpass' +# Write the PostgreSQL connection details to the ~/.pgpass file +echo "${POSTGRES_HOST}:${POSTGRES_PORT}:${POSTGRES_DB}:${POSTGRES_USER}:${POSTGRES_PASSWORD}" > ~/.pgpass + +# Set permissions on the ~/.pgpass file +chmod 600 ~/.pgpass + +# Create a timestamp for the backup file name NOW=$(date '+%y-%m-%d-%H%M') -FILE=/etc/backup/${POSTGRES_DB}-${NOW}.dump.gz -log "Create Backup $FILE" -pg_dump -h ${POSTGRES_HOST} -U ${POSTGRES_USER} ${POSTGRES_DB} -Z 9 > $FILE +# Define the backup file path and name +FILE="/etc/backup/${POSTGRES_DB}-${NOW}.dump.gz" + +log "Creating Backup $FILE" + +# Perform the PostgreSQL database dump +pg_dump -h "${POSTGRES_HOST}" -U "${POSTGRES_USER}" "${POSTGRES_DB}" -Z 9 > "$FILE" # Check if the backup file is not empty and has a reasonable size MIN_SIZE=$((1024 * 10)) # 10KB minimum size @@ -25,6 +54,9 @@ if [ ! -s "$FILE" ] || [ $(stat -c%s "$FILE") -lt $MIN_SIZE ]; then exit 1 fi -/notify_uptime_kuma.sh || log "Failed to send notification" +# Send a notification using the notify_uptime_kuma.sh script +if ! ./notify_uptime_kuma.sh; then + log "Failed to send notification" +fi log "Backup completed successfully" \ No newline at end of file diff --git a/postgresql/postgresql-cron b/postgresql/postgresql-cron old mode 100644 new mode 100755 diff --git a/prepare-cron.sh b/prepare-cron.sh old mode 100644 new mode 100755 diff --git a/s3/s3_backup.sh b/s3/s3_backup.sh old mode 100644 new mode 100755 index 0d51a83..7e28eba --- a/s3/s3_backup.sh +++ b/s3/s3_backup.sh @@ -90,4 +90,8 @@ find "$S3_CONFIGS_PATH" -type f -name "*.cfg" | while read -r env_data; do rm -f "$tmp_conf" done -/notify_uptime_kuma.sh || log "Failed to send notification" \ No newline at end of file + +# Send a notification using the s3_notify_uptime_kuma.sh script +if ! ./s3_notify_uptime_kuma.sh; then + log "Failed to send notification" +fi \ No newline at end of file diff --git a/s3/s3_backup_rotation.sh b/s3/s3_backup_rotation.sh old mode 100644 new mode 100755 diff --git a/s3/s3_notify_uptime_kuma.sh b/s3/s3_notify_uptime_kuma.sh old mode 100644 new mode 100755 index cadb5b9..ecf4e43 --- a/s3/s3_notify_uptime_kuma.sh +++ b/s3/s3_notify_uptime_kuma.sh @@ -1,10 +1,19 @@ #!/bin/bash -S3_KUMA_STATUS_ENDPOINT=${S3_KUMA_STATUS_ENDPOINT} +# Set S3_KUMA_STATUS_ENDPOINT variable from environment or default to empty string +S3_KUMA_STATUS_ENDPOINT=${S3_KUMA_STATUS_ENDPOINT:-} -if [ -z "$KUMA_STATUS_ENDPOINT" ]; then - echo "S3_KUMA_STATUS_ENDPOINT is not set. Skipping." +if [ -z "$S3_KUMA_STATUS_ENDPOINT" ]; then + echo "Error: S3_KUMA_STATUS_ENDPOINT is not set. Skipping." else - curl -X POST \\ - $S3_KUMA_STATUS_ENDPOINT + # Use curl to make a POST request to the status endpoint + response=$(curl -s -X POST \\ + "$S3_KUMA_STATUS_ENDPOINT") + + # Check if the request was successful + if [ $? -eq 0 ]; then + echo "Status endpoint responded successfully: $response" + else + echo "Error: Failed to send status update to $S3_KUMA_STATUS_ENDPOINT. Status code: $?" + fi fi \ No newline at end of file