Published on: 01.12.2017

Intro

I will show how to upload files to Dropbox from Python code.

Why do I need this?

Currently, I am only using WebFaction for all my web services and also as my private git server.

I wanted to make an automatic backup of my git repositories to Dropbox.

Dropbox App

I order to upload files to Dropbox you need to have an access token.

And for the access token, you need to register your app on DBX platform.

All of this must be done on Dropbox website.

The first step is to go to https://www.dropbox.com/developers/apps/ and press “Create App” button.

Step 1

Just click “Create app” button

Step 2

We will use Dropbox API.

We will choose “App folder” because we will just upload one backup to Dropbox, we do not need full access to all our files.

Name your app and click “Create App” button.

Step 3

We will use defaults settings, here we will get the access token so, click “Generate access token” button.

Step 4

Now you have your access token, you will need it in your code, so copy it.

Step 5

Now we have our “my_git_backup” Dropbox app.

pip install

pip install dropbox pip install fabric 1 2 pip install dropbox pip install fabric

It is always recommended to use virtual environments inside python.

At least I use them always.

Code

from fabric.api import local, lcd import datetime import dropbox remote_directory = 'full path to root git repository' NUMBER_OF_BACKUP_TO_KEEP = 10 1 2 3 4 5 6 from fabric . api import local , lcd import datetime import dropbox remote_directory = 'full path to root git repository' NUMBER_OF_BACKUP_TO_KEEP = 10

I am using fabric to make my life(code) easier.

I use fabric every time when I am calling CLI command from Python.

I will explain NUMBER_OF_BACKUP_TO_KEEP later, I use it at the end of the program.

def git_backup_to_dropbox(): with lcd(remote_directory): 1 2 def git_backup_to_dropbox ( ) : with lcd ( remote_directory ) :

All code that follows is inside with lcd(remote_directory): Python context manager.

The context manager is used so all code that follows is executed inside remote_directory directory.

name_of_backup = datetime.datetime.today().strftime('%Y-%m-%d_%H%M%S') name_of_backup += '_git_backup.zip' local('zip --password LAME_PASSWORD -r ' + name_of_backup + ' *.git') 1 2 3 4 name_of_backup = datetime . datetime . today ( ) . strftime ( '%Y-%m-%d_%H%M%S' ) name_of_backup += '_git_backup.zip' local ( 'zip --password LAME_PASSWORD -r ' + name_of_backup + ' *.git' )

Name of backup file will be YYYYMMDD_HHMM_git_backup.zip where upper case letters are date and time when a program was executed.

Eg. 20171121_1856_git_backup.zip so that we know from when is this backup file.

For making an actual backup, zip CLI command is used, we are only doing the backup of files that end on *.git (in my case only git repositories).

I also have LAME_PASSWORD for basic protection.

This is why I used fabric, just by calling local() function you can execute CLI commands.

dbx = dropbox.Dropbox('YOUR ACCESS TOKEN FROM DROPBOX!!!') with open(remote_directory + '/' + name_of_backup, 'rb') as f: dbx.files_upload(f.read(), '/' + name_of_backup ) local('rm ' + name_of_backup) 1 2 3 4 5 6 dbx = dropbox . Dropbox ( 'YOUR ACCESS TOKEN FROM DROPBOX!!!' ) with open ( remote_directory + '/' + name_of_backup , 'rb' ) as f : dbx . files_upload ( f . read ( ) , '/' + name_of _ backup ) local ( 'rm ' + name_of_backup )

The first line is the opening connection to your Dropbox application, you need to add your own access token as an argument.

Next two lines are for upload, you are: opening file, reading it and uploading bytes to Dropbox.

In Dropbox documentation is mention that this is only working for files till 150MB in size.

With last line program is deleting the local backup.

all_backup_files = [] for entry in dbx.files_list_folder('').entries: all_backup_files.append(entry.name) for file in sorted(all_backup_files[:-NUMBER_OF_BACKUP_TO_KEEP]): dbx.files_delete('/' + file) 1 2 3 4 5 6 all_backup_files = [ ] for entry in dbx . files_list_folder ( '' ) . entries : all_backup_files . append ( entry . name ) for file in sorted ( all_backup_files [ : - NUMBER_OF_BACKUP_TO_KEEP ] ) : dbx . files_delete ( '/' + file )

First for loop is getting all files from your backup folder in a list.

Second for loop is deleting all files except, last few files.

How many files to keep (otherwise we need manually to delete old backup files) is define in NUMBER_OF_BACKUP_TO_KEEP from the beginning of the code.

I keep it at 10, more than that I do not need.

Because we have date and time in our filename we can use Python sort function to sort files by when the backup was done.

The program can be run with

fab -f fabfile git_backup_to_dropbox

First is fab because we used fabric, fabfile because fabfile.py is file of our source code and git_backup_to_dropbox is name of the function that we are executing from fabfile.py file.

How I run this automaticaly

I personally run this command from crontab once per day.

35 02 * * * /home/user_name/code/venv/bin/fab -f /home/user_name/code/fabfile git_backup_to_dropbox

Conclusion

This can be used for backup of any folder as zip file automaticaly to Dropbox.

For any questions, please write them in comments.