Skip to content

Instantly share code, notes, and snippets.

@cfr
Created October 25, 2017 11:31
Show Gist options
  • Save cfr/05fcdfc9cf48edccdac3435f17160b6d to your computer and use it in GitHub Desktop.
Save cfr/05fcdfc9cf48edccdac3435f17160b6d to your computer and use it in GitHub Desktop.
Script to backup large file to dropbox
#!/usr/bin/env python
import dropbox
import os
import time
file_path = "/opt/backups/backup.tar.gz"
dest_path = "/Apps/backup/"
dbx = dropbox.Dropbox("<app-token>")
dest_today_path = dest_path + time.strftime("%Y-%m-%d") + ".tgz"
f = open(file_path)
file_size = os.path.getsize(file_path)
CHUNK_SIZE = 4 * 1024 * 1024
if file_size <= CHUNK_SIZE:
print dbx.files_upload(f.read(), dest_today_path)
else:
upload_session_start_result = dbx.files_upload_session_start(f.read(CHUNK_SIZE))
cursor = dropbox.files.UploadSessionCursor(session_id=upload_session_start_result.session_id,
offset=f.tell())
commit = dropbox.files.CommitInfo(path=dest_today_path)
while f.tell() < file_size:
if ((file_size - f.tell()) <= CHUNK_SIZE):
print dbx.files_upload_session_finish(f.read(CHUNK_SIZE),
cursor,
commit)
else:
dbx.files_upload_session_append(f.read(CHUNK_SIZE),
cursor.session_id,
cursor.offset)
cursor.offset = f.tell()
f.close()
print ("Done: " + dest_today_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment