Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/bin/bash

#Define the bucket name and the limit timestamp
bucket="BUCKET_NAME"
limit=`date --date="1 month ago" +"%Y%m%d"`

echo `date '+%F %T'` - Timestamp of one month ago: $onemonthago
echo `date '+%F %T'` - Getting the list of available backups

total=0

for filename in `s3cmd ls s3://$bucket`; do
    if [[ $filename =~ ([0-9]*)\.7z ]]; then
        timestamp=${BASH_REMATCH[1]}
        echo `date '+%F %T'` - Reading metadata of: $filename
        echo -e "\tFilename: $filename"
        echo -e "\tTimestamp: $timestamp"
        if [[ $timestamp -le $limit ]]; then
            let "total=total+1"
            echo -e "\tResult: Backup deleted\n"
            /usr/bin/s3cmd del $filename
        else
            echo -e "\tResult: Backup keeped\n"
        fi
done

echo `date '+%F %T'` - $total old backups removed