Merge branch 'shf_rename' of github.com:webnotes/wnframework into shf_rename
This commit is contained in:
commit
10dc071ce6
1 changed files with 35 additions and 46 deletions
|
|
@ -34,12 +34,8 @@ from datetime import datetime
|
|||
|
||||
|
||||
#Global constants
|
||||
import conf
|
||||
from conf import backup_path
|
||||
subdomain = webnotes.conn.get_value('Website Settings', 'Website Settings', 'subdomain')
|
||||
backup_link_path = "/".join(['http:/', subdomain, 'backups'])
|
||||
verbose = 0
|
||||
|
||||
import conf
|
||||
#-------------------------------------------------------------------------------
|
||||
class BackupGenerator:
|
||||
"""
|
||||
|
|
@ -48,39 +44,30 @@ class BackupGenerator:
|
|||
To initialize, specify (db_name, user, password, db_file_name=None)
|
||||
If specifying db_file_name, also append ".sql.gz"
|
||||
"""
|
||||
def __init__(self, db_name, user, password, db_file_name=None):
|
||||
def __init__(self, db_name, user, password):
|
||||
self.db_name = db_name.replace('$', '\$')
|
||||
self.user = user
|
||||
self.password = password
|
||||
self.db_file_name = db_file_name and db_file_name \
|
||||
or (os.path.join(backup_path, self.db_name + ".sql.gz"))
|
||||
|
||||
def take_dump(self):
|
||||
"""
|
||||
Dumps a db via mysqldump
|
||||
"""
|
||||
os.system("""mysqldump -u %(user)s -p%(password)s %(db_name)s |
|
||||
gzip -c > %(db_file_name)s""" % self.__dict__)
|
||||
self.backup_file_name = self.get_backup_file_name()
|
||||
self.backup_file_path = os.path.join(conf.backup_path, self.backup_file_name)
|
||||
|
||||
|
||||
def copy_to_backup_link(self):
|
||||
"""
|
||||
Copies the backup file from backup path to shared link path
|
||||
It also gives the file a random name, thus hiding the db_name
|
||||
"""
|
||||
def get_backup_file_name(self):
|
||||
import random
|
||||
todays_date = "".join(str(datetime.date(datetime.today())).split("-"))
|
||||
random_number = str(int(random.random()*99999999))
|
||||
|
||||
#Generate a random name using today's date and a 8 digit random number
|
||||
random_name = todays_date + "_" + random_number + ".sql.gz"
|
||||
|
||||
os.system("""cp -f %(src_file)s %(dest_file)s""" % \
|
||||
{"src_file":self.db_file_name,
|
||||
"dest_file":os.path.join(backup_link_path, random_name)})
|
||||
if verbose: print "file copied"
|
||||
return random_name
|
||||
|
||||
def take_dump(self):
|
||||
"""
|
||||
Dumps a db via mysqldump
|
||||
"""
|
||||
cmd_string = """mysqldump -u %(user)s -p%(password)s %(db_name)s |
|
||||
gzip -c > %(backup_file_path)s""" % self.__dict__
|
||||
webnotes.msgprint(cmd_string)
|
||||
os.system(cmd_string)
|
||||
|
||||
def get_recipients(self):
|
||||
"""
|
||||
|
|
@ -101,15 +88,9 @@ class BackupGenerator:
|
|||
"""
|
||||
Sends the link to backup file located at erpnext/backups
|
||||
"""
|
||||
import conf
|
||||
if hasattr(conf, 'backup_url'):
|
||||
backup_url = conf.backup_url
|
||||
else:
|
||||
backup_url = webnotes.conn.get_value('Website Settings',
|
||||
'Website Settings', 'subdomain') or ''
|
||||
if hasattr(conf, 'backup_folder_name'):
|
||||
backup_url = os.path.join(backup_url,
|
||||
conf.backup_folder_name)
|
||||
backup_url = webnotes.conn.get_value('Website Settings',
|
||||
'Website Settings', 'subdomain') or ''
|
||||
backup_url = os.path.join(backup_url, 'backups')
|
||||
file_url = os.path.join(backup_url, backup_file)
|
||||
from webnotes.utils.email_lib import sendmail
|
||||
|
||||
|
|
@ -122,7 +103,8 @@ class BackupGenerator:
|
|||
Also, a new backup will be available for download (if requested)\
|
||||
only after 24 hours.""" % {"file_url":file_url}
|
||||
|
||||
datetime_str = datetime.fromtimestamp(os.stat(self.db_file_name.replace('\$', '$')).st_ctime)
|
||||
backup_file_path = os.path.join(conf.backup_path, backup_file)
|
||||
datetime_str = datetime.fromtimestamp(os.stat(backup_file_path).st_ctime)
|
||||
|
||||
subject = datetime_str.strftime("%d/%m/%Y %H:%M:%S") + """ - Backup ready to be downloaded"""
|
||||
sendmail(recipients=recipient_list, msg=msg, subject=subject, from_defs=1)
|
||||
|
|
@ -136,19 +118,18 @@ class BackupGenerator:
|
|||
"""
|
||||
#Check if file exists and is less than a day old
|
||||
#If not Take Dump
|
||||
if is_file_old(self.db_file_name):
|
||||
self.take_dump()
|
||||
|
||||
#Copy file to backup_link_path
|
||||
#This is a security hole. When the user calls get_backup repeatedly
|
||||
#a file will be generated each time.
|
||||
backup_file = self.copy_to_backup_link()
|
||||
backup_file = recent_backup_exists()
|
||||
|
||||
if not backup_file:
|
||||
self.take_dump()
|
||||
backup_file = self.backup_file_name
|
||||
|
||||
#Email Link
|
||||
recipient_list = self.send_email(backup_file)
|
||||
|
||||
return recipient_list
|
||||
|
||||
|
||||
@webnotes.whitelist()
|
||||
def get_backup():
|
||||
"""
|
||||
|
|
@ -156,21 +137,29 @@ def get_backup():
|
|||
Toos > Download Backup
|
||||
"""
|
||||
#if verbose: print webnotes.conn.cur_db_name + " " + conf.db_password
|
||||
delete_temp_backups()
|
||||
odb = BackupGenerator(webnotes.conn.cur_db_name, webnotes.conn.cur_db_name,\
|
||||
webnotes.get_db_password(webnotes.conn.cur_db_name))
|
||||
recipient_list = odb.get_backup()
|
||||
delete_temp_backups()
|
||||
webnotes.msgprint("""A download link to your backup will be emailed \
|
||||
to you shortly on the following email address:
|
||||
%s""" % (', '.join(recipient_list)))
|
||||
|
||||
def recent_backup_exists():
|
||||
file_list = os.listdir(conf.backup_path)
|
||||
for this_file in file_list:
|
||||
this_file_path = os.path.join(conf.backup_path, this_file)
|
||||
if not is_file_old(this_file_path):
|
||||
return this_file
|
||||
return None
|
||||
|
||||
def delete_temp_backups():
|
||||
"""
|
||||
Cleans up the backup_link_path directory by deleting files older than 24 hours
|
||||
"""
|
||||
file_list = os.listdir(backup_link_path)
|
||||
file_list = os.listdir(conf.backup_path)
|
||||
for this_file in file_list:
|
||||
this_file_path = os.path.join(backup_link_path, this_file)
|
||||
this_file_path = os.path.join(conf.backup_path, this_file)
|
||||
if is_file_old(this_file_path):
|
||||
os.remove(this_file_path)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue