From 997d8c5d02110244a55e6ee5824035dc42aad5e8 Mon Sep 17 00:00:00 2001 From: vktrrdk Date: Wed, 17 Jan 2024 09:36:39 +0000 Subject: [PATCH 01/14] fix(Dockerfile): Removed installation of pip, using binary of needed package instead --- Dockerfile | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 76c6dad..8d9c938 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,14 +5,10 @@ RUN apk add --update --no-cache \ bash \ gnupg \ fdupes \ - python3 \ - py3-pip \ + s3cmd \ busybox-extras \ ssmtp -RUN pip3 install --upgrade pip -RUN pip3 install s3cmd - RUN touch /var/log/cron.log COPY ./prepare-cron.sh /prepare-cron.sh From b545a60281901e80e38a20344748691a0e598368 Mon Sep 17 00:00:00 2001 From: dweinholz Date: Wed, 17 Jan 2024 10:47:19 +0100 Subject: [PATCH 02/14] Update build.yml --- .github/workflows/build.yml | 44 ++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 626c53a..d14f093 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -4,9 +4,6 @@ on: push: branches: - 'main' - # - 'staging' - # - 'dev' - # - 'hotfix/**' jobs: build: runs-on: ubuntu-latest @@ -43,3 +40,44 @@ jobs: file: Dockerfile push: true tags: quay.io/denbicloud/cron-backup:${{ steps.tag.outputs.TAG }} + + special_builds: + runs-on: ubuntu-latest + needs: build + strategy: + fail-fast: false + matrix: + type: [ postgresql, mysql, mongodb ] + steps: + + - name: Extract branch name + shell: bash + run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})" + id: extract_branch + + - name: Set tag + run: sed 's/\//-/g' <<< "::set-output name=TAG::${{ steps.extract_branch.outputs.branch }}" + id: tag + + - name: Get tag + run: echo "The selected tag is ${{ steps.tag.outputs.TAG }}" + + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Login to Quay.io + uses: docker/login-action@v3 + with: + registry: quay.io + username: ${{ secrets.QUAY_USERNAME }} + password: ${{ secrets.QUAY_TOKEN }} + - name: Build and publish image to Quay + uses: docker/build-push-action@v5 + env: + BASE_TAG: ${{ steps.tag.outputs.TAG }} + with: + file: ${{matrix.type}}/Dockerfile + context: ${{matrix.type}} + push: true + build-args: BASE_TAG + tags: quay.io/denbicloud/cron-backup:${{matrix.type}}-${{ steps.get_version.outputs.VERSION }} From 63c71be77ada0e18e800ff59b04da79e6dd15d26 Mon Sep 17 00:00:00 2001 From: dweinholz Date: Wed, 17 Jan 2024 10:54:02 +0100 Subject: [PATCH 03/14] Update build.yml --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d14f093..816bfce 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -80,4 +80,4 @@ jobs: context: ${{matrix.type}} push: true build-args: BASE_TAG - tags: quay.io/denbicloud/cron-backup:${{matrix.type}}-${{ steps.get_version.outputs.VERSION }} + tags: quay.io/denbicloud/cron-backup:${{matrix.type}}-${{ steps.get_version.outputs.TAG }} From 757f2f7b4924da3101bf49b80ea17e6da65f28f4 Mon Sep 17 00:00:00 2001 From: dweinholz Date: Wed, 17 Jan 2024 10:56:42 +0100 Subject: [PATCH 04/14] Update build.yml --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 816bfce..2f43804 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -61,7 +61,7 @@ jobs: - name: Get tag run: echo "The selected tag is ${{ steps.tag.outputs.TAG }}" - + - uses: actions/checkout@master - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 From 5f204417104008d0a51bf13fc73087c1e7e40715 Mon Sep 17 00:00:00 2001 From: dweinholz Date: Wed, 17 Jan 2024 10:58:35 +0100 Subject: [PATCH 05/14] Update build.yml --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2f43804..e79ed93 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -80,4 +80,4 @@ jobs: context: ${{matrix.type}} push: true build-args: BASE_TAG - tags: quay.io/denbicloud/cron-backup:${{matrix.type}}-${{ steps.get_version.outputs.TAG }} + tags: quay.io/denbicloud/cron-backup:${{matrix.type}}-${{ steps.tag.outputs.TAG }} From bfa63cd43a61c21c937a190d8dfd7ab9301b8e23 Mon Sep 17 00:00:00 2001 From: dweinholz Date: Thu, 19 Sep 2024 10:53:54 +0200 Subject: [PATCH 06/14] feat(Validation):now check min size for created backup added kuma endpoint env for status check --- Dockerfile | 4 ++++ README.md | 5 +++++ backup/notify_uptime_kuma.sh | 10 ++++++++++ mongodb/mongodb-backup.sh | 12 ++++++++++++ mysql/mysql-backup.sh | 12 ++++++++++++ postgresql/postgresql-backup.sh | 12 ++++++++++++ s3/s3_backup.sh | 4 +++- s3/s3_notify_uptime_kuma.sh | 10 ++++++++++ 8 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 backup/notify_uptime_kuma.sh create mode 100644 s3/s3_notify_uptime_kuma.sh diff --git a/Dockerfile b/Dockerfile index 4dadbc3..287e446 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,6 +7,7 @@ RUN apk add --update --no-cache \ fdupes \ python3 \ py3-pip \ + curl \ busybox-extras \ ssmtp @@ -18,10 +19,13 @@ RUN touch /var/log/cron.log COPY ./prepare-cron.sh /prepare-cron.sh COPY ./backup/rotate_backup.sh /rotate_backup.sh COPY ./backup/backup-cron /backup-cron +COPY ./backup/notify_uptime_kuma.sh /notify_uptime_kuma.sh + COPY ./s3/s3_backup.sh /s3_backup.sh COPY ./s3/s3_backup-cron /s3_backup-cron COPY ./s3/s3_backup_rotation.sh /s3_backup_rotation.sh COPY ./s3/s3_backup_rotation-cron /s3_backup_rotation-cron +COPY ./s3/s3_notify_uptime_kuma.sh /s3_notify_uptime_kuma.sh RUN chmod +x /prepare-cron.sh diff --git a/README.md b/README.md index e2a8f9a..909f6b9 100644 --- a/README.md +++ b/README.md @@ -30,9 +30,12 @@ base image with a shell script to prepare and run cron jobs. To use this you nee - S3_CONFIGS_PATH - Directory of the different site configs with variables see below (should be mounted) - S3_BACKUP_ROTATION_ENABLED - must to set to true to activate backup rotation in S3 - S3_BACKUP_ROTATION_TIME_LIMIT - expiration time in days for Backups - if rotation is enabled uploads older than this limit will be removed - (global - can be overwirtten per site.cfg) + - S3_KUMA_STATUS_ENDPOINT - when provided will be called after an successfull backup In addition, a cfg must be specified for each site to which the backups are to be pushed - with the following content [example](s3/configs/example.site.cfg): +6. An Endpoint can be provided for Status Updates for [Uptime Kuma](https://github.com/louislam/uptime-kuma). Following env variables must be set: + - KUMA_STATUS_ENDPOINT -- when provided will be called after an successfull backup ~~~Bash S3_HASHDIR=DIR #should be mounted - stores local checksum of pushed non-encrypted files (in S3 they are encrypted thus different checksum) S3_OBJECT_STORAGE_EP=SITE_SPECIFIC_OBJECT_STORAGE EP (e.g openstack.cebitec.uni-bielefeld.de:8080) @@ -52,10 +55,12 @@ Next use this image with your docker-compose.yml (here an example for a limesurv - ${general_PERSISTENT_PATH}backup/limesurvey:/etc/backup environment: - LIMESURVEY_DB_PASSWORD + - KUMA_STATUS_ENDPOINT - BACKUP_ROTATION_ENABLED=true - BACKUP_ROTATION_MAX_SIZE=10 - BACKUP_ROTATION_CUT_SIZE=5 - BACKUP_ROTATION_SIZE_TYPE=GiB + - S3_KUMA_STATUS_ENDPOINT - S3_BACKUP_ENABLED=true - S3_PATH=limesurvey - S3_CONFIGS_DIR=~/configs diff --git a/backup/notify_uptime_kuma.sh b/backup/notify_uptime_kuma.sh new file mode 100644 index 0000000..4ea3fff --- /dev/null +++ b/backup/notify_uptime_kuma.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +KUMA_STATUS_ENDPOINT=${KUMA_STATUS_ENDPOINT} + +if [ -z "$KUMA_STATUS_ENDPOINT" ]; then + echo "KUMA_STATUS_ENDPOINT is not set. Skipping." +else + curl -X POST \\ + $KUMA_STATUS_ENDPOINT +fi \ No newline at end of file diff --git a/mongodb/mongodb-backup.sh b/mongodb/mongodb-backup.sh index fcbd310..6612c1b 100644 --- a/mongodb/mongodb-backup.sh +++ b/mongodb/mongodb-backup.sh @@ -3,6 +3,7 @@ log() { echo "[$(date +"%Y-%m-%d %H:%M:%S")] - $1" } +trap 'log "Error occurred, exiting script"; exit 1' ERR NOW=$(date '+%y-%m-%d-%H%M') FILE="/etc/backup/${MONGODB_DB}-${NOW}.dump.gz" @@ -18,3 +19,14 @@ fi MONGODB_HOST=$(echo "$MONGODB_HOST" | cut -d: -f1) mongodump --archive="$FILE" --gzip --uri="$URI" + +# Check if the backup file is not empty and has a reasonable size +MIN_SIZE=$((1024 * 10)) # 10KB minimum size +if [ ! -s "$FILE" ] || [ $(stat -c%s "$FILE") -lt $MIN_SIZE ]; then + log "Backup file $FILE is too small (${MIN_SIZE}B required), aborting script" + exit 1 +fi + +/notify_uptime_kuma.sh || log "Failed to send notification" + +log "Backup completed successfully" \ No newline at end of file diff --git a/mysql/mysql-backup.sh b/mysql/mysql-backup.sh index bb13517..dfbd7d4 100644 --- a/mysql/mysql-backup.sh +++ b/mysql/mysql-backup.sh @@ -3,9 +3,21 @@ log() { echo "[$(date +"%Y-%m-%d %H:%M:%S")] - $1" } +trap 'log "Error occurred, exiting script"; exit 1' ERR NOW=$(date '+%y-%m-%d-%H%M') FILE=/etc/backup/${MYSQL_HOST}-${NOW}.sql.gz log "Create Backup $FILE" mysqldump -h ${MYSQL_HOST} -u ${MYSQL_USER} --password=${MYSQL_PASSWORD} --all-databases | gzip > $FILE + +# Check if the backup file is not empty and has a reasonable size +MIN_SIZE=$((1024 * 10)) # 10KB minimum size +if [ ! -s "$FILE" ] || [ $(stat -c%s "$FILE") -lt $MIN_SIZE ]; then + log "Backup file $FILE is too small (${MIN_SIZE}B required), aborting script" + exit 1 +fi + +/notify_uptime_kuma.sh || log "Failed to send notification" + +log "Backup completed successfully" \ No newline at end of file diff --git a/postgresql/postgresql-backup.sh b/postgresql/postgresql-backup.sh index d83a3aa..890bfb5 100644 --- a/postgresql/postgresql-backup.sh +++ b/postgresql/postgresql-backup.sh @@ -3,6 +3,7 @@ log() { echo "[$(date +"%Y-%m-%d %H:%M:%S")] - $1" } +trap 'log "Error occurred, exiting script"; exit 1' ERR touch ~/.pgpass log "Creating ~/.pgpass file" @@ -16,3 +17,14 @@ 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 + +# Check if the backup file is not empty and has a reasonable size +MIN_SIZE=$((1024 * 10)) # 10KB minimum size +if [ ! -s "$FILE" ] || [ $(stat -c%s "$FILE") -lt $MIN_SIZE ]; then + log "Backup file $FILE is too small (${MIN_SIZE}B required), aborting script" + exit 1 +fi + +/notify_uptime_kuma.sh || log "Failed to send notification" + +log "Backup completed successfully" \ No newline at end of file diff --git a/s3/s3_backup.sh b/s3/s3_backup.sh index 617021d..0d51a83 100644 --- a/s3/s3_backup.sh +++ b/s3/s3_backup.sh @@ -3,7 +3,7 @@ log() { echo "[$(date +"%Y-%m-%d %H:%M:%S")] - $1" } - +trap 'log "Error occurred, exiting script"; exit 1' ERR log "Starting backup script" basedir="/etc/backup" @@ -89,3 +89,5 @@ find "$S3_CONFIGS_PATH" -type f -name "*.cfg" | while read -r env_data; do log "Removing temp config and password files" rm -f "$tmp_conf" done + +/notify_uptime_kuma.sh || log "Failed to send notification" \ No newline at end of file diff --git a/s3/s3_notify_uptime_kuma.sh b/s3/s3_notify_uptime_kuma.sh new file mode 100644 index 0000000..cadb5b9 --- /dev/null +++ b/s3/s3_notify_uptime_kuma.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +S3_KUMA_STATUS_ENDPOINT=${S3_KUMA_STATUS_ENDPOINT} + +if [ -z "$KUMA_STATUS_ENDPOINT" ]; then + echo "S3_KUMA_STATUS_ENDPOINT is not set. Skipping." +else + curl -X POST \\ + $S3_KUMA_STATUS_ENDPOINT +fi \ No newline at end of file From 296aa345ec06c468168ee7fb8cf2bce7e2b67d37 Mon Sep 17 00:00:00 2001 From: dweinholz Date: Thu, 19 Sep 2024 10:58:37 +0200 Subject: [PATCH 07/14] pip brak system packages --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 287e446..a826e75 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,8 +11,8 @@ RUN apk add --update --no-cache \ busybox-extras \ ssmtp -RUN pip3 install --upgrade pip -RUN pip3 install s3cmd +RUN pip3 install --upgrade pip --break-system-packages. +RUN pip3 install s3cmd --break-system-packages. RUN touch /var/log/cron.log From dc0dd4c120c5a1b015d932b89aefe906593cc35e Mon Sep 17 00:00:00 2001 From: dweinholz Date: Thu, 19 Sep 2024 11:00:07 +0200 Subject: [PATCH 08/14] fixed typo --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index a826e75..ef39316 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,8 +11,8 @@ RUN apk add --update --no-cache \ busybox-extras \ ssmtp -RUN pip3 install --upgrade pip --break-system-packages. -RUN pip3 install s3cmd --break-system-packages. +RUN pip3 install --upgrade pip --break-system-packages +RUN pip3 install s3cmd --break-system-packages RUN touch /var/log/cron.log From 0da875118972f3589196ce6838f1f34367e2b344 Mon Sep 17 00:00:00 2001 From: dweinholz Date: Thu, 19 Sep 2024 11:04:55 +0200 Subject: [PATCH 09/14] also special builds for publish docker! --- .github/workflows/build.yml | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 77bd28b..8262010 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -43,3 +43,36 @@ jobs: file: Dockerfile push: true tags: quay.io/denbicloud/cron-backup:${{ steps.tag.outputs.TAG }} + + special_builds: + runs-on: ubuntu-latest + needs: build + strategy: + fail-fast: false + matrix: + type: [ postgresql, mysql, mongodb ] + steps: + + - name: Get the version + id: get_version + run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//} + - uses: actions/checkout@master + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Login to Quay.io + uses: docker/login-action@v3 + with: + registry: quay.io + username: ${{ secrets.QUAY_USERNAME }} + password: ${{ secrets.QUAY_TOKEN }} + - name: Build and publish image to Quay + uses: docker/build-push-action@v5 + env: + BASE_TAG: ${{ steps.get_version.outputs.VERSION }} + with: + file: ${{matrix.type}}/Dockerfile + context: ${{matrix.type}} + push: true + build-args: BASE_TAG + tags: quay.io/denbicloud/cron-backup:${{matrix.type}}-${{ steps.get_version.outputs.VERSION }} \ No newline at end of file From 84bac26106c30478b6df4de7374e1e31d97e7abb Mon Sep 17 00:00:00 2001 From: dweinholz Date: Thu, 19 Sep 2024 11:07:33 +0200 Subject: [PATCH 10/14] also special builds for publish docker! --- .github/workflows/build.yml | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8262010..60312b8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -53,10 +53,22 @@ jobs: type: [ postgresql, mysql, mongodb ] steps: - - name: Get the version - id: get_version - run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//} + - name: Workflow run cleanup action + uses: rokroskar/workflow-run-cleanup-action@v0.3.3 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - uses: actions/checkout@master + - name: Extract branch name + shell: bash + run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})" + id: extract_branch + + - name: Set tag + run: sed 's/\//-/g' <<< "::set-output name=TAG::${{ steps.extract_branch.outputs.branch }}" + id: tag + - name: Get tag + run: echo "The selected tag is ${{ steps.tag.outputs.TAG }}" + - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 @@ -75,4 +87,4 @@ jobs: context: ${{matrix.type}} push: true build-args: BASE_TAG - tags: quay.io/denbicloud/cron-backup:${{matrix.type}}-${{ steps.get_version.outputs.VERSION }} \ No newline at end of file + tags: quay.io/denbicloud/cron-backup:${{matrix.type}}-${{ steps.tag.outputs.TAG }} \ No newline at end of file From ddd8f51ad37a478ddb65c0496b3bc5f681dc5c87 Mon Sep 17 00:00:00 2001 From: dweinholz Date: Thu, 19 Sep 2024 11:09:24 +0200 Subject: [PATCH 11/14] also special builds for publish docker! --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 60312b8..2b51b91 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -81,7 +81,7 @@ jobs: - name: Build and publish image to Quay uses: docker/build-push-action@v5 env: - BASE_TAG: ${{ steps.get_version.outputs.VERSION }} + BASE_TAG: ${{ steps.tag.outputs.TAG }} with: file: ${{matrix.type}}/Dockerfile context: ${{matrix.type}} From 03375b53514270636831423b231170a71cb9acc6 Mon Sep 17 00:00:00 2001 From: dweinholz Date: Mon, 7 Oct 2024 08:45:02 +0200 Subject: [PATCH 12/14] 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 From 68f52dfa2be2908ce2cd738b3baaab01de964947 Mon Sep 17 00:00:00 2001 From: dweinholz Date: Mon, 7 Oct 2024 10:43:41 +0200 Subject: [PATCH 13/14] error to info text --- backup/notify_uptime_kuma.sh | 2 +- s3/s3_notify_uptime_kuma.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/backup/notify_uptime_kuma.sh b/backup/notify_uptime_kuma.sh index d366cde..9c4786e 100755 --- a/backup/notify_uptime_kuma.sh +++ b/backup/notify_uptime_kuma.sh @@ -4,7 +4,7 @@ KUMA_STATUS_ENDPOINT=${KUMA_STATUS_ENDPOINT:-} if [ -z "$KUMA_STATUS_ENDPOINT" ]; then - echo "Error: KUMA_STATUS_ENDPOINT is not set. Skipping." + echo "INFO: KUMA_STATUS_ENDPOINT is not set. Skipping." else # Use curl to make a GET request to the status endpoint response=$(curl -s -X GET "$KUMA_STATUS_ENDPOINT") diff --git a/s3/s3_notify_uptime_kuma.sh b/s3/s3_notify_uptime_kuma.sh index ecf4e43..e04514a 100755 --- a/s3/s3_notify_uptime_kuma.sh +++ b/s3/s3_notify_uptime_kuma.sh @@ -4,7 +4,7 @@ S3_KUMA_STATUS_ENDPOINT=${S3_KUMA_STATUS_ENDPOINT:-} if [ -z "$S3_KUMA_STATUS_ENDPOINT" ]; then - echo "Error: S3_KUMA_STATUS_ENDPOINT is not set. Skipping." + echo "INFO: S3_KUMA_STATUS_ENDPOINT is not set. Skipping." else # Use curl to make a POST request to the status endpoint response=$(curl -s -X POST \\ From c45a91d66074fdf49af62da8f4ebe4cefd3acae8 Mon Sep 17 00:00:00 2001 From: dweinholz Date: Mon, 7 Oct 2024 12:10:56 +0200 Subject: [PATCH 14/14] fixed location for kuma ep script --- mongodb/mongodb-backup.sh | 2 +- postgresql/postgresql-backup.sh | 2 +- s3/s3_backup.sh | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/mongodb/mongodb-backup.sh b/mongodb/mongodb-backup.sh index 0751bd8..149e4a9 100755 --- a/mongodb/mongodb-backup.sh +++ b/mongodb/mongodb-backup.sh @@ -28,7 +28,7 @@ if [ ! -s "$FILE" ] || [ $(stat -c%s "$FILE") -lt $MIN_SIZE ]; then fi # Send a notification using the notify_uptime_kuma.sh script -if ! ./notify_uptime_kuma.sh; then +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-backup.sh b/postgresql/postgresql-backup.sh index aeafbbc..d5d463f 100755 --- a/postgresql/postgresql-backup.sh +++ b/postgresql/postgresql-backup.sh @@ -55,7 +55,7 @@ if [ ! -s "$FILE" ] || [ $(stat -c%s "$FILE") -lt $MIN_SIZE ]; then fi # Send a notification using the notify_uptime_kuma.sh script -if ! ./notify_uptime_kuma.sh; then +if ! /notify_uptime_kuma.sh; then log "Failed to send notification" fi diff --git a/s3/s3_backup.sh b/s3/s3_backup.sh index 7e28eba..dc38298 100755 --- a/s3/s3_backup.sh +++ b/s3/s3_backup.sh @@ -92,6 +92,6 @@ done # Send a notification using the s3_notify_uptime_kuma.sh script -if ! ./s3_notify_uptime_kuma.sh; then +if ! /s3_notify_uptime_kuma.sh; then log "Failed to send notification" fi \ No newline at end of file