From b041c03909bfe6d1d54afa7405704d9f25218dd1 Mon Sep 17 00:00:00 2001 From: Csaba Sulyok Date: Wed, 16 Mar 2022 05:10:40 +0200 Subject: [PATCH] attempt to create s3 bucket automatically if not found (#140) Co-authored-by: Sulyok Csaba --- mysql-backup-s3/backup.sh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mysql-backup-s3/backup.sh b/mysql-backup-s3/backup.sh index 2b4b583..f3130ad 100644 --- a/mysql-backup-s3/backup.sh +++ b/mysql-backup-s3/backup.sh @@ -50,8 +50,15 @@ copy_s3 () { AWS_ARGS="--endpoint-url ${S3_ENDPOINT}" fi - echo "Uploading ${DEST_FILE} on S3..." + echo "Ensuring S3 bucket $S3_BUCKET exists" + EXISTS_ERR=`aws $AWS_ARGS s3api head-bucket --bucket "$S3_BUCKET" 2>&1 || true` + if [[ ! -z "$EXISTS_ERR" ]]; then + echo "Bucket $S3_BUCKET not found (or owned by someone else), attempting to create" + aws $AWS_ARGS s3api create-bucket --bucket $S3_BUCKET + fi + echo "Uploading ${DEST_FILE} on S3..." + cat $SRC_FILE | aws $AWS_ARGS s3 cp - s3://$S3_BUCKET/$S3_PREFIX/$DEST_FILE if [ $? != 0 ]; then