From 2dbb08d7a897533097a0ddb00d09df81ea356e8c Mon Sep 17 00:00:00 2001 From: Nikolay Kiryanov Date: Wed, 28 Feb 2024 00:41:04 +0300 Subject: [PATCH] Linting: require braces around variables --- .github/workflows/ci.yml | 2 +- Makefile | 4 ++++ backup.sh | 46 ++++++++++++++++++++-------------------- entrypoint.sh | 6 +++--- 4 files changed, 31 insertions(+), 27 deletions(-) create mode 100644 Makefile diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eb6c9ed..7298ad2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,4 +13,4 @@ jobs: uses: actions/checkout@v4 - name: Lint - run: shellcheck *.sh + run: make lint diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d0d204d --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +SHELL_FILES := $(wildcard *.sh) + +lint: + @shellcheck --enable=require-variable-braces $(SHELL_FILES) && echo "ShellCheck passed" diff --git a/backup.sh b/backup.sh index fcb9a8c..6b05931 100644 --- a/backup.sh +++ b/backup.sh @@ -6,73 +6,73 @@ set -e set -o pipefail -if [ "$S3_ACCESS_KEY_ID" = "**None**" ]; then +if [ "${S3_ACCESS_KEY_ID}" = "**None**" ]; then echo "You need to set the S3_ACCESS_KEY_ID environment variable." exit 1 fi -if [ "$S3_SECRET_ACCESS_KEY" = "**None**" ]; then +if [ "${S3_SECRET_ACCESS_KEY}" = "**None**" ]; then echo "You need to set the S3_SECRET_ACCESS_KEY environment variable." exit 1 fi -if [ "$S3_BUCKET" = "**None**" ]; then +if [ "${S3_BUCKET}" = "**None**" ]; then echo "You need to set the S3_BUCKET environment variable." exit 1 fi -if [ "$POSTGRES_DATABASE" = "**None**" ]; then +if [ "${POSTGRES_DATABASE}" = "**None**" ]; then echo "You need to set the POSTGRES_DATABASE environment variable." exit 1 fi -if [ "$POSTGRES_HOST" = "**None**" ]; then +if [ "${POSTGRES_HOST}" = "**None**" ]; then if [ -n "${POSTGRES_PORT_5432_TCP_ADDR}" ]; then - POSTGRES_HOST="$POSTGRES_PORT_5432_TCP_ADDR" - POSTGRES_PORT="$POSTGRES_PORT_5432_TCP_PORT" + POSTGRES_HOST="${POSTGRES_PORT_5432_TCP_ADDR}" + POSTGRES_PORT="${POSTGRES_PORT_5432_TCP_PORT}" else echo "You need to set the POSTGRES_HOST environment variable." exit 1 fi fi -if [ "$POSTGRES_USER" = "**None**" ]; then +if [ "${POSTGRES_USER}" = "**None**" ]; then echo "You need to set the POSTGRES_USER environment variable." exit 1 fi -if [ "$POSTGRES_PASSWORD" = "**None**" ]; then +if [ "${POSTGRES_PASSWORD}" = "**None**" ]; then echo "You need to set the POSTGRES_PASSWORD environment variable or link to a container named POSTGRES." exit 1 fi -if [ "$S3_ENDPOINT" = "**None**" ]; then +if [ "${S3_ENDPOINT}" = "**None**" ]; then AWS_ARGS="" else - AWS_ARGS="--endpoint-url $S3_ENDPOINT" + AWS_ARGS="--endpoint-url ${S3_ENDPOINT}" fi # env vars needed for aws tools -export AWS_ACCESS_KEY_ID="$S3_ACCESS_KEY_ID" -export AWS_SECRET_ACCESS_KEY="$S3_SECRET_ACCESS_KEY" -export AWS_DEFAULT_REGION="$S3_REGION" +export AWS_ACCESS_KEY_ID="${S3_ACCESS_KEY_ID}" +export AWS_SECRET_ACCESS_KEY="${S3_SECRET_ACCESS_KEY}" +export AWS_DEFAULT_REGION="${S3_REGION}" -export PGPASSWORD="$POSTGRES_PASSWORD" -POSTGRES_HOST_OPTS="-h $POSTGRES_HOST -p $POSTGRES_PORT -U $POSTGRES_USER $POSTGRES_EXTRA_OPTS" +export PGPASSWORD="${POSTGRES_PASSWORD}" +POSTGRES_HOST_OPTS="-h ${POSTGRES_HOST} -p ${POSTGRES_PORT} -U ${POSTGRES_USER} ${POSTGRES_EXTRA_OPTS}" -echo "Creating dump of $POSTGRES_DATABASE database from $POSTGRES_HOST..." +echo "Creating dump of ${POSTGRES_DATABASE} database from ${POSTGRES_HOST}..." -pg_dump -Fc $POSTGRES_HOST_OPTS "$POSTGRES_DATABASE" > db.dump +pg_dump -Fc ${POSTGRES_HOST_OPTS} "${POSTGRES_DATABASE}" > db.dump -echo "Uploading dump to $S3_BUCKET" +echo "Uploading dump to ${S3_BUCKET}" -aws $AWS_ARGS s3 cp db.dump "s3://$S3_BUCKET/$S3_PREFIX/${POSTGRES_DATABASE}_$(date +"%Y-%m-%dT%H:%M:%SZ").dump" || exit 2 +aws ${AWS_ARGS} s3 cp db.dump "s3://${S3_BUCKET}/${S3_PREFIX}/${POSTGRES_DATABASE}_$(date +"%Y-%m-%dT%H:%M:%SZ").dump" || exit 2 echo "DB backup uploaded successfully" rm db.dump -if [ ! "$SUCCESS_WEBHOOK" = "**None**" ]; then - echo "Notifying $SUCCESS_WEBHOOK" - curl -m 10 --retry 5 "$SUCCESS_WEBHOOK" +if [ ! "${SUCCESS_WEBHOOK}" = "**None**" ]; then + echo "Notifying ${SUCCESS_WEBHOOK}" + curl -m 10 --retry 5 "${SUCCESS_WEBHOOK}" fi diff --git a/entrypoint.sh b/entrypoint.sh index 3c7df9a..bbb32cb 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -2,14 +2,14 @@ set -e -if [ "$S3_S3V4" = "yes" ]; then +if [ "${S3_S3V4}" = "yes" ]; then aws configure set default.s3.signature_version s3v4 fi -if [ "$SCHEDULE" = "**None**" ]; then +if [ "${SCHEDULE}" = "**None**" ]; then echo You need to set up SCHEDULE env var exit 127 else - echo "$SCHEDULE /bin/sh /backup.sh" > /etc/crontab.backup + echo "${SCHEDULE} /bin/sh /backup.sh" > /etc/crontab.backup exec supercronic -debug -prometheus-listen-address 0.0.0.0 /etc/crontab.backup fi