Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

248 rindas
8.7 KiB

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