# Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com) # # MIT License (MIT) # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), # to deal in the Software without restriction, including without limitation # the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # from __future__ import unicode_literals """ This module handles the On Demand Backup utility To setup in defs set: backup_path: path where backups will be taken (for eg /backups) backup_link_path: download link for backups (eg /var/www/wnframework/backups) backup_url: base url of the backup folder (eg http://mysite.com/backups) """ #Imports import os, webnotes from datetime import datetime #Global constants verbose = 0 import conf #------------------------------------------------------------------------------- class BackupGenerator: """ This class contains methods to perform On Demand Backup 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): self.db_name = db_name self.user = user self.password = password self.backup_path_files = None self.backup_path_db = None def get_backup(self): """ Takes a new dump if existing file is old and sends the link to the file as email """ #Check if file exists and is less than a day old #If not Take Dump self.get_recent_backup() if not (self.backup_path_files and self.backup_path_db): self.set_backup_file_name() self.take_dump() self.zip_files() def set_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 for_db = todays_date + "_" + random_number + "_database.sql.gz" for_files = todays_date + "_" + random_number + "_files.tar" backup_path = get_backup_path() self.backup_path_db = os.path.join(backup_path, for_db) self.backup_path_files = os.path.join(backup_path, for_files) def get_recent_backup(self): file_list = os.listdir(get_backup_path()) for this_file in file_list: this_file_path = os.path.join(get_backup_path(), this_file) if not is_file_old(this_file_path): if "_files" in this_file_path: self.backup_path_files = this_file_path if "_database" in this_file_path: self.backup_path_db = this_file_path def zip_files(self): files_path = os.path.join(os.path.dirname(os.path.abspath(conf.__file__)), 'public', 'files') cmd_string = """tar -cf %s %s""" % (self.backup_path_files, files_path) err, out = webnotes.utils.execute_in_shell(cmd_string) def take_dump(self): import webnotes.utils # escape reserved characters args = dict([item[0], webnotes.utils.esc(item[1], '$ ')] for item in self.__dict__.copy().items()) cmd_string = """mysqldump -u %(user)s -p%(password)s %(db_name)s | gzip -c > %(backup_path_db)s""" % args err, out = webnotes.utils.execute_in_shell(cmd_string) def get_recipients(self): """ Get recepient's email address """ #import webnotes.db #webnotes.conn = webnotes.db.Database(use_default=1) recipient_list = webnotes.conn.sql(\ """SELECT parent FROM tabUserRole WHERE role='System Manager' AND parent!='Administrator' AND parent IN (SELECT email FROM tabProfile WHERE enabled=1)""") return [i[0] for i in recipient_list] def send_email(self): """ Sends the link to backup file located at erpnext/backups """ from webnotes.utils.email_lib import sendmail backup_url = webnotes.conn.get_value('Website Settings', 'Website Settings', 'subdomain') or '' backup_url = os.path.join('http://' + backup_url, 'backups') recipient_list = self.get_recipients() msg = """
Hello,
Your backups are ready to be downloaded.
1. Click here to download\ the database backup
2. Click here to download\ the files backup
This link will be valid for 24 hours. A new backup will be available for download only after 24 hours.
Have a nice day!
ERPNext