No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

222 líneas
7.2 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. """
  5. This module handles the On Demand Backup utility
  6. To setup in defs set:
  7. backup_path: path where backups will be taken (for eg /backups)
  8. backup_link_path: download link for backups (eg /var/www/wnframework/backups)
  9. backup_url: base url of the backup folder (eg http://mysite.com/backups)
  10. """
  11. #Imports
  12. import os, webnotes
  13. from datetime import datetime
  14. #Global constants
  15. verbose = 0
  16. import conf
  17. #-------------------------------------------------------------------------------
  18. class BackupGenerator:
  19. """
  20. This class contains methods to perform On Demand Backup
  21. To initialize, specify (db_name, user, password, db_file_name=None)
  22. If specifying db_file_name, also append ".sql.gz"
  23. """
  24. def __init__(self, db_name, user, password):
  25. self.db_name = db_name
  26. self.user = user
  27. self.password = password
  28. self.backup_path_files = None
  29. self.backup_path_db = None
  30. def get_backup(self, older_than=24, ignore_files=False):
  31. """
  32. Takes a new dump if existing file is old
  33. and sends the link to the file as email
  34. """
  35. #Check if file exists and is less than a day old
  36. #If not Take Dump
  37. self.get_recent_backup(older_than)
  38. if not (self.backup_path_files and self.backup_path_db):
  39. self.set_backup_file_name()
  40. self.take_dump()
  41. if not ignore_files:
  42. self.zip_files()
  43. def set_backup_file_name(self):
  44. import random
  45. todays_date = "".join(str(datetime.date(datetime.today())).split("-"))
  46. random_number = str(int(random.random()*99999999))
  47. #Generate a random name using today's date and a 8 digit random number
  48. for_db = todays_date + "_" + random_number + "_database.sql.gz"
  49. for_files = todays_date + "_" + random_number + "_files.tar"
  50. backup_path = get_backup_path()
  51. self.backup_path_db = os.path.join(backup_path, for_db)
  52. self.backup_path_files = os.path.join(backup_path, for_files)
  53. def get_recent_backup(self, older_than):
  54. file_list = os.listdir(get_backup_path())
  55. for this_file in file_list:
  56. this_file_path = os.path.join(get_backup_path(), this_file)
  57. if not is_file_old(this_file_path, older_than):
  58. if "_files" in this_file_path:
  59. self.backup_path_files = this_file_path
  60. if "_database" in this_file_path:
  61. self.backup_path_db = this_file_path
  62. def zip_files(self):
  63. # TODO use get_storage_base_path
  64. files_path = os.path.join(os.path.dirname(os.path.abspath(conf.__file__)), 'public', 'files')
  65. cmd_string = """tar -cf %s %s""" % (self.backup_path_files, files_path)
  66. err, out = webnotes.utils.execute_in_shell(cmd_string)
  67. def take_dump(self):
  68. import webnotes.utils
  69. # escape reserved characters
  70. args = dict([item[0], webnotes.utils.esc(item[1], '$ ')]
  71. for item in self.__dict__.copy().items())
  72. cmd_string = """mysqldump -u %(user)s -p%(password)s %(db_name)s | gzip -c > %(backup_path_db)s""" % args
  73. err, out = webnotes.utils.execute_in_shell(cmd_string)
  74. def send_email(self):
  75. """
  76. Sends the link to backup file located at erpnext/backups
  77. """
  78. from webnotes.utils.email_lib import sendmail, get_system_managers
  79. backup_url = webnotes.conn.get_value('Website Settings',
  80. 'Website Settings', 'subdomain') or ''
  81. backup_url = os.path.join('http://' + backup_url, 'backups')
  82. recipient_list = get_system_managers()
  83. msg = """<p>Hello,</p>
  84. <p>Your backups are ready to be downloaded.</p>
  85. <p>1. <a href="%(db_backup_url)s">Click here to download\
  86. the database backup</a></p>
  87. <p>2. <a href="%(files_backup_url)s">Click here to download\
  88. the files backup</a></p>
  89. <p>This link will be valid for 24 hours. A new backup will be available
  90. for download only after 24 hours.</p>
  91. <p>Have a nice day!<br>ERPNext</p>""" % {
  92. "db_backup_url": os.path.join(backup_url, os.path.basename(self.backup_path_db)),
  93. "files_backup_url": os.path.join(backup_url, os.path.basename(self.backup_path_files))
  94. }
  95. datetime_str = datetime.fromtimestamp(os.stat(self.backup_path_db).st_ctime)
  96. subject = datetime_str.strftime("%d/%m/%Y %H:%M:%S") + """ - Backup ready to be downloaded"""
  97. sendmail(recipients=recipient_list, msg=msg, subject=subject)
  98. return recipient_list
  99. @webnotes.whitelist()
  100. def get_backup():
  101. """
  102. This function is executed when the user clicks on
  103. Toos > Download Backup
  104. """
  105. #if verbose: print webnotes.conn.cur_db_name + " " + conf.db_password
  106. delete_temp_backups()
  107. odb = BackupGenerator(webnotes.conn.cur_db_name, webnotes.conn.cur_db_name,\
  108. webnotes.get_db_password(webnotes.conn.cur_db_name))
  109. odb.get_backup()
  110. recipient_list = odb.send_email()
  111. webnotes.msgprint("""A download link to your backup will be emailed \
  112. to you shortly on the following email address:
  113. %s""" % (', '.join(recipient_list)))
  114. def scheduled_backup(older_than=6, ignore_files=False):
  115. """this function is called from scheduler
  116. deletes backups older than 7 days
  117. takes backup"""
  118. odb = new_backup(older_than, ignore_files)
  119. from webnotes.utils import now
  120. print "backup taken -", odb.backup_path_db, "- on", now()
  121. def new_backup(older_than=6, ignore_files=False):
  122. delete_temp_backups(older_than=168)
  123. odb = BackupGenerator(webnotes.conn.cur_db_name, webnotes.conn.cur_db_name,\
  124. webnotes.get_db_password(webnotes.conn.cur_db_name))
  125. odb.get_backup(older_than, ignore_files)
  126. return odb
  127. def delete_temp_backups(older_than=24):
  128. """
  129. Cleans up the backup_link_path directory by deleting files older than 24 hours
  130. """
  131. file_list = os.listdir(get_backup_path())
  132. for this_file in file_list:
  133. this_file_path = os.path.join(get_backup_path(), this_file)
  134. if is_file_old(this_file_path, older_than):
  135. os.remove(this_file_path)
  136. def is_file_old(db_file_name, older_than=24):
  137. """
  138. Checks if file exists and is older than specified hours
  139. Returns ->
  140. True: file does not exist or file is old
  141. False: file is new
  142. """
  143. if os.path.isfile(db_file_name):
  144. from datetime import timedelta
  145. import time
  146. #Get timestamp of the file
  147. file_datetime = datetime.fromtimestamp\
  148. (os.stat(db_file_name).st_ctime)
  149. if datetime.today() - file_datetime >= timedelta(hours = older_than):
  150. if verbose: print "File is old"
  151. return True
  152. else:
  153. if verbose: print "File is recent"
  154. return False
  155. else:
  156. if verbose: print "File does not exist"
  157. return True
  158. backup_path = None
  159. def get_backup_path():
  160. global backup_path
  161. if not backup_path:
  162. import os, conf
  163. # TODO Use get_storage_base_path
  164. backup_path = os.path.join(os.path.dirname(os.path.abspath(conf.__file__)),
  165. 'public', 'backups')
  166. return backup_path
  167. #-------------------------------------------------------------------------------
  168. if __name__ == "__main__":
  169. """
  170. is_file_old db_name user password
  171. get_backup db_name user password
  172. """
  173. import sys
  174. cmd = sys.argv[1]
  175. if cmd == "is_file_old":
  176. odb = BackupGenerator(sys.argv[2], sys.argv[3], sys.argv[4])
  177. is_file_old(odb.db_file_name)
  178. if cmd == "get_backup":
  179. odb = BackupGenerator(sys.argv[2], sys.argv[3], sys.argv[4])
  180. odb.get_backup()
  181. if cmd == "take_dump":
  182. odb = BackupGenerator(sys.argv[2], sys.argv[3], sys.argv[4])
  183. odb.take_dump()
  184. if cmd == "send_email":
  185. odb = BackupGenerator(sys.argv[2], sys.argv[3], sys.argv[4])
  186. odb.send_email("abc.sql.gz")
  187. if cmd == "delete_temp_backups":
  188. delete_temp_backups()