Reputation: 1853
So. I'm an idiot. I programmatically overwrote about 14,000 files in a bucket Friday and didn't realize it until today. Luckily, the files are versioned. Unluckily, I can't find a good clear example of how to write a script to go through the files, and then restore the second most recent version. I've been going through the Boto documentation and I see how to delete versions, but not restore. I'm comfortable using either php or python if anybody can give me a bit of code showing how to restore a specific version.
Upvotes: 5
Views: 2187
Reputation: 45856
This is something that will require some care on your part. I don't want to try to provide a complete solution for you because I won't have time to test it thoroughly and I wouldn't want to make any promises.
However, perhaps this will help.
First, let's assume that we want to find all of the versions of a particular key in a particular bucket in boto. We could do that like this:
import boto
conn = boto.connect_s3()
bucket_name = 'mybucket'
key_name = 'mykey'
bucket = conn.lookup(bucket_name)
for k in bucket.list_versions(key_name):
print(k.name, k.version_id, k.last_modified)
This should print out something like this:
mykey TyvPH4UUD4zRnGhmmLH6HGHOcOnsJgQG 2013-03-03T19:10:39.000Z
mykey IxNYlmoyDsOSspR6SwuGVNM7Nr83ZTSI 2013-03-03T15:11:06.000Z
mykey XVI9_yxQYU6B2KXQv0VLj7luYOGwWCoh 2013-03-03T15:10:55.000Z
mykey qh0zjxWjRC8WvXQc_RmvVdCJ.S3gF2ui 2013-03-03T15:07:46.000Z
Let's say that we want to "restore" the oldest file. To do that, we actually copy the desired version of the object back onto itself. In boto, that would look like this:
bucket.copy_key(new_key_name=key_name, src_bucket_name=bucket_name, src_key_name=key_name, src_version_id='qh0zjxWjRC8WvXQc_RmvVdCJ.S3gF2ui')
This would copy the specified version of the key back onto itself in the same bucket.
I think you will have to explore this a bit and do a lot of testing prior to turning your script loose. You could try, for example, to copy the desired version of the objects into another bucket and see if you get what you want before copying to your desired bucket.
Hope this helps.
Upvotes: 6