This post will helps you to find in the Last one hour files or directories and copy those files/directories from AW S3 bucket to in your Local machine or AWS EC2 instance using the Shell script,
Before, you execute the shell script make sure that are you able to access the AWS S3 buckets from your location where do you want to save the files. And Install an AWS CLI package in your machine.
Create a new file named: copy-s3-to-local.sh
#!/bin/bash
BUCKET_NAME="bucketname"
DATE=`date "+%Y-%m-%d"`
HOUR_NOW=`date "+%H"`
HOUR=`expr $HOUR_NOW - 1`
FILENAME=($(aws s3 ls s3://"$BUCKET_NAME"/ --recursive | sort | grep "$DATE" | grep "$HOUR:" | awk '{print $4}' ) )
for FILE in "${FILENAME[@]}"
do
sleep 2s
aws s3 cp s3://"$BUCKET_NAME"/$FILE /your_destination/ --recursive
done
Give the writable permission using the command below,
chmod +x copy-s3-to-local.sh
or
chmod 755 copy-s3-to-local.sh
Finally, execute the script with the command below,
sh copy-s3-to-local.sh
Comments (0)