from dropbox import Dropbox
from subprocess import call
from time import gmtime, strftime
backupName = strftime("%Y%m%d-%H%M%S", gmtime())
backupNameZip = backupName+'.zip'
blogPath = '/var/www/blog'
backupPath = '~/'+backupName
call('cp -r ' + blogPath + ' ' + backupPath, shell=True)
call('zip -x *node_modules* -r ' + backupNameZip + ' ' + backupPath, shell=True)
dbx = Dropbox('TOKEN')
listFolder = dbx.files_list_folder('')
if len(listFolder.entries) >= 10:
dbx.files_delete(listFolder.entries[0].path_lower)
data = open(backupNameZip, 'rb')
dbx.files_upload(data.read(), '/'+backupNameZip)
data.close()
call('rm -r ' + backupPath + ' ' + backupNameZip, shell=True)