Skip to content

Instantly share code, notes, and snippets.

@nigelheap
Last active July 6, 2017 10:15
Show Gist options
  • Save nigelheap/5826ba6e82018f85c65932f8dbe0560d to your computer and use it in GitHub Desktop.
Save nigelheap/5826ba6e82018f85c65932f8dbe0560d to your computer and use it in GitHub Desktop.
# arg1 : bucket
# arg2 : max sub folders / days
total=`s3cmd ls s3://$1/ | wc -l`
# remove old folders if move then max
if [ "$total" -gt "$2" ]
then
# clean up s3 backups
s3cmd ls s3://$1/ | while read -r line;
do
createDate=`echo $line|awk {'print $1" "$2'}`
folder=`echo $line|awk {'print $2'}`
folderDate=`echo $folder|awk -F\/ {'print $5'}`
createDate=`date -d"$folderDate" +%s`
olderThan=`date -d"-$2 days" +%s`
if [[ $createDate != "" && $createDate -lt $olderThan ]]
then
if [[ $folder != "" ]]
then
folder=${folder:0:-1}
s3cmd del --recursive "$folder"
echo "Deleted $folder"
fi
fi
done;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment