diff --git a/setup/doctype/backup_manager/backup_dropbox.py b/setup/doctype/backup_manager/backup_dropbox.py index 0cd82c9601..6cdae7aa1b 100644 --- a/setup/doctype/backup_manager/backup_dropbox.py +++ b/setup/doctype/backup_manager/backup_dropbox.py @@ -3,8 +3,6 @@ import webnotes from webnotes.utils import get_request_site_address, get_base_path from webnotes import _ -filename = '' - @webnotes.whitelist() def get_dropbox_authorize_url(): sess = get_dropbox_session() @@ -12,7 +10,8 @@ def get_dropbox_authorize_url(): return_address = get_request_site_address(True) \ + "?cmd=setup.doctype.backup_manager.backup_dropbox.dropbox_callback" - url = sess.build_authorize_url(request_token, return_address) + url = sess.build_authorize_url(request_token, return_address) + return { "url": url, "key": request_token.key, @@ -21,31 +20,32 @@ def get_dropbox_authorize_url(): @webnotes.whitelist(allow_guest=True) def dropbox_callback(oauth_token=None, not_approved=False): + from dropbox import client if not not_approved: if webnotes.conn.get_value("Backup Manager", None, "dropbox_access_key")==oauth_token: - webnotes.conn.set_value("Backup Manager", "Backup Manager", "dropbox_access_allowed", 1) + allowed = 1 message = "Dropbox access allowed." sess = get_dropbox_session() sess.set_request_token(webnotes.conn.get_value("Backup Manager", None, "dropbox_access_key"), webnotes.conn.get_value("Backup Manager", None, "dropbox_access_secret")) access_token = sess.obtain_access_token() + webnotes.conn.set_value("Backup Manager", "Backup Manager", "dropbox_access_key", access_token.key) + webnotes.conn.set_value("Backup Manager", "Backup Manager", "dropbox_access_secret", access_token.secret) + webnotes.conn.set_value("Backup Manager", "Backup Manager", "dropbox_access_allowed", allowed) + dropbox_client = client.DropboxClient(sess) + dropbox_client.file_create_folder("files") - webnotes.conn.set_value("Backup Manager", "Backup Manager", "dropbox_access_key", - access_token.key) - webnotes.conn.set_value("Backup Manager", "Backup Manager", "dropbox_access_secret", - access_token.secret) - else: - webnotes.conn.set_value("Backup Manager", "Backup Manager", "dropbox_access_allowed", 0) + allowed = 0 message = "Illegal Access Token Please try again." else: - webnotes.conn.set_value("Backup Manager", "Backup Manager", "dropbox_access_allowed", 0) + allowed = 0 message = "Dropbox Access not approved." - + webnotes.message_title = "Dropbox Approval" webnotes.message = "
Please close this window.
" % message - + webnotes.conn.commit() webnotes.response['type'] = 'page' webnotes.response['page_name'] = 'message.html' @@ -57,7 +57,6 @@ def backup_to_dropbox(): if not webnotes.conn: webnotes.connect() - sess = session.DropboxSession(dropbox_access_key, dropbox_secret_key, "app_folder") sess.set_token(webnotes.conn.get_value("Backup Manager", None, "dropbox_access_key"), @@ -69,19 +68,20 @@ def backup_to_dropbox(): backup = new_backup() filename = backup.backup_path_db upload_file_to_dropbox(filename, "database", dropbox_client) - # upload files - response = dropbox_client.metadata("/database") - #add missing files - # for filename in os.listdir(filename): - # found = False - # for file_metadata in response["contents"]: - # if filename==os.path.basename(file_metadata["path"]): - # if os.stat(filename).st_size==file_metadata["bytes"]: - # found=True + response = dropbox_client.metadata("/files") + + # upload files to files folder + filename = os.path.join(get_base_path(),"public", "files") + for filename in os.listdir(filename): + found = False + for file_metadata in response["contents"]: + if filename==os.path.basename(file_metadata["path"]): + if os.stat(filename).st_size==file_metadata["bytes"]: + found=True - # if not found: - # upload_file_to_dropbox(filename, "database", dropbox_client) + if not found: + upload_file_to_dropbox(os.path.join(get_base_path(),"public", "files", filename), "files", dropbox_client) def get_dropbox_session(): from dropbox import session @@ -94,21 +94,18 @@ def get_dropbox_session(): return sess def upload_file_to_dropbox(filename, folder, dropbox_client): - path = os.path.join(get_base_path(),"public", "backups") - for files in os.listdir(path): - file_name = path + "/" + files - size = os.stat(file_name).st_size - f = open(filename,'r') - - if size > 4194304: - uploader = dropbox_client.get_chunked_uploader(f, size) - while uploader.offset < size: - try: - uploader.upload_chunked() - except rest.ErrorResponse, e: - pass - else: - response = dropbox_client.put_file(folder + "/" + os.path.basename(file_name), f, overwrite=True) + size = os.stat(filename).st_size + f = open(filename,'r') + if size > 4194304: + uploader = dropbox_client.get_chunked_uploader(f, size) + while uploader.offset < size: + try: + uploader.upload_chunked() + finish(folder + '/' + os.path.basename(filename), overwrite='True') + except rest.ErrorResponse, e: + pass + else: + response = dropbox_client.put_file(folder + "/" + os.path.basename(filename), f, overwrite=True) if __name__=="__main__": backup_to_dropbox() \ No newline at end of file diff --git a/setup/doctype/backup_manager/backup_manager.js b/setup/doctype/backup_manager/backup_manager.js index 154c72ec0e..073e0a0906 100644 --- a/setup/doctype/backup_manager/backup_manager.js +++ b/setup/doctype/backup_manager/backup_manager.js @@ -1,24 +1,66 @@ +cur_frm.cscript.refresh = function(doc) { + cur_frm.disable_save(); +} + +//dropbox cur_frm.cscript.allow_dropbox_access = function(doc) { - wn.call({ - method: "setup.doctype.backup_manager.backup_dropbox.get_dropbox_authorize_url", - callback: function(r) { - if(!r.exc) { - cur_frm.set_value("dropbox_access_secret", r.message.secret); - cur_frm.set_value("dropbox_access_key", r.message.key); - cur_frm.save(null, function() { - window.open(r.message.url); - }); + if (doc.send_notifications_to == '') { + msgprint("Please enter email address.") + } + else { + wn.call({ + method: "setup.doctype.backup_manager.backup_dropbox.get_dropbox_authorize_url", + callback: function(r) { + if(!r.exc) { + cur_frm.set_value("dropbox_access_secret", r.message.secret); + cur_frm.set_value("dropbox_access_key", r.message.key); + cur_frm.save(null, function() { + window.open(r.message.url); + }); + } } - } - }) + }) + } } cur_frm.cscript.backup_right_now = function(doc) { msgprint("Backing up and uploading. This may take a few minutes.") wn.call({ - method: "setup.doctype.backup_manager.backup_manager.take_backups", + method: "setup.doctype.backup_manager.backup_manager.take_backups_dropbox", callback: function(r) { msgprint("Backups taken. Please check your email for the response.") } }) -} \ No newline at end of file +} +//gdrive +cur_frm.cscript.allow_gdrive_access = function(doc) { + if (doc.send_notifications_to == '') { + msgprint("Please enter email address.") + } + else { + wn.call({ + method: "setup.doctype.backup_manager.backup_googledrive.get_gdrive_authorize_url", + callback: function(r) { + window.open(r.message.authorize_url); + } + }) + } +} + +cur_frm.cscript.validate_gdrive_code = function(doc) { + wn.call({ + method: "setup.doctype.backup_manager.backup_manager.gdrive_callback", + args: { + verification_code: doc.verification_code + }, + }); +} +// cur_frm.cscript.backup_to_gdrive = function(doc) { +// msgprint("Backing up and uploading. This may take a few minutes.") +// wn.call({ +// method: "setup.doctype.backup_manager.backup_manager.take_backups_gdrive", +// callback: function(r) { +// msgprint("Backups taken. Please check your email for the response.") +// } +// }) +// } \ No newline at end of file diff --git a/setup/doctype/backup_manager/backup_manager.py b/setup/doctype/backup_manager/backup_manager.py index ed2c64b0b9..71d8ad7625 100644 --- a/setup/doctype/backup_manager/backup_manager.py +++ b/setup/doctype/backup_manager/backup_manager.py @@ -4,6 +4,7 @@ from __future__ import unicode_literals import webnotes from webnotes import _ from backup_dropbox import dropbox_callback, get_dropbox_session, get_dropbox_authorize_url +from backup_googledrive import gdrive_callback, get_gdrive_flow, get_gdrive_authorize_url class DocType: def __init__(self, d, dl): @@ -17,16 +18,28 @@ def take_backups_weekly(): def take_backups_if(freq): if webnotes.conn.get_value("Backup Manager", None, "upload_backups_to_dropbox")==freq: - take_backups() - + take_backups_dropbox() + take_backups_gdrive() +#backup to dropbox @webnotes.whitelist() -def take_backups(): +def take_backups_dropbox(): try: from setup.doctype.backup_manager.backup_dropbox import backup_to_dropbox backup_to_dropbox() send_email(True, "Dropbox") except Exception, e: send_email(False, "Dropbox", e) + +#backup to gdrive +@webnotes.whitelist() +def take_backups_gdrive(): + try: + from setup.doctype.backup_manager.backup_googledrive import backup_to_gdrive + backup_to_gdrive() + send_email(True, "Google Drive") + except Exception, e: + send_email(False, "Google Drive", e) + def send_email(success, service_name, error_status=None): if success: subject = "Backup Upload Successful" @@ -45,6 +58,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) - -get_dropbox_authorize_url() \ No newline at end of file + subject=subject, msg=message) \ No newline at end of file diff --git a/setup/doctype/backup_manager/backup_manager.txt b/setup/doctype/backup_manager/backup_manager.txt index a994e7da53..bdb9c50605 100644 --- a/setup/doctype/backup_manager/backup_manager.txt +++ b/setup/doctype/backup_manager/backup_manager.txt @@ -1,8 +1,8 @@ [ { - "creation": "2013-03-05 16:35:50", + "creation": "2013-03-15 11:06:59", "docstatus": 0, - "modified": "2013-03-07 12:18:07", + "modified": "2013-03-15 16:10:05", "modified_by": "Administrator", "owner": "Administrator" }, @@ -56,7 +56,9 @@ "doctype": "DocField", "fieldname": "backup_right_now", "fieldtype": "Button", - "label": "Backup Right Now" + "hidden": 1, + "label": "Backup Right Now", + "read_only": 1 }, { "description": "Note: Backups and files are not deleted from Dropbox, you will have to delete them manually.", @@ -102,6 +104,70 @@ "fieldtype": "Button", "label": "Allow Dropbox Access" }, + { + "description": "Note: Backups and files are not deleted from Google Drive, you will have to delete them manually.", + "doctype": "DocField", + "fieldname": "sync_with_gdrive", + "fieldtype": "Section Break", + "label": "Sync with Google Drive" + }, + { + "doctype": "DocField", + "fieldname": "upload_backups_to_gdrive", + "fieldtype": "Select", + "label": "Upload Backups to Google Drive", + "options": "Never\nDaily\nWeekly" + }, + { + "doctype": "DocField", + "fieldname": "allow_gdrive_access", + "fieldtype": "Button", + "label": "Allow Google Drive Access" + }, + { + "doctype": "DocField", + "fieldname": "verification_code", + "fieldtype": "Data", + "label": "Enter Verification Code" + }, + { + "doctype": "DocField", + "fieldname": "validate_gdrive", + "fieldtype": "Button", + "label": "Validate" + }, + { + "doctype": "DocField", + "fieldname": "gdrive_access_allowed", + "fieldtype": "Check", + "hidden": 1, + "label": "Google Drive Access Allowed", + "read_only": 1 + }, + { + "doctype": "DocField", + "fieldname": "gdrive_credentials", + "fieldtype": "Text", + "hidden": 1, + "label": "Credentials", + "read_only": 1 + }, + { + "doctype": "DocField", + "fieldname": "database_folder_id", + "fieldtype": "Data", + "hidden": 1, + "label": "Database Folder ID", + "read_only": 1 + }, + { + "doctype": "DocField", + "fieldname": "files_folder_id", + "fieldtype": "Data", + "hidden": 1, + "label": "Files Folder ID", + "read_only": 1 + }, { "doctype": "DocPerm" }