From fcc1662f5b1e66bdaf87d1df4b759c611bc3225b Mon Sep 17 00:00:00 2001 From: Volker Kettenbach Date: Wed, 21 Jul 2021 15:28:12 +0200 Subject: [PATCH 1/2] Fixed incoreect shebang --- resources/perform-backup.sh | 2 +- resources/slack-alert.sh | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/resources/perform-backup.sh b/resources/perform-backup.sh index 12197b9..657a7d8 100644 --- a/resources/perform-backup.sh +++ b/resources/perform-backup.sh @@ -1,4 +1,4 @@ -#/bin/sh +#!/bin/sh # Set the has_failed variable to false. This will change if any of the subsequent database backups/uploads fail. has_failed=false diff --git a/resources/slack-alert.sh b/resources/slack-alert.sh index 0385070..dad1c37 100644 --- a/resources/slack-alert.sh +++ b/resources/slack-alert.sh @@ -1,5 +1,4 @@ -#/bin/sh - +#!/bin/sh # Check if there is any value in $2. If so, post an entry to the Slack channel with log information. If not, send a general message that all databases successfully completed if [ "$(printf '%s' "$2")" == '' ] @@ -10,4 +9,4 @@ PAYLOAD="payload={\"channel\": \"$SLACK_CHANNEL\", \"username\": \"$SLACK_USERNA fi # Send Slack message -curl -s -X POST --data-urlencode "$PAYLOAD" "$SLACK_WEBHOOK_URL" > /dev/null \ No newline at end of file +curl -s -X POST --data-urlencode "$PAYLOAD" "$SLACK_WEBHOOK_URL" > /dev/null From a26f2d956c2464140a555e4b7e4ea187427603ad Mon Sep 17 00:00:00 2001 From: Volker Kettenbach Date: Wed, 21 Jul 2021 17:07:20 +0200 Subject: [PATCH 2/2] Add feature DATE_PREFIX --- README.md | 1 + resources/perform-backup.sh | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c6bf555..4cd00b7 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ The below table lists all of the Environment Variables that are configurable for | BACKUP_TIMESTAMP | **(Optional)** Date string to append to the backup filename ([date](http://man7.org/linux/man-pages/man1/date.1.html) format). Leave unset if using S3 Versioning and date stamp is not required. | | BACKUP_COMPRESS | **(Optional)** (true/false) Enable or disable gzip backup compression - (Default False). | | BACKUP_COMPRESS_LEVEL | **(Optional - default `9`)** Set the gzip level used for compression. | +| DATE_PREFIX | **(Optional)** (true/false) Enable or disable path prefix of `%Y/%m/%d/` format - (Default False). | | AGE_PUBLIC_KEY | **(Optional)** Public key used to encrypt backup with [FiloSottile/age](https://github.com/FiloSottile/age). Leave blank to disable backup encryption. | | SLACK_ENABLED | **(Optional)** (true/false) Enable or disable the Slack Integration (Default False). | | SLACK_USERNAME | **(Optional)** (true/false) Username to use for the Slack Integration (Default: kubernetes-cloud-mysql-backup). | diff --git a/resources/perform-backup.sh b/resources/perform-backup.sh index 657a7d8..6a70a3f 100644 --- a/resources/perform-backup.sh +++ b/resources/perform-backup.sh @@ -25,6 +25,13 @@ else BACKUP_CREATE_DATABASE_STATEMENT="" fi +# Add prefix to backup name +if [ "$DATE_PREFIX" = "true" ]; then + DATE_PREFIX=$(date +'%Y/%m/%d/') +else + DATE_PREFIX="" +fi + if [ "$TARGET_ALL_DATABASES" = "true" ]; then # Ignore any databases specified by TARGET_DATABASE_NAMES if [ ! -z "$TARGET_DATABASE_NAMES" ] @@ -94,7 +101,7 @@ if [ "$has_failed" = false ]; then fi # Perform the upload to S3. Put the output to a variable. If successful, print an entry to the console and the log. If unsuccessful, set has_failed to true and print an entry to the console and the log - if awsoutput=$(aws $ENDPOINT s3 cp /tmp/$DUMP s3://$AWS_BUCKET_NAME$AWS_BUCKET_BACKUP_PATH/$DUMP 2>&1); then + if awsoutput=$(aws $ENDPOINT s3 cp /tmp/$DUMP s3://$AWS_BUCKET_NAME$AWS_BUCKET_BACKUP_PATH/$DATE_PREFIX$DUMP 2>&1); then echo -e "Database backup successfully uploaded for $CURRENT_DATABASE at $(date +'%d-%m-%Y %H:%M:%S')." else echo -e "Database backup failed to upload for $CURRENT_DATABASE at $(date +'%d-%m-%Y %H:%M:%S'). Error: $awsoutput" | tee -a /tmp/kubernetes-cloud-mysql-backup.log @@ -107,7 +114,7 @@ if [ "$has_failed" = false ]; then if [ "$BACKUP_PROVIDER" = "gcp" ]; then # Perform the upload to S3. Put the output to a variable. If successful, print an entry to the console and the log. If unsuccessful, set has_failed to true and print an entry to the console and the log - if gcpoutput=$(gsutil cp /tmp/$DUMP gs://$GCP_BUCKET_NAME$GCP_BUCKET_BACKUP_PATH/$DUMP 2>&1); then + if gcpoutput=$(gsutil cp /tmp/$DUMP gs://$GCP_BUCKET_NAME$GCP_BUCKET_BACKUP_PATH/$DATE_PREFIX$DUMP 2>&1); then echo -e "Database backup successfully uploaded for $CURRENT_DATABASE at $(date +'%d-%m-%Y %H:%M:%S')." else echo -e "Database backup failed to upload for $CURRENT_DATABASE at $(date +'%d-%m-%Y %H:%M:%S'). Error: $gcpoutput" | tee -a /tmp/kubernetes-cloud-mysql-backup.log