Skip to content

Commit

Permalink
Set file name
Browse files Browse the repository at this point in the history
  • Loading branch information
Rub21 committed Nov 19, 2023
1 parent 3796c2c commit 5a34712
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions images/backup-restore/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,33 +63,32 @@ restoreDB() {
wget -O ${RESTORE_FILE} ${RESTORE_URL_FILE}
echo "Restoring ${RESTORE_URL_FILE} in ${POSTGRES_DB}"
gunzip -c <${RESTORE_FILE} | psql -h ${POSTGRES_HOST} -U ${POSTGRES_USER} -d ${POSTGRES_DB} | tee ${LOG_RESULT_FILE}
aws s3 cp ${LOG_RESULT_FILE} s3://${AWS_S3_BUCKET}/
aws s3 cp ${LOG_RESULT_FILE} s3://${AWS_S3_BUCKET}/${LOG_RESULT_FILE}
echo "Import data to ${POSTGRES_DB} has finished ..."
done
}

delete_old_s3_files() {
# Use RETENTION_DAYS from environment variable or default to 30 days
if [ -z "${RETENTION_DAYS}" ]; then
DAYS_AGO=30
else
DAYS_AGO="${RETENTION_DAYS}"
fi
# Use RETENTION_DAYS from environment variable or default to 30 days
if [ -z "${RETENTION_DAYS}" ]; then
DAYS_AGO=30
else
DAYS_AGO="${RETENTION_DAYS}"
fi

echo "Files older than $DAYS_AGO days will be deleted."
echo "Processing s3://${AWS_S3_BUCKET}/${BACKUP_CLOUD_FOLDER}/"
TARGET_DATE=$(date -d "${DAYS_AGO} days ago" +%Y-%m-%d)
aws s3 ls "s3://${AWS_S3_BUCKET}/${BACKUP_CLOUD_FOLDER}/" --recursive | while read -r line; do
FILE_DATE=$(echo "$line" | awk '{print $1}')
FILE_PATH=$(echo "$line" | awk '{print $4}')
if [[ "$FILE_DATE" < "$TARGET_DATE" && ! -z "$FILE_PATH" ]]; then
echo "Deleting ${FILE_PATH} which was modified on ${FILE_DATE}"
aws s3 rm "s3://${AWS_S3_BUCKET}/${FILE_PATH}"
fi
done
echo "Files older than $DAYS_AGO days will be deleted."
echo "Processing s3://${AWS_S3_BUCKET}/${BACKUP_CLOUD_FOLDER}/"
TARGET_DATE=$(date -d "${DAYS_AGO} days ago" +%Y-%m-%d)
aws s3 ls "s3://${AWS_S3_BUCKET}/${BACKUP_CLOUD_FOLDER}/" --recursive | while read -r line; do
FILE_DATE=$(echo "$line" | awk '{print $1}')
FILE_PATH=$(echo "$line" | awk '{print $4}')
if [[ "$FILE_DATE" < "$TARGET_DATE" && ! -z "$FILE_PATH" ]]; then
echo "Deleting ${FILE_PATH} which was modified on ${FILE_DATE}"
aws s3 rm "s3://${AWS_S3_BUCKET}/${FILE_PATH}"
fi
done
}


# Main logic
case "${DB_ACTION}" in
backup)
Expand All @@ -106,7 +105,7 @@ esac

# Check for the CLEAN_BACKUPS var
if [ "$CLEANUP_BACKUPS" == "true" ]; then
delete_old_s3_files
delete_old_s3_files
else
echo "CLEANUP_BACKUPS is not set to true. Skipping deletion."
fi
echo "CLEANUP_BACKUPS is not set to true. Skipping deletion."
fi

0 comments on commit 5a34712

Please sign in to comment.