Skip to content
Switch branches/tags
Go to file
…ces (#207)

* Add Close() method to _DropboxTransport to cleanup any network resources

* Added contextManager

* lint fixes

Co-authored-by: Karandeep Johar <>
4 contributors

Users who have contributed to this file

@posita @tivaliy @braincore @karandeep-johar
Backs up and restores a settings file to Dropbox.
This is an example app for API v2.
import sys
import dropbox
from dropbox.files import WriteMode
from dropbox.exceptions import ApiError, AuthError
# Add OAuth2 access token here.
# You can generate one for yourself in the App Console.
# See <>
TOKEN = ''
LOCALFILE = 'my-file.txt'
BACKUPPATH = '/my-file-backup.txt'
# Uploads contents of LOCALFILE to Dropbox
def backup():
with open(LOCALFILE, 'rb') as f:
# We use WriteMode=overwrite to make sure that the settings in the file
# are changed on upload
print("Uploading " + LOCALFILE + " to Dropbox as " + BACKUPPATH + "...")
dbx.files_upload(, BACKUPPATH, mode=WriteMode('overwrite'))
except ApiError as err:
# This checks for the specific error where a user doesn't have
# enough Dropbox space quota to upload this file
if (err.error.is_path() and
sys.exit("ERROR: Cannot back up; insufficient space.")
elif err.user_message_text:
# Change the text string in LOCALFILE to be new_content
# @param new_content is a string
def change_local_file(new_content):
print("Changing contents of " + LOCALFILE + " on local machine...")
with open(LOCALFILE, 'wb') as f:
# Restore the local and Dropbox files to a certain revision
def restore(rev=None):
# Restore the file on Dropbox to a certain revision
print("Restoring " + BACKUPPATH + " to revision " + rev + " on Dropbox...")
dbx.files_restore(BACKUPPATH, rev)
# Download the specific revision of the file at BACKUPPATH to LOCALFILE
print("Downloading current " + BACKUPPATH + " from Dropbox, overwriting " + LOCALFILE + "...")
dbx.files_download_to_file(LOCALFILE, BACKUPPATH, rev)
# Look at all of the available revisions on Dropbox, and return the oldest one
def select_revision():
# Get the revisions for a file (and sort by the datetime object, "server_modified")
print("Finding available revisions on Dropbox...")
entries = dbx.files_list_revisions(BACKUPPATH, limit=30).entries
revisions = sorted(entries, key=lambda entry: entry.server_modified)
for revision in revisions:
print(revision.rev, revision.server_modified)
# Return the oldest revision (first entry, because revisions was sorted oldest:newest)
return revisions[0].rev
if __name__ == '__main__':
# Check for an access token
if (len(TOKEN) == 0):
sys.exit("ERROR: Looks like you didn't add your access token. "
"Open up in a text editor and "
"paste in your token in line 14.")
# Create an instance of a Dropbox class, which can make requests to the API.
print("Creating a Dropbox object...")
with dropbox.Dropbox(TOKEN) as dbx:
# Check that the access token is valid
except AuthError:
sys.exit("ERROR: Invalid access token; try re-generating an "
"access token from the app console on the web.")
# Create a backup of the current settings file
# Change the user's file, create another backup
# Restore the local and Dropbox files to a certain revision
to_rev = select_revision()