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 webnotes
from webnotes.utils import get_request_site_address, get_base_path
@ -66,13 +76,14 @@ def backup_to_dropbox():
# upload database
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)
response = dropbox_client.metadata("/files")
# 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):
found = False
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 sys
import os
@ -11,34 +23,12 @@ from apiclient.http import MediaFileUpload
@webnotes.whitelist()
def get_gdrive_authorize_url():
from conf import client_id, client_secret, oauth_scope, redirect_url
flow = get_gdrive_flow()
authorize_url = flow.step1_get_authorize_url()
return_address = get_request_site_address(True) \
+ "?cmd=setup.doctype.backup_manager.backup_googledrive.gdrive_callback"
return {
"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()
def upload_files(name, mimetype, service, folder_id):
if not webnotes.conn:
@ -74,11 +64,15 @@ def backup_to_gdrive():
# upload database
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(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
path = os.path.join(get_base_path(),"public", "files")
path = os.path.join(get_base_path(), "public", "files")
for files in os.listdir(path):
filename = path + "/" + files
ext = filename.split('.')[-1]
@ -89,7 +83,9 @@ def backup_to_gdrive():
mimetype = mimetypes.types_map["." + ext]
#Compare Local File with Server File
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', []):
file = drive_service.files().get(fileId=child['id']).execute()
if files == file['title'] and size == int(file['fileSize']):
@ -100,13 +96,44 @@ def backup_to_gdrive():
def get_gdrive_flow():
from oauth2client.client import OAuth2WebServerFlow
try:
from conf import client_id, client_secret, oauth_scope, redirect_url
except ImportError, e:
import conf
if not hasattr(conf, "gdrive_client_id"):
webnotes.msgprint(_("Please set Google Drive access keys in") + " conf.py",
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
@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):
if not webnotes.conn:

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({
method: "setup.doctype.backup_manager.backup_manager.gdrive_callback",
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.")
// 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.")
// }
// })
// }
cur_frm.cscript.upload_backups_to_dropbox = function(doc) {
cur_frm.save()
}
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):
if webnotes.conn.get_value("Backup Manager", None, "upload_backups_to_dropbox")==freq:
take_backups_dropbox()
if webnotes.conn.get_value("Backup Manager", None, "upload_backups_to_gdrive")==freq:
take_backups_gdrive()
#backup to dropbox
@webnotes.whitelist()
def take_backups_dropbox():
try: