Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

249 rader
8.4 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
  45. self.user = user
  46. self.password = password
  47. self.backup_path_files = None
  48. self.backup_path_db = None
  49. def get_backup(self):
  50. """
  51. Takes a new dump if existing file is old
  52. and sends the link to the file as email
  53. """
  54. #Check if file exists and is less than a day old
  55. #If not Take Dump
  56. self.get_recent_backup()
  57. if not (self.backup_path_files and self.backup_path_db):
  58. self.set_backup_file_name()
  59. self.take_dump()
  60. self.zip_files()
  61. def set_backup_file_name(self):
  62. import random
  63. todays_date = "".join(str(datetime.date(datetime.today())).split("-"))
  64. random_number = str(int(random.random()*99999999))
  65. #Generate a random name using today's date and a 8 digit random number
  66. for_db = todays_date + "_" + random_number + "_database.sql.gz"
  67. for_files = todays_date + "_" + random_number + "_files.tar"
  68. backup_path = get_backup_path()
  69. self.backup_path_db = os.path.join(backup_path, for_db)
  70. self.backup_path_files = os.path.join(backup_path, for_files)
  71. def get_recent_backup(self):
  72. file_list = os.listdir(get_backup_path())
  73. for this_file in file_list:
  74. this_file_path = os.path.join(get_backup_path(), this_file)
  75. if not is_file_old(this_file_path):
  76. if "_files" in this_file_path:
  77. self.backup_path_files = this_file_path
  78. if "_database" in this_file_path:
  79. self.backup_path_db = this_file_path
  80. def zip_files(self):
  81. files_path = os.path.join(os.path.dirname(os.path.abspath(conf.__file__)), 'public', 'files')
  82. cmd_string = """tar -cf %s %s""" % (self.backup_path_files, files_path)
  83. err, out = webnotes.utils.execute_in_shell(cmd_string)
  84. def take_dump(self):
  85. import webnotes.utils
  86. # escape reserved characters
  87. args = dict([item[0], webnotes.utils.esc(item[1], '$ ')]
  88. for item in self.__dict__.copy().items())
  89. cmd_string = """mysqldump -u %(user)s -p%(password)s %(db_name)s | gzip -c > %(backup_path_db)s""" % args
  90. err, out = webnotes.utils.execute_in_shell(cmd_string)
  91. def get_recipients(self):
  92. """
  93. Get recepient's email address
  94. """
  95. #import webnotes.db
  96. #webnotes.conn = webnotes.db.Database(use_default=1)
  97. recipient_list = webnotes.conn.sql(\
  98. """SELECT parent FROM tabUserRole
  99. WHERE role='System Manager'
  100. AND parent!='Administrator'
  101. AND parent IN
  102. (SELECT email FROM tabProfile WHERE enabled=1)""")
  103. return [i[0] for i in recipient_list]
  104. def send_email(self):
  105. """
  106. Sends the link to backup file located at erpnext/backups
  107. """
  108. from webnotes.utils.email_lib import sendmail
  109. backup_url = webnotes.conn.get_value('Website Settings',
  110. 'Website Settings', 'subdomain') or ''
  111. backup_url = os.path.join('http://' + backup_url, 'backups')
  112. recipient_list = self.get_recipients()
  113. msg = """<p>Hello,</p>
  114. <p>Your backups are ready to be downloaded.</p>
  115. <p>1. <a href="%(db_backup_url)s">Click here to download\
  116. the database backup</a></p>
  117. <p>2. <a href="%(files_backup_url)s">Click here to download\
  118. the files backup</a></p>
  119. <p>This link will be valid for 24 hours. A new backup will be available
  120. for download only after 24 hours.</p>
  121. <p>Have a nice day!<br>ERPNext</p>""" % {
  122. "db_backup_url": os.path.join(backup_url, os.path.basename(self.backup_path_db)),
  123. "files_backup_url": os.path.join(backup_url, os.path.basename(self.backup_path_files))
  124. }
  125. datetime_str = datetime.fromtimestamp(os.stat(self.backup_path_db).st_ctime)
  126. subject = datetime_str.strftime("%d/%m/%Y %H:%M:%S") + """ - Backup ready to be downloaded"""
  127. sendmail(recipients=recipient_list, msg=msg, subject=subject)
  128. return recipient_list
  129. @webnotes.whitelist()
  130. def get_backup():
  131. """
  132. This function is executed when the user clicks on
  133. Toos > Download Backup
  134. """
  135. #if verbose: print webnotes.conn.cur_db_name + " " + conf.db_password
  136. delete_temp_backups()
  137. odb = BackupGenerator(webnotes.conn.cur_db_name, webnotes.conn.cur_db_name,\
  138. webnotes.get_db_password(webnotes.conn.cur_db_name))
  139. odb.get_backup()
  140. recipient_list = odb.send_email()
  141. webnotes.msgprint("""A download link to your backup will be emailed \
  142. to you shortly on the following email address:
  143. %s""" % (', '.join(recipient_list)))
  144. def scheduled_backup():
  145. """this function is called from scheduler
  146. deletes backups older than 7 days
  147. takes backup"""
  148. delete_temp_backups(older_than=168)
  149. odb = BackupGenerator(webnotes.conn.cur_db_name, webnotes.conn.cur_db_name,\
  150. webnotes.get_db_password(webnotes.conn.cur_db_name))
  151. odb.get_backup()
  152. from webnotes.utils import now
  153. print "backup taken -", odb.backup_path_db, "- on", now()
  154. def delete_temp_backups(older_than=24):
  155. """
  156. Cleans up the backup_link_path directory by deleting files older than 24 hours
  157. """
  158. file_list = os.listdir(get_backup_path())
  159. for this_file in file_list:
  160. this_file_path = os.path.join(get_backup_path(), this_file)
  161. if is_file_old(this_file_path, older_than):
  162. os.remove(this_file_path)
  163. def is_file_old(db_file_name, older_than=24):
  164. """
  165. Checks if file exists and is older than specified hours
  166. Returns ->
  167. True: file does not exist or file is old
  168. False: file is new
  169. """
  170. if os.path.isfile(db_file_name):
  171. from datetime import timedelta
  172. import time
  173. #Get timestamp of the file
  174. file_datetime = datetime.fromtimestamp\
  175. (os.stat(db_file_name).st_ctime)
  176. if datetime.today() - file_datetime >= timedelta(hours = older_than):
  177. if verbose: print "File is old"
  178. return True
  179. else:
  180. if verbose: print "File is recent"
  181. return False
  182. else:
  183. if verbose: print "File does not exist"
  184. return True
  185. backup_path = None
  186. def get_backup_path():
  187. global backup_path
  188. if not backup_path:
  189. import os, conf
  190. backup_path = os.path.join(os.path.dirname(os.path.abspath(conf.__file__)),
  191. 'public', 'backups')
  192. return backup_path
  193. #-------------------------------------------------------------------------------
  194. if __name__ == "__main__":
  195. """
  196. is_file_old db_name user password
  197. get_backup db_name user password
  198. """
  199. import sys
  200. cmd = sys.argv[1]
  201. if cmd == "is_file_old":
  202. odb = BackupGenerator(sys.argv[2], sys.argv[3], sys.argv[4])
  203. is_file_old(odb.db_file_name)
  204. if cmd == "get_backup":
  205. odb = BackupGenerator(sys.argv[2], sys.argv[3], sys.argv[4])
  206. odb.get_backup()
  207. if cmd == "take_dump":
  208. odb = BackupGenerator(sys.argv[2], sys.argv[3], sys.argv[4])
  209. odb.take_dump()
  210. if cmd == "send_email":
  211. odb = BackupGenerator(sys.argv[2], sys.argv[3], sys.argv[4])
  212. odb.send_email("abc.sql.gz")
  213. if cmd == "delete_temp_backups":
  214. delete_temp_backups()