Skip to content

Commit

Permalink
Add helpful text
Browse files Browse the repository at this point in the history
  • Loading branch information
itbm committed May 22, 2019
1 parent 6574ed4 commit aa63add
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
set -e
set -o pipefail

>&2 echo "-----"

if [ "${S3_ACCESS_KEY_ID}" = "**None**" ]; then
echo "You need to set the S3_ACCESS_KEY_ID environment variable."
exit 1
Expand Down Expand Up @@ -65,7 +67,7 @@ DEST_FILE=${POSTGRES_DATABASE}_$(date +"%Y-%m-%dT%H:%M:%SZ").sql.gz
pg_dump $POSTGRES_HOST_OPTS $POSTGRES_DATABASE | gzip > $SRC_FILE

if [ "${ENCRYPTION_PASSWORD}" != "**None**" ]; then
echo "Encrypting ${SRC_FILE}"
>&2 echo "Encrypting ${SRC_FILE}"
openssl enc -aes-256-cbc -in $SRC_FILE -out ${SRC_FILE}.enc -k $ENCRYPTION_PASSWORD
if [ $? != 0 ]; then
>&2 echo "Error encrypting ${SRC_FILE}"
Expand All @@ -80,21 +82,26 @@ echo "Uploading dump to $S3_BUCKET"
cat $SRC_FILE | aws $AWS_ARGS s3 cp - s3://$S3_BUCKET/$S3_PREFIX/$DEST_FILE || exit 2

if [ "${DELETE_OLDER_THAN}" != "**None**" ]; then
>&2 echo "Checking for files older than ${DELETE_OLDER_THAN}"
aws $AWS_ARGS s3 ls s3://$S3_BUCKET/$S3_PREFIX/ | grep " PRE " -v | while read -r line;
do
fileName=`echo $line|awk {'print $4'}`
created=`echo $line|awk {'print $1" "$2'}`
created=`date -d "$created" +%s`
older_than=`date -d "$DELETE_OLDER_THAN" +%s`
if [ $created -lt $older_than ]
then
fileName=`echo $line|awk {'print $4'}`
if [ $fileName != "" ]
then
printf 'Deleting "%s"\n' $fileName
>&2 echo "DELETING ${fileName}"
aws $AWS_ARGS s3 rm s3://$S3_BUCKET/$S3_PREFIX/$fileName
fi
else
>&2 echo "${fileName} not older than ${DELETE_OLDER_THAN}"
fi
done;
fi

echo "SQL backup uploaded successfully"
echo "SQL backup finished"

>&2 echo "-----"

0 comments on commit aa63add

Please sign in to comment.