[backup manager] [fixes] fixes in dropbox and google drive

This commit is contained in:
Anand Doshi 2013-04-23 14:23:03 +05:30
parent 94dab56a1f
commit ed7cf1bced
4 changed files with 57 additions and 15 deletions

View File

@ -7,12 +7,14 @@
# dropbox_access_key
# dropbox_access_secret
from __future__ import unicode_literals
import os
import webnotes
from webnotes.utils import get_request_site_address, get_base_path
from webnotes.utils import get_request_site_address, get_base_path, cstr
from webnotes import _
from backup_manager import ignore_list
@webnotes.whitelist()
def get_dropbox_authorize_url():
sess = get_dropbox_session()
@ -86,8 +88,13 @@ def backup_to_dropbox():
response = dropbox_client.metadata("/files")
# upload files to files folder
did_not_upload = []
error_log = []
path = os.path.join(get_base_path(), "public", "files")
for filename in os.listdir(path):
if filename in ignore_list:
continue
found = False
filepath = os.path.join(path, filename)
for file_metadata in response["contents"]:
@ -95,7 +102,13 @@ def backup_to_dropbox():
found = True
break
if not found:
try:
upload_file_to_dropbox(filepath, "/files", dropbox_client)
except Exception, e:
did_not_upload.append(filename)
error_log.append(cstr(e))
return did_not_upload, list(set(error_log))
def get_dropbox_session():
try:

View File

@ -10,12 +10,13 @@
# gdrive_client_id
# gdrive_client_secret
from __future__ import unicode_literals
import httplib2
import os
import mimetypes
import webnotes
import oauth2client.client
from webnotes.utils import get_base_path
from webnotes.utils import get_base_path, cstr
from webnotes import _, msgprint
from apiclient.discovery import build
from apiclient.http import MediaFileUpload
@ -30,6 +31,9 @@ def get_gdrive_authorize_url():
@webnotes.whitelist()
def upload_files(name, mimetype, service, folder_id):
import logging
logging.basicConfig()
if not webnotes.conn:
webnotes.connect()
file_name = os.path.basename(name)
@ -69,6 +73,9 @@ def backup_to_gdrive():
webnotes.conn.get_value("Backup Manager", None, "database_folder_id"))
# upload files to files folder
did_not_upload = []
error_log = []
path = os.path.join(get_base_path(), "public", "files")
for filename in os.listdir(path):
found = False
@ -78,7 +85,8 @@ def backup_to_gdrive():
if ext == 'gz' or ext == 'gzip':
mimetype = 'application/x-gzip'
else:
mimetype = mimetypes.types_map["." + ext]
mimetype = mimetypes.types_map.get("." + ext) or "application/octet-stream"
#Compare Local File with Server File
param = {}
children = drive_service.children().list(
@ -90,7 +98,14 @@ def backup_to_gdrive():
found = True
break
if not found:
upload_files(filepath, mimetype, drive_service, webnotes.conn.get_value("Backup Manager", None, "files_folder_id"))
try:
upload_files(filepath, mimetype, drive_service,
webnotes.conn.get_value("Backup Manager", None, "files_folder_id"))
except Exception, e:
did_not_upload.append(filename)
error_log.append(cstr(e))
return did_not_upload, list(set(error_log))
def get_gdrive_flow():
from oauth2client.client import OAuth2WebServerFlow

View File

@ -41,15 +41,17 @@ cur_frm.cscript.allow_gdrive_access = function(doc) {
wn.call({
method: "setup.doctype.backup_manager.backup_googledrive.get_gdrive_authorize_url",
callback: function(r) {
if(!r.exc) {
window.open(r.message.authorize_url);
}
}
})
}
}
cur_frm.cscript.validate_gdrive = function(doc) {
wn.call({
method: "setup.doctype.backup_manager.backup_manager.gdrive_callback",
method: "setup.doctype.backup_manager.backup_googledrive.gdrive_callback",
args: {
verification_code: doc.verification_code
},

View File

@ -4,6 +4,8 @@ from __future__ import unicode_literals
import webnotes
from webnotes import _
ignore_list = []
class DocType:
def __init__(self, d, dl):
self.doc, self.doclist = d, dl
@ -23,22 +25,32 @@ def take_backups_if(freq):
@webnotes.whitelist()
def take_backups_dropbox():
did_not_upload, error_log = [], []
try:
from setup.doctype.backup_manager.backup_dropbox import backup_to_dropbox
backup_to_dropbox()
did_not_upload, error_log = backup_to_dropbox()
if did_not_upload: raise Exception
send_email(True, "Dropbox")
except Exception:
send_email(False, "Dropbox", webnotes.getTraceback())
error_message = ("\n".join(error_log) + "\n" + webnotes.getTraceback())
print error_message
send_email(False, "Dropbox", error_message)
#backup to gdrive
@webnotes.whitelist()
def take_backups_gdrive():
did_not_upload, error_log = [], []
try:
from setup.doctype.backup_manager.backup_googledrive import backup_to_gdrive
backup_to_gdrive()
did_not_upload, error_log = backup_to_gdrive()
if did_not_upload: raise Exception
send_email(True, "Google Drive")
except Exception:
send_email(False, "Google Drive", webnotes.getTraceback())
error_message = ("\n".join(error_log) + "\n" + webnotes.getTraceback())
print error_message
send_email(False, "Google Drive", error_message)
def send_email(success, service_name, error_status=None):
if success: