Skip to content

Commit

Permalink
feat(Validation):now check min size for created backup added kuma end…
Browse files Browse the repository at this point in the history
…point env for status check
  • Loading branch information
dweinholz committed Sep 19, 2024
1 parent 0ecb71a commit bfa63cd
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ RUN apk add --update --no-cache \
fdupes \
python3 \
py3-pip \
curl \
busybox-extras \
ssmtp

Expand All @@ -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

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions backup/notify_uptime_kuma.sh
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions mongodb/mongodb-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
12 changes: 12 additions & 0 deletions mysql/mysql-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
12 changes: 12 additions & 0 deletions postgresql/postgresql-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
4 changes: 3 additions & 1 deletion s3/s3_backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
10 changes: 10 additions & 0 deletions s3/s3_notify_uptime_kuma.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit bfa63cd

Please sign in to comment.