mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-24 15:39:20 +00:00
Backup commited.
This commit is contained in:
@@ -3,8 +3,6 @@ import webnotes
|
|||||||
from webnotes.utils import get_request_site_address, get_base_path
|
from webnotes.utils import get_request_site_address, get_base_path
|
||||||
from webnotes import _
|
from webnotes import _
|
||||||
|
|
||||||
filename = ''
|
|
||||||
|
|
||||||
@webnotes.whitelist()
|
@webnotes.whitelist()
|
||||||
def get_dropbox_authorize_url():
|
def get_dropbox_authorize_url():
|
||||||
sess = get_dropbox_session()
|
sess = get_dropbox_session()
|
||||||
@@ -12,7 +10,8 @@ def get_dropbox_authorize_url():
|
|||||||
return_address = get_request_site_address(True) \
|
return_address = get_request_site_address(True) \
|
||||||
+ "?cmd=setup.doctype.backup_manager.backup_dropbox.dropbox_callback"
|
+ "?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 {
|
return {
|
||||||
"url": url,
|
"url": url,
|
||||||
"key": request_token.key,
|
"key": request_token.key,
|
||||||
@@ -21,31 +20,32 @@ def get_dropbox_authorize_url():
|
|||||||
|
|
||||||
@webnotes.whitelist(allow_guest=True)
|
@webnotes.whitelist(allow_guest=True)
|
||||||
def dropbox_callback(oauth_token=None, not_approved=False):
|
def dropbox_callback(oauth_token=None, not_approved=False):
|
||||||
|
from dropbox import client
|
||||||
if not not_approved:
|
if not not_approved:
|
||||||
if webnotes.conn.get_value("Backup Manager", None, "dropbox_access_key")==oauth_token:
|
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."
|
message = "Dropbox access allowed."
|
||||||
|
|
||||||
sess = get_dropbox_session()
|
sess = get_dropbox_session()
|
||||||
sess.set_request_token(webnotes.conn.get_value("Backup Manager", None, "dropbox_access_key"),
|
sess.set_request_token(webnotes.conn.get_value("Backup Manager", None, "dropbox_access_key"),
|
||||||
webnotes.conn.get_value("Backup Manager", None, "dropbox_access_secret"))
|
webnotes.conn.get_value("Backup Manager", None, "dropbox_access_secret"))
|
||||||
access_token = sess.obtain_access_token()
|
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:
|
else:
|
||||||
webnotes.conn.set_value("Backup Manager", "Backup Manager", "dropbox_access_allowed", 0)
|
allowed = 0
|
||||||
message = "Illegal Access Token Please try again."
|
message = "Illegal Access Token Please try again."
|
||||||
else:
|
else:
|
||||||
webnotes.conn.set_value("Backup Manager", "Backup Manager", "dropbox_access_allowed", 0)
|
allowed = 0
|
||||||
message = "Dropbox Access not approved."
|
message = "Dropbox Access not approved."
|
||||||
|
|
||||||
webnotes.message_title = "Dropbox Approval"
|
webnotes.message_title = "Dropbox Approval"
|
||||||
webnotes.message = "<h3>%s</h3><p>Please close this window.</p>" % message
|
webnotes.message = "<h3>%s</h3><p>Please close this window.</p>" % message
|
||||||
|
|
||||||
webnotes.conn.commit()
|
webnotes.conn.commit()
|
||||||
webnotes.response['type'] = 'page'
|
webnotes.response['type'] = 'page'
|
||||||
webnotes.response['page_name'] = 'message.html'
|
webnotes.response['page_name'] = 'message.html'
|
||||||
@@ -57,7 +57,6 @@ def backup_to_dropbox():
|
|||||||
if not webnotes.conn:
|
if not webnotes.conn:
|
||||||
webnotes.connect()
|
webnotes.connect()
|
||||||
|
|
||||||
|
|
||||||
sess = session.DropboxSession(dropbox_access_key, dropbox_secret_key, "app_folder")
|
sess = session.DropboxSession(dropbox_access_key, dropbox_secret_key, "app_folder")
|
||||||
|
|
||||||
sess.set_token(webnotes.conn.get_value("Backup Manager", None, "dropbox_access_key"),
|
sess.set_token(webnotes.conn.get_value("Backup Manager", None, "dropbox_access_key"),
|
||||||
@@ -69,19 +68,20 @@ def backup_to_dropbox():
|
|||||||
backup = new_backup()
|
backup = new_backup()
|
||||||
filename = backup.backup_path_db
|
filename = backup.backup_path_db
|
||||||
upload_file_to_dropbox(filename, "database", dropbox_client)
|
upload_file_to_dropbox(filename, "database", dropbox_client)
|
||||||
# upload files
|
|
||||||
response = dropbox_client.metadata("/database")
|
|
||||||
|
|
||||||
#add missing files
|
response = dropbox_client.metadata("/files")
|
||||||
# for filename in os.listdir(filename):
|
|
||||||
# found = False
|
# upload files to files folder
|
||||||
# for file_metadata in response["contents"]:
|
filename = os.path.join(get_base_path(),"public", "files")
|
||||||
# if filename==os.path.basename(file_metadata["path"]):
|
for filename in os.listdir(filename):
|
||||||
# if os.stat(filename).st_size==file_metadata["bytes"]:
|
found = False
|
||||||
# found=True
|
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:
|
if not found:
|
||||||
# upload_file_to_dropbox(filename, "database", dropbox_client)
|
upload_file_to_dropbox(os.path.join(get_base_path(),"public", "files", filename), "files", dropbox_client)
|
||||||
|
|
||||||
def get_dropbox_session():
|
def get_dropbox_session():
|
||||||
from dropbox import session
|
from dropbox import session
|
||||||
@@ -94,21 +94,18 @@ def get_dropbox_session():
|
|||||||
return sess
|
return sess
|
||||||
|
|
||||||
def upload_file_to_dropbox(filename, folder, dropbox_client):
|
def upload_file_to_dropbox(filename, folder, dropbox_client):
|
||||||
path = os.path.join(get_base_path(),"public", "backups")
|
size = os.stat(filename).st_size
|
||||||
for files in os.listdir(path):
|
f = open(filename,'r')
|
||||||
file_name = path + "/" + files
|
if size > 4194304:
|
||||||
size = os.stat(file_name).st_size
|
uploader = dropbox_client.get_chunked_uploader(f, size)
|
||||||
f = open(filename,'r')
|
while uploader.offset < size:
|
||||||
|
try:
|
||||||
if size > 4194304:
|
uploader.upload_chunked()
|
||||||
uploader = dropbox_client.get_chunked_uploader(f, size)
|
finish(folder + '/' + os.path.basename(filename), overwrite='True')
|
||||||
while uploader.offset < size:
|
except rest.ErrorResponse, e:
|
||||||
try:
|
pass
|
||||||
uploader.upload_chunked()
|
else:
|
||||||
except rest.ErrorResponse, e:
|
response = dropbox_client.put_file(folder + "/" + os.path.basename(filename), f, overwrite=True)
|
||||||
pass
|
|
||||||
else:
|
|
||||||
response = dropbox_client.put_file(folder + "/" + os.path.basename(file_name), f, overwrite=True)
|
|
||||||
|
|
||||||
if __name__=="__main__":
|
if __name__=="__main__":
|
||||||
backup_to_dropbox()
|
backup_to_dropbox()
|
||||||
@@ -1,24 +1,66 @@
|
|||||||
|
cur_frm.cscript.refresh = function(doc) {
|
||||||
|
cur_frm.disable_save();
|
||||||
|
}
|
||||||
|
|
||||||
|
//dropbox
|
||||||
cur_frm.cscript.allow_dropbox_access = function(doc) {
|
cur_frm.cscript.allow_dropbox_access = function(doc) {
|
||||||
wn.call({
|
if (doc.send_notifications_to == '') {
|
||||||
method: "setup.doctype.backup_manager.backup_dropbox.get_dropbox_authorize_url",
|
msgprint("Please enter email address.")
|
||||||
callback: function(r) {
|
}
|
||||||
if(!r.exc) {
|
else {
|
||||||
cur_frm.set_value("dropbox_access_secret", r.message.secret);
|
wn.call({
|
||||||
cur_frm.set_value("dropbox_access_key", r.message.key);
|
method: "setup.doctype.backup_manager.backup_dropbox.get_dropbox_authorize_url",
|
||||||
cur_frm.save(null, function() {
|
callback: function(r) {
|
||||||
window.open(r.message.url);
|
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) {
|
cur_frm.cscript.backup_right_now = function(doc) {
|
||||||
msgprint("Backing up and uploading. This may take a few minutes.")
|
msgprint("Backing up and uploading. This may take a few minutes.")
|
||||||
wn.call({
|
wn.call({
|
||||||
method: "setup.doctype.backup_manager.backup_manager.take_backups",
|
method: "setup.doctype.backup_manager.backup_manager.take_backups_dropbox",
|
||||||
callback: function(r) {
|
callback: function(r) {
|
||||||
msgprint("Backups taken. Please check your email for the response.")
|
msgprint("Backups taken. Please check your email for the response.")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
//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.")
|
||||||
|
// }
|
||||||
|
// })
|
||||||
|
// }
|
||||||
@@ -4,6 +4,7 @@ from __future__ import unicode_literals
|
|||||||
import webnotes
|
import webnotes
|
||||||
from webnotes import _
|
from webnotes import _
|
||||||
from backup_dropbox import dropbox_callback, get_dropbox_session, get_dropbox_authorize_url
|
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:
|
class DocType:
|
||||||
def __init__(self, d, dl):
|
def __init__(self, d, dl):
|
||||||
@@ -17,16 +18,28 @@ 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()
|
take_backups_dropbox()
|
||||||
|
take_backups_gdrive()
|
||||||
|
#backup to dropbox
|
||||||
@webnotes.whitelist()
|
@webnotes.whitelist()
|
||||||
def take_backups():
|
def take_backups_dropbox():
|
||||||
try:
|
try:
|
||||||
from setup.doctype.backup_manager.backup_dropbox import backup_to_dropbox
|
from setup.doctype.backup_manager.backup_dropbox import backup_to_dropbox
|
||||||
backup_to_dropbox()
|
backup_to_dropbox()
|
||||||
send_email(True, "Dropbox")
|
send_email(True, "Dropbox")
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
send_email(False, "Dropbox", 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):
|
def send_email(success, service_name, error_status=None):
|
||||||
if success:
|
if success:
|
||||||
subject = "Backup Upload Successful"
|
subject = "Backup Upload Successful"
|
||||||
@@ -45,6 +58,4 @@ def send_email(success, service_name, error_status=None):
|
|||||||
# email system managers
|
# email system managers
|
||||||
from webnotes.utils.email_lib import sendmail
|
from webnotes.utils.email_lib import sendmail
|
||||||
sendmail(webnotes.conn.get_value("Backup Manager", None, "send_notifications_to").split(","),
|
sendmail(webnotes.conn.get_value("Backup Manager", None, "send_notifications_to").split(","),
|
||||||
subject=subject, msg=message)
|
subject=subject, msg=message)
|
||||||
|
|
||||||
get_dropbox_authorize_url()
|
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
[
|
[
|
||||||
{
|
{
|
||||||
"creation": "2013-03-05 16:35:50",
|
"creation": "2013-03-15 11:06:59",
|
||||||
"docstatus": 0,
|
"docstatus": 0,
|
||||||
"modified": "2013-03-07 12:18:07",
|
"modified": "2013-03-15 16:10:05",
|
||||||
"modified_by": "Administrator",
|
"modified_by": "Administrator",
|
||||||
"owner": "Administrator"
|
"owner": "Administrator"
|
||||||
},
|
},
|
||||||
@@ -56,7 +56,9 @@
|
|||||||
"doctype": "DocField",
|
"doctype": "DocField",
|
||||||
"fieldname": "backup_right_now",
|
"fieldname": "backup_right_now",
|
||||||
"fieldtype": "Button",
|
"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.",
|
"description": "Note: Backups and files are not deleted from Dropbox, you will have to delete them manually.",
|
||||||
@@ -102,6 +104,70 @@
|
|||||||
"fieldtype": "Button",
|
"fieldtype": "Button",
|
||||||
"label": "Allow Dropbox Access"
|
"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"
|
"doctype": "DocPerm"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user