def process_folder(path): print 'process folder', path.encode('ascii', 'replace') folder_metadata = client.metadata(path, include_deleted=True) for meta in folder_metadata['contents']: if meta['is_dir']: process_folder(meta['path']) elif meta.has_key('is_deleted') and meta['is_deleted']: f = meta['path'] print 'restore file', f.encode('ascii', 'replace') revs = client.revisions(f, rev_limit=2) if len(revs) >= 2: client.restore(f, revs[1]['rev'])
def list_folder (root_folder_name): f.write ("Traversing " + root_folder_name + '\n'); folder_metadata = client.metadata(root_folder_name, list=True, include_deleted=True) for file_name in folder_metadata['contents']: if (file_name['is_dir']): list_folder(file_name['path']) else: file_mod = parser.parse(file_name['modified']) if file_name.get('is_deleted', False): # file has been deleted if (file_mod >= cutoff_bottom) and (file_mod <= cutoff_top): # and only recently #Now look for the PREVIOUS version revisions = client.revisions(file_name['path'], 100); #print "Found " + str(len(revisions)) + " revisions: \n"; revisions.reverse(); revisions.pop() # ignore the latest, this is the deleted version myrev = revisions.pop(); f.write("Recovering " + file_name['path'] + "(" + myrev['modified'] + ", rev " + str(myrev['rev']) + '\n'); result = client.restore(file_name['path'], myrev['rev']); print "Restored " + file_name['path'] + ". (" + myrev['modified'] + ", " + str(result['size']) + ")"; else: f.write("Not recovering " + file_name['path'] + ' rev ' + file_name['rev'] + '\n'); return;
# ACCESS_TYPE should be 'dropbox' or 'app_folder' as configured for your app ACCESS_TYPE = 'dropbox' sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE) # get req token request_token = sess.obtain_request_token() url = sess.build_authorize_url(request_token) print "url:", url print "Please visit this website and press the 'Allow' button, then hit 'Enter' here." raw_input() # This will fail if the user didn't visit the above URL and hit 'Allow' access_token = sess.obtain_access_token(request_token) client = client.DropboxClient(sess) with open ('out_grepped.txt','rb') as f: for line in f: delfile =json.loads(line) print delfile['path'] revs = client.revisions(delfile['path']) for r in revs: print r if 'is_deleted' not in r: rev = r['rev'] client.restore(delfile['path'],rev) break
else: print " %s is deleted"%(filedata["path"]) # fetch file history, and pick the first non-deleted revision. revisions = client.revisions(filedata["path"], rev_limit=10) alive = filter(lambda r: not r.get("is_deleted", False), revisions)[0] # create destination folder. try: os.makedirs(os.path.dirname(target)) except OSError: pass if USE_RESTORE: restore = client.restore(filedata["path"], alive["rev"]) print restore else: # try to download file. # I'm torn here - I could just use the Dropbox API and tell it to # restore the deleted file to the non-deleted version. PRoblem with # that is that it might recover too much. THis approach lets me restore # to a new folder with _just_ the restored files in, and cherry-pick # what I want to copy back into the main dropbox. try: fh = client.get_file(filedata["path"], rev=alive["rev"]) with open(target+".temp", "w") as oh: oh.write(fh.read()) os.rename(target+'.temp', target) print " ..recovered"
print " %s is deleted" % (filedata["path"]) # fetch file history, and pick the first non-deleted revision. revisions = client.revisions(filedata["path"], rev_limit=10) alive = filter(lambda r: not r.get("is_deleted", False), revisions)[0] # create destination folder. try: os.makedirs(os.path.dirname(target)) except OSError: pass if USE_RESTORE: restore = client.restore(filedata["path"], alive["rev"]) print restore else: # try to download file. # I'm torn here - I could just use the Dropbox API and tell it to # restore the deleted file to the non-deleted version. PRoblem with # that is that it might recover too much. THis approach lets me restore # to a new folder with _just_ the restored files in, and cherry-pick # what I want to copy back into the main dropbox. try: fh = client.get_file(filedata["path"], rev=alive["rev"]) with open(target + ".temp", "w") as oh: oh.write(fh.read()) os.rename(target + '.temp', target) print " ..recovered"