# Dropbox SDK import dropbox # app_key app_secret 1 app_key = 'INSERT_APP_KEY' app_secret = 'INSERT_APP_SECRET' flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret) # authorize_url = flow.start() print '1. : ' + authorize_url print '2. "Allow"' print '3. .' code = raw_input(" : ").strip() # , access_token, access_token, user_id = flow.finish(code) # client = dropbox.client.DropboxClient(access_token) print 'linked account: ', client.account_info() # access_token print 'access_token: ', access_token
#!/usr/bin/python import os import sys import time import string from os.path import getsize curDate = time.strftime("%d.%m.%Y", time.gmtime()) curDay = time.strftime("%d", time.gmtime()) backupDelay = time.time()-86400 if curDay == "01" or curDay == "15": backupDelay = 0 print "curDate:", curDate # Include the Dropbox SDK libraries from dropbox import client, rest, session # Get your app key and secret from the Dropbox developer website APP_KEY = ' ' APP_SECRET = ' ' # ACCESS_TYPE should be 'dropbox' or 'app_folder' as configured for your app ACCESS_TYPE = 'app_folder' sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE) oauth_token = '' oauth_token_secret = '' f = open("dropbox_token.txt",'r') if f: oauth_token = string.strip(f.readline()) oauth_token_secret = string.strip(f.readline()) f.close() print "oauth token found:", oauth_token, oauth_token_secret if oauth_token == '' or oauth_token_secret == '': request_token = sess.obtain_request_token() # Authorize the application on dropbox site url = sess.build_authorize_url(request_token) print "url:", url print "Please visit this website and press the 'Allow' button, then hit 'Enter' here." raw_input() # This will fail if the user didn't visit the above URL and hit 'Allow' access_token = sess.obtain_access_token(request_token) f = open("dropbox_token.txt","wb") f.write(access_token.key + '\n') f.write(access_token.secret) f.close() else: sess.set_token(oauth_token, oauth_token_secret) client = client.DropboxClient(sess) print "linked account:", client.account_info() def sync_dir(dir): rootdir = dir print "Syncing directory:", rootdir startTime = backupDelay for root, subFolders, files in os.walk(rootdir): for file in files: fname = os.path.join(root,file) if os.path.getmtime(fname)>startTime: #print root, file os.system("mkdir -p 'backup"+root+"'") os.system("cp '"+fname+"' 'backup"+fname+"'") print "Making dump of MySQL databases..." os.system("mysqldump --all-databases -uroot -pROOT__MYSQL -r backup/backup.sql") sync_dir("/var/www") sync_dir("/var/spool/virtual") sync_dir("/home/user") backupName = 'backup_'+curDate+'.7z' print "Creating archive with name", backupName os.system("7z a -p_ "+backupName+" backup/* /etc") f = open(backupName,'rb') if f: fsize = getsize(backupName) uploader = client.get_chunked_uploader(f, fsize) print "Uploading file", fsize, "bytes..." while uploader.offset < fsize: try: upload = uploader.upload_chunked() print "." except rest.ErrorResponse, e: # perform error handling and retry logic print "error uploading file!" uploader.finish("/"+backupName) f.close() print "File uploaded successfully." print "Deleting temp files..." os.system("rm -r backup/*") os.system("rm " + backupName);
Source: https://habr.com/ru/post/236483/
All Articles