Hi, everybody!
I have a simple Python script to create an archive and store it in Dropbox.
import subprocess
import os
import dropbox
import shutil
from datetime import datetime
class DropboxUploader:
def __init__(self, dropbox_token, CHUNK_SIZE):
self.dropbox_token = dropbox_token
self.CHUNK_SIZE = CHUNK_SIZE
self.dbx = dropbox.Dropbox(dropbox_token)
def upload(self, file_name, file_size):
with open(f"{pwd}/{file_name}", 'rb') as f:
upload_session_start_result = self.dbx.files_upload_session_start(f.read(self.CHUNK_SIZE))
cursor = dropbox.files.UploadSessionCursor(session_id=upload_session_start_result.session_id, offset=f.tell())
commit = dropbox.files.CommitInfo(path=f"{pwd}/{file_name}")
while f.tell() < file_size:
if ((file_size - f.tell()) <= self.CHUNK_SIZE):
self.dbx.files_upload_session_finish(f.read(self.CHUNK_SIZE), cursor, commit)
else:
self.dbx.files_upload_session_append_v2(f.read(self.CHUNK_SIZE), cursor.session_id, cursor.offset)
cursor.offset = f.tell()
#Change those variables
root_dir = '/home/user/site_name'
site = 'site'
database = 'database'
dropbox_token = 'xxxxxxxxxxxxxxx'
CHUNK_SIZE = 8 * 1024 * 1024 # 8MB
archive_name = site + '_' + datetime.now().strftime("%Y%m%d_%H%M%S")
database_dump_name = database + '_' + datetime.now().strftime("%Y%m%d_%H%M%S") + '.sql'
pwd = os.getcwd()
archive_path = shutil.make_archive(archive_name, 'tar', root_dir)
archive_name = os.path.basename(archive_path)
command = f"mysqldump -u root {database} > {database_dump_name}"
subprocess.run(command, shell=True)
archive_size = os.path.getsize(f"./{archive_name}")
database_dump_size = os.path.getsize(f"./{database_dump_name}")
# Only for file sizes less than 150 MB
# dbx = dropbox.Dropbox(dropbox_token)
# with open(f"./{archive_name}.tar", 'rb') as f:
# dbx.files_upload(f.read(), f"{pwd}/{archive_name}.tar")
# with open(f"./{database_dump_name}.sql", 'rb') as f:
# dbx.files_upload(f.read(), f"{pwd}/{database_dump_name}.sql")
uploader = DropboxUploader(dropbox_token, CHUNK_SIZE)
uploader.upload(archive_name, archive_size)
uploader.upload(database_dump_name, database_dump_size)
But there is an error:
Exception has occurred: AssertionError
Expected content-type to be application/json, got 'application/grpc'
File "/home/user/dropbox/dropbox_backup.py", line 15, in upload
upload_session_start_result = self.dbx.files_upload_session_start(f.read(self.CHUNK_SIZE))
File "/home/user/dropbox/dropbox_backup.py", line 54, in <module>
uploader.upload(archive_name, archive_size)
AssertionError: Expected content-type to be application/json, got 'application/grpc'
I saw this post Python upload big file example - Dropbox Community (dropboxforum.com) my code is practically the same, and I don't get why mine isn't working.