Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

218 lignes
6.9 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. from __future__ import unicode_literals
  23. """
  24. This module handles the On Demand Backup utility
  25. To setup in defs set:
  26. backup_path: path where backups will be taken (for eg /backups)
  27. backup_link_path: download link for backups (eg /var/www/wnframework/backups)
  28. backup_url: base url of the backup folder (eg http://mysite.com/backups)
  29. """
  30. #Imports
  31. import os, webnotes
  32. from datetime import datetime
  33. #Global constants
  34. verbose = 0
  35. import conf
  36. #-------------------------------------------------------------------------------
  37. class BackupGenerator:
  38. """
  39. This class contains methods to perform On Demand Backup
  40. To initialize, specify (db_name, user, password, db_file_name=None)
  41. If specifying db_file_name, also append ".sql.gz"
  42. """
  43. def __init__(self, db_name, user, password):
  44. self.db_name = db_name.replace('$', '\$')
  45. self.user = user
  46. self.password = password
  47. self.backup_file_name = self.get_backup_file_name()
  48. self.backup_file_path = os.path.join(conf.backup_path, self.backup_file_name)
  49. def get_backup_file_name(self):
  50. import random
  51. todays_date = "".join(str(datetime.date(datetime.today())).split("-"))
  52. random_number = str(int(random.random()*99999999))
  53. #Generate a random name using today's date and a 8 digit random number
  54. random_name = todays_date + "_" + random_number + ".sql.gz"
  55. return random_name
  56. def take_dump(self):
  57. """
  58. Dumps a db via mysqldump
  59. """
  60. cmd_string = """mysqldump -u %(user)s -p%(password)s %(db_name)s |
  61. gzip -c > %(backup_file_path)s""" % self.__dict__
  62. os.system(cmd_string)
  63. def get_recipients(self):
  64. """
  65. Get recepient's email address
  66. """
  67. #import webnotes.db
  68. #webnotes.conn = webnotes.db.Database(use_default=1)
  69. recipient_list = webnotes.conn.sql(\
  70. """SELECT parent FROM tabUserRole
  71. WHERE role='System Manager'
  72. AND parent!='Administrator'
  73. AND parent IN
  74. (SELECT email FROM tabProfile WHERE enabled=1)""")
  75. return [i[0] for i in recipient_list]
  76. def send_email(self, backup_file):
  77. """
  78. Sends the link to backup file located at erpnext/backups
  79. """
  80. backup_url = webnotes.conn.get_value('Website Settings',
  81. 'Website Settings', 'subdomain') or ''
  82. backup_url = os.path.join('http://' + backup_url, 'backups')
  83. file_url = os.path.join(backup_url, backup_file)
  84. from webnotes.utils.email_lib import sendmail
  85. recipient_list = self.get_recipients()
  86. msg = """<a href="%(file_url)s">Click here to begin downloading\
  87. your backup</a>
  88. This link will be valid for 24 hours.
  89. Also, a new backup will be available for download (if requested)\
  90. only after 24 hours.""" % {"file_url":file_url}
  91. backup_file_path = os.path.join(conf.backup_path, backup_file)
  92. datetime_str = datetime.fromtimestamp(os.stat(backup_file_path).st_ctime)
  93. subject = datetime_str.strftime("%d/%m/%Y %H:%M:%S") + """ - Backup ready to be downloaded"""
  94. sendmail(recipients=recipient_list, msg=msg, subject=subject)
  95. return recipient_list
  96. def get_backup(self):
  97. """
  98. Takes a new dump if existing file is old
  99. and sends the link to the file as email
  100. """
  101. #Check if file exists and is less than a day old
  102. #If not Take Dump
  103. backup_file = recent_backup_exists()
  104. if not backup_file:
  105. self.take_dump()
  106. backup_file = self.backup_file_name
  107. #Email Link
  108. recipient_list = self.send_email(backup_file)
  109. return recipient_list
  110. @webnotes.whitelist()
  111. def get_backup():
  112. """
  113. This function is executed when the user clicks on
  114. Toos > Download Backup
  115. """
  116. #if verbose: print webnotes.conn.cur_db_name + " " + conf.db_password
  117. delete_temp_backups()
  118. odb = BackupGenerator(webnotes.conn.cur_db_name, webnotes.conn.cur_db_name,\
  119. webnotes.get_db_password(webnotes.conn.cur_db_name))
  120. recipient_list = odb.get_backup()
  121. webnotes.msgprint("""A download link to your backup will be emailed \
  122. to you shortly on the following email address:
  123. %s""" % (', '.join(recipient_list)))
  124. def recent_backup_exists():
  125. file_list = os.listdir(conf.backup_path)
  126. for this_file in file_list:
  127. this_file_path = os.path.join(conf.backup_path, this_file)
  128. if not is_file_old(this_file_path):
  129. return this_file
  130. return None
  131. def delete_temp_backups():
  132. """
  133. Cleans up the backup_link_path directory by deleting files older than 24 hours
  134. """
  135. file_list = os.listdir(conf.backup_path)
  136. for this_file in file_list:
  137. this_file_path = os.path.join(conf.backup_path, this_file)
  138. if is_file_old(this_file_path):
  139. os.remove(this_file_path)
  140. def is_file_old(db_file_name, older_than=24):
  141. """
  142. Checks if file exists and is older than specified hours
  143. Returns ->
  144. True: file does not exist or file is old
  145. False: file is new
  146. """
  147. if os.path.isfile(db_file_name):
  148. from datetime import timedelta
  149. import time
  150. #Get timestamp of the file
  151. file_datetime = datetime.fromtimestamp\
  152. (os.stat(db_file_name).st_ctime)
  153. if datetime.today() - file_datetime >= timedelta(hours = older_than):
  154. if verbose: print "File is old"
  155. return True
  156. else:
  157. if verbose: print "File is recent"
  158. return False
  159. else:
  160. if verbose: print "File does not exist"
  161. return True
  162. #-------------------------------------------------------------------------------
  163. if __name__ == "__main__":
  164. """
  165. is_file_old db_name user password
  166. get_backup db_name user password
  167. """
  168. import sys
  169. cmd = sys.argv[1]
  170. if cmd == "is_file_old":
  171. odb = BackupGenerator(sys.argv[2], sys.argv[3], sys.argv[4])
  172. is_file_old(odb.db_file_name)
  173. if cmd == "get_backup":
  174. odb = BackupGenerator(sys.argv[2], sys.argv[3], sys.argv[4])
  175. odb.get_backup()
  176. if cmd == "take_dump":
  177. odb = BackupGenerator(sys.argv[2], sys.argv[3], sys.argv[4])
  178. odb.take_dump()
  179. if cmd == "send_email":
  180. odb = BackupGenerator(sys.argv[2], sys.argv[3], sys.argv[4])
  181. odb.send_email("abc.sql.gz")
  182. if cmd == "delete_temp_backups":
  183. delete_temp_backups()