-
-
Save version-control/28a891ab5d78d9f71649429ce1181099 to your computer and use it in GitHub Desktop.
Backup a file to dropbox using dropbox's python SDK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import dropbox | |
from dropbox.files import WriteMode | |
from dropbox.exceptions import ApiError, AuthError | |
import datetime | |
dt = datetime.datetime.today() | |
TOKEN = '<Replace with your Access Token>' | |
LOCALFILE = '<Replace with the file that you want to backup>' | |
# Don't forget to add the file extension at the end of BACKUPPATH. | |
BACKUPPATH = f"/{dt.day}-{dt.month}-{dt.year}" | |
def backup(localFile, backupPath): | |
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 + "...") | |
try: | |
dbx.files_upload(f.read(), 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 | |
err.error.get_path().reason.is_insufficient_space()): | |
sys.exit("ERROR: Cannot back up; insufficient space.") | |
elif err.user_message_text: | |
print(err.user_message_text) | |
sys.exit() | |
else: | |
print(err) | |
sys.exit() | |
if __name__ == '__main__': | |
# Create an instance of a Dropbox class, which can make requests to the API. | |
print("Creating a Dropbox object...") | |
dbx = dropbox.Dropbox(TOKEN) | |
# Check that the access token is valid | |
try: | |
dbx.users_get_current_account() | |
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 | |
backup(LOCALFILE, BACKUPPATH) | |
print("Done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment