This is a simple script that moves files to S3 if they are > 14 days old. It ignores files that are < 14 days old. If the file is successfully synced to AWS S3 then we can remove it from the server. If not, we leave the file where it is. The echo commands in the script are for debugging. I am aware that this could be a one liner, so interested to see what people suggest.
#!/bin/bash
fortnight=$(python -c "from datetime import datetime, timedelta; print (datetime.now()-timedelta(days=14)).strftime('%Y%m%d')")
for f in /var/mail/catchall-????????.gz
do
if [[ "$f" < "/var/mail/catchall-$fortnight.gz" ]] ; then
mv "$f" ./archive
echo "$f"
echo "$(basename $f)"
cd ./archive
if s3cmd sync "$(basename $f)" s3://vpcgwmail ; then
echo "S3 sync successful, removing file"
rm "$(basename $f)"
else
echo "s3 sync not succesful"
fi
fi
done