[backup manager] [fixes] fixes in dropbox and google drive
This commit is contained in:
parent
94dab56a1f
commit
ed7cf1bced
@ -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()
|
||||
@ -58,7 +60,7 @@ def dropbox_callback(oauth_token=None, not_approved=False):
|
||||
|
||||
webnotes.message_title = "Dropbox Approval"
|
||||
webnotes.message = "<h3>%s</h3><p>Please close this window.</p>" % message
|
||||
|
||||
|
||||
webnotes.conn.commit()
|
||||
webnotes.response['type'] = 'page'
|
||||
webnotes.response['page_name'] = 'message.html'
|
||||
@ -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:
|
||||
upload_file_to_dropbox(filepath, "/files", dropbox_client)
|
||||
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:
|
||||
|
@ -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)
|
||||
@ -67,8 +71,11 @@ def backup_to_gdrive():
|
||||
# upload files to database folder
|
||||
upload_files(filename, 'application/x-gzip', drive_service,
|
||||
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
|
||||
|
@ -41,7 +41,9 @@ cur_frm.cscript.allow_gdrive_access = function(doc) {
|
||||
wn.call({
|
||||
method: "setup.doctype.backup_manager.backup_googledrive.get_gdrive_authorize_url",
|
||||
callback: function(r) {
|
||||
window.open(r.message.authorize_url);
|
||||
if(!r.exc) {
|
||||
window.open(r.message.authorize_url);
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -49,7 +51,7 @@ cur_frm.cscript.allow_gdrive_access = function(doc) {
|
||||
|
||||
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
|
||||
},
|
||||
|
@ -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:
|
||||
@ -58,4 +70,4 @@ def send_email(success, service_name, error_status=None):
|
||||
# email system managers
|
||||
from webnotes.utils.email_lib import sendmail
|
||||
sendmail(webnotes.conn.get_value("Backup Manager", None, "send_notifications_to").split(","),
|
||||
subject=subject, msg=message)
|
||||
subject=subject, msg=message)
|
||||
|
Loading…
x
Reference in New Issue
Block a user