fixed backup/gdrive

This commit is contained in:
Rushabh Mehta 2013-03-18 12:48:34 +05:30
parent f64407d363
commit edf639b607
4 changed files with 82 additions and 43 deletions

View File

@ -1,3 +1,13 @@
# SETUP:
# install pip install --upgrade dropbox
#
# Create new Dropbox App
#
# in conf.py, set oauth2 settings
# dropbox_access_key
# dropbox_access_secret
import os import os
import webnotes import webnotes
from webnotes.utils import get_request_site_address, get_base_path from webnotes.utils import get_request_site_address, get_base_path
@ -66,13 +76,14 @@ def backup_to_dropbox():
# upload database # upload database
backup = new_backup() backup = new_backup()
filename = backup.backup_path_db filename = os.path.join(get_base_path(), "public", "backups",
os.path.basename(backup.backup_path_db))
upload_file_to_dropbox(filename, "database", dropbox_client) upload_file_to_dropbox(filename, "database", dropbox_client)
response = dropbox_client.metadata("/files") response = dropbox_client.metadata("/files")
# upload files to files folder # upload files to files folder
filename = os.path.join(get_base_path(),"public", "files") filename = os.path.join(get_base_path(), "public", "files")
for filename in os.listdir(filename): for filename in os.listdir(filename):
found = False found = False
for file_metadata in response["contents"]: for file_metadata in response["contents"]:

View File

@ -1,3 +1,15 @@
# SETUP:
# install pip install --upgrade google-api-python-client
#
# In Google API
# - create new API project
# - create new oauth2 client (create installed app type as google \
# does not support subdomains)
#
# in conf.py, set oauth2 settings
# gdrive_client_id
# gdrive_client_secret
import httplib2 import httplib2
import sys import sys
import os import os
@ -11,34 +23,12 @@ from apiclient.http import MediaFileUpload
@webnotes.whitelist() @webnotes.whitelist()
def get_gdrive_authorize_url(): def get_gdrive_authorize_url():
from conf import client_id, client_secret, oauth_scope, redirect_url
flow = get_gdrive_flow() flow = get_gdrive_flow()
authorize_url = flow.step1_get_authorize_url() authorize_url = flow.step1_get_authorize_url()
return_address = get_request_site_address(True) \
+ "?cmd=setup.doctype.backup_manager.backup_googledrive.gdrive_callback"
return { return {
"authorize_url": authorize_url, "authorize_url": authorize_url,
} }
@webnotes.whitelist(allow_guest=True)
def gdrive_callback(verification_code = None):
flow = get_gdrive_flow()
if verification_code:
credentials = flow.step2_exchange(verification_code)
allowed = 1
http = httplib2.Http()
http = credentials.authorize(http)
final_credentials = credentials.to_json()
drive_service = build('drive', 'v2', http=http)
erpnext_folder_id = create_erpnext_folder(drive_service)
database_folder_id = create_folder('database', drive_service, erpnext_folder_id)
files_folder_id = create_folder('files', drive_service, erpnext_folder_id)
webnotes.msgprint(_("Google Drive Access Approved."))
webnotes.conn.set_value("Backup Manager", "Backup Manager", "gdrive_access_allowed", allowed)
webnotes.conn.set_value("Backup Manager", "Backup Manager", "database_folder_id", database_folder_id)
webnotes.conn.set_value("Backup Manager", "Backup Manager", "files_folder_id", files_folder_id)
webnotes.conn.set_value("Backup Manager", "Backup Manager", "gdrive_credentials", final_credentials)
@webnotes.whitelist() @webnotes.whitelist()
def upload_files(name, mimetype, service, folder_id): def upload_files(name, mimetype, service, folder_id):
if not webnotes.conn: if not webnotes.conn:
@ -74,11 +64,15 @@ def backup_to_gdrive():
# upload database # upload database
backup = new_backup() backup = new_backup()
filename = os.path.basename(backup.backup_path_db) path = os.path.join(get_base_path(), "public", "backups")
filename = os.path.join(path, os.path.basename(backup.backup_path_db))
# upload files to database folder # 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(filename, 'application/x-gzip', drive_service,
webnotes.conn.get_value("Backup Manager", None, "database_folder_id"))
# upload files to files folder # upload files to files folder
path = os.path.join(get_base_path(),"public", "files") path = os.path.join(get_base_path(), "public", "files")
for files in os.listdir(path): for files in os.listdir(path):
filename = path + "/" + files filename = path + "/" + files
ext = filename.split('.')[-1] ext = filename.split('.')[-1]
@ -89,7 +83,9 @@ def backup_to_gdrive():
mimetype = mimetypes.types_map["." + ext] mimetype = mimetypes.types_map["." + ext]
#Compare Local File with Server File #Compare Local File with Server File
param = {} param = {}
children = drive_service.children().list(folderId=webnotes.conn.get_value("Backup Manager", None, "files_folder_id"), **param).execute() children = drive_service.children().list(
folderId=webnotes.conn.get_value("Backup Manager", None, "files_folder_id"),
**param).execute()
for child in children.get('items', []): for child in children.get('items', []):
file = drive_service.files().get(fileId=child['id']).execute() file = drive_service.files().get(fileId=child['id']).execute()
if files == file['title'] and size == int(file['fileSize']): if files == file['title'] and size == int(file['fileSize']):
@ -100,14 +96,45 @@ def backup_to_gdrive():
def get_gdrive_flow(): def get_gdrive_flow():
from oauth2client.client import OAuth2WebServerFlow from oauth2client.client import OAuth2WebServerFlow
try: import conf
from conf import client_id, client_secret, oauth_scope, redirect_url
except ImportError, e: if not hasattr(conf, "gdrive_client_id"):
webnotes.msgprint(_("Please set Google Drive access keys in") + " conf.py", webnotes.msgprint(_("Please set Google Drive access keys in") + " conf.py",
raise_exception=True) raise_exception=True)
flow = OAuth2WebServerFlow(client_id, client_secret, oauth_scope, redirect_url)
#callback_url = get_request_site_address(True) \
# + "?cmd=setup.doctype.backup_manager.backup_googledrive.googledrive_callback"
# for installed apps since google does not support subdomains
redirect_uri = "urn:ietf:wg:oauth:2.0:oob"
flow = OAuth2WebServerFlow(conf.gdrive_client_id, conf.gdrive_client_secret,
"https://www.googleapis.com/auth/drive", redirect_uri)
return flow return flow
@webnotes.whitelist()
def gdrive_callback(verification_code = None):
flow = get_gdrive_flow()
if verification_code:
credentials = flow.step2_exchange(verification_code)
allowed = 1
# make folders to save id
http = httplib2.Http()
http = credentials.authorize(http)
drive_service = build('drive', 'v2', http=http)
erpnext_folder_id = create_erpnext_folder(drive_service)
database_folder_id = create_folder('database', drive_service, erpnext_folder_id)
files_folder_id = create_folder('files', drive_service, erpnext_folder_id)
webnotes.conn.set_value("Backup Manager", "Backup Manager", "gdrive_access_allowed", allowed)
webnotes.conn.set_value("Backup Manager", "Backup Manager", "database_folder_id", database_folder_id)
webnotes.conn.set_value("Backup Manager", "Backup Manager", "files_folder_id", files_folder_id)
final_credentials = credentials.to_json()
webnotes.conn.set_value("Backup Manager", "Backup Manager", "gdrive_credentials", final_credentials)
webnotes.msgprint("Updated")
def create_erpnext_folder(service): def create_erpnext_folder(service):
if not webnotes.conn: if not webnotes.conn:
webnotes.connect() webnotes.connect()

View File

@ -47,7 +47,7 @@ cur_frm.cscript.allow_gdrive_access = function(doc) {
} }
} }
cur_frm.cscript.validate_gdrive_code = function(doc) { cur_frm.cscript.validate_gdrive = function(doc) {
wn.call({ wn.call({
method: "setup.doctype.backup_manager.backup_manager.gdrive_callback", method: "setup.doctype.backup_manager.backup_manager.gdrive_callback",
args: { args: {
@ -55,12 +55,11 @@ cur_frm.cscript.validate_gdrive_code = function(doc) {
}, },
}); });
} }
// cur_frm.cscript.backup_to_gdrive = function(doc) {
// msgprint("Backing up and uploading. This may take a few minutes.") cur_frm.cscript.upload_backups_to_dropbox = function(doc) {
// wn.call({ cur_frm.save()
// method: "setup.doctype.backup_manager.backup_manager.take_backups_gdrive", }
// callback: function(r) {
// msgprint("Backups taken. Please check your email for the response.") cur_frm.cscript.upload_backups_to_gdrive = function(doc) {
// } cur_frm.save()
// }) }
// }

View File

@ -19,8 +19,10 @@ def take_backups_weekly():
def take_backups_if(freq): def take_backups_if(freq):
if webnotes.conn.get_value("Backup Manager", None, "upload_backups_to_dropbox")==freq: if webnotes.conn.get_value("Backup Manager", None, "upload_backups_to_dropbox")==freq:
take_backups_dropbox() take_backups_dropbox()
if webnotes.conn.get_value("Backup Manager", None, "upload_backups_to_gdrive")==freq:
take_backups_gdrive() take_backups_gdrive()
#backup to dropbox
@webnotes.whitelist() @webnotes.whitelist()
def take_backups_dropbox(): def take_backups_dropbox():
try: try: