Download the Latest File From S3

- - posted in s3, shell | Comments

I was writing a Ruby script which runs shell commands on a remote server as part of a disaster recoveray. I needed to fetch the latest backup for S3. My solution was to use Fog to query the S3 API and use Ruby to figure out which was the most recent filename, then build a command to run on the server. In other words I figure out the command locally and then run it to the server.

But Guy had a better solution which ran server side:

1
s3cmd get `s3cmd ls  s3://my_bucket/back* | tail -1 | awk '{print $4}'`

or

1
2
3
BACKUP_FILENAME=`s3cmd ls  s3://my_bucket/back* | tail -1 | awk '{print $4}'`
echo "The backup file name is $BACKUP_FILENAME"
s3cmd get $BACKUP_FILENAME

Comments