Created
April 26, 2018 19:26
-
-
Save eduardogspereira/7753bc8d780d07b70668c07afac7ec3b to your computer and use it in GitHub Desktop.
Delete files from S3 that are older than X days.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# You need to uncomment two lines for the script work. | |
# Please check the logic before use it. | |
main () { | |
#aws s3 ls $BUCKETNAME | grep -v ' PRE ' | grep -v ' 0 ' > data.txt | |
UNIXDAYS=$(date -d "$DAYS days ago" +%s) | |
while read LINE | |
do | |
FILEAGE=$(date -d "$(echo $LINE | awk '{print $1 " " $2}')" +%s) | |
if [ $FILEAGE -lt $UNIXDAYS ] | |
then | |
FILENAME=$(echo $LINE | rev | awk '{print $1}' | rev) | |
echo "removing $BUCKETNAME$FILENAME." | |
#aws s3 rm $BUCKETNAME$FILENAME | |
fi | |
done < data.txt | |
} | |
BUCKETNAME=$1 | |
if [ -z $BUCKETNAME ] | |
then | |
echo "Insert a valid bucket name." | |
exit 1 | |
fi | |
DAYS=$2 | |
if [ -z $DAYS ] | |
then | |
echo "You need the number of days. Files older than this will be deleted." | |
exit 1 | |
fi | |
main $BUCKETNAME $DAYS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment