You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
10 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2004-2009 Tiny SPRL (<http://tiny.be>).
  3. # © 2015 Agile Business Group <http://www.agilebg.com>
  4. # © 2016 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis
  5. # License AGPL-3.0 or later (http://www.gnu.org/licenses/gpl.html).
  6. import os
  7. import shutil
  8. import traceback
  9. from contextlib import contextmanager
  10. from datetime import datetime, timedelta
  11. from glob import iglob
  12. from odoo import exceptions, models, fields, api, _, tools
  13. from odoo.service import db
  14. import logging
  15. _logger = logging.getLogger(__name__)
  16. try:
  17. import pysftp
  18. except ImportError: # pragma: no cover
  19. _logger.debug('Cannot import pysftp')
  20. class DbBackup(models.Model):
  21. _name = 'db.backup'
  22. _inherit = "mail.thread"
  23. _sql_constraints = [
  24. ("name_unique", "UNIQUE(name)", "Cannot duplicate a configuration."),
  25. ("days_to_keep_positive", "CHECK(days_to_keep >= 0)",
  26. "I cannot remove backups from the future. Ask Doc for that."),
  27. ]
  28. name = fields.Char(
  29. string="Name",
  30. compute="_compute_name",
  31. store=True,
  32. help="Summary of this backup process",
  33. )
  34. folder = fields.Char(
  35. default=lambda self: self._default_folder(),
  36. oldname="bkp_dir",
  37. help='Absolute path for storing the backups',
  38. required=True
  39. )
  40. days_to_keep = fields.Integer(
  41. oldname="daystokeep",
  42. required=True,
  43. default=0,
  44. help="Backups older than this will be deleted automatically. "
  45. "Set 0 to disable autodeletion.",
  46. )
  47. method = fields.Selection(
  48. selection=[("local", "Local disk"), ("sftp", "Remote SFTP server")],
  49. default="local",
  50. help="Choose the storage method for this backup.",
  51. )
  52. sftp_host = fields.Char(
  53. string='SFTP Server',
  54. oldname="sftpip",
  55. help=(
  56. "The host name or IP address from your remote"
  57. " server. For example 192.168.0.1"
  58. )
  59. )
  60. sftp_port = fields.Integer(
  61. string="SFTP Port",
  62. default=22,
  63. oldname="sftpport",
  64. help="The port on the FTP server that accepts SSH/SFTP calls."
  65. )
  66. sftp_user = fields.Char(
  67. string='Username in the SFTP Server',
  68. oldname="sftpusername",
  69. help=(
  70. "The username where the SFTP connection "
  71. "should be made with. This is the user on the external server."
  72. )
  73. )
  74. sftp_password = fields.Char(
  75. string="SFTP Password",
  76. oldname="sftppassword",
  77. help="The password for the SFTP connection. If you specify a private "
  78. "key file, then this is the password to decrypt it.",
  79. )
  80. sftp_private_key = fields.Char(
  81. string="Private key location",
  82. help="Path to the private key file. Only the Odoo user should have "
  83. "read permissions for that file.",
  84. )
  85. @api.model
  86. def _default_folder(self):
  87. """Default to ``backups`` folder inside current server datadir."""
  88. return os.path.join(
  89. tools.config["data_dir"],
  90. "backups",
  91. self.env.cr.dbname)
  92. @api.multi
  93. @api.depends("folder", "method", "sftp_host", "sftp_port", "sftp_user")
  94. def _compute_name(self):
  95. """Get the right summary for this job."""
  96. for rec in self:
  97. if rec.method == "local":
  98. rec.name = "%s @ localhost" % rec.folder
  99. elif rec.method == "sftp":
  100. rec.name = "sftp://%s@%s:%d%s" % (
  101. rec.sftp_user, rec.sftp_host, rec.sftp_port, rec.folder)
  102. @api.multi
  103. @api.constrains("folder", "method")
  104. def _check_folder(self):
  105. """Do not use the filestore or you will backup your backups."""
  106. for s in self:
  107. if (s.method == "local" and
  108. s.folder.startswith(
  109. tools.config.filestore(self.env.cr.dbname))):
  110. raise exceptions.ValidationError(
  111. _("Do not save backups on your filestore, or you will "
  112. "backup your backups too!"))
  113. @api.multi
  114. def action_sftp_test_connection(self):
  115. """Check if the SFTP settings are correct."""
  116. try:
  117. # Just open and close the connection
  118. with self.sftp_connection():
  119. raise exceptions.Warning(_("Connection Test Succeeded!"))
  120. except (pysftp.CredentialException,
  121. pysftp.ConnectionException,
  122. pysftp.SSHException):
  123. _logger.info("Connection Test Failed!", exc_info=True)
  124. raise exceptions.Warning(_("Connection Test Failed!"))
  125. @api.multi
  126. def action_backup(self):
  127. """Run selected backups."""
  128. backup = None
  129. filename = self.filename(datetime.now())
  130. successful = self.browse()
  131. # Start with local storage
  132. for rec in self.filtered(lambda r: r.method == "local"):
  133. with rec.backup_log():
  134. # Directory must exist
  135. try:
  136. os.makedirs(rec.folder)
  137. except OSError:
  138. pass
  139. with open(os.path.join(rec.folder, filename),
  140. 'wb') as destiny:
  141. # Copy the cached backup
  142. if backup:
  143. with open(backup) as cached:
  144. shutil.copyfileobj(cached, destiny)
  145. # Generate new backup
  146. else:
  147. db.dump_db(self.env.cr.dbname, destiny)
  148. backup = backup or destiny.name
  149. successful |= rec
  150. # Ensure a local backup exists if we are going to write it remotely
  151. sftp = self.filtered(lambda r: r.method == "sftp")
  152. if sftp:
  153. if backup:
  154. cached = open(backup)
  155. else:
  156. cached = db.dump_db(self.env.cr.dbname, None)
  157. with cached:
  158. for rec in sftp:
  159. with rec.backup_log():
  160. with rec.sftp_connection() as remote:
  161. # Directory must exist
  162. try:
  163. remote.makedirs(rec.folder)
  164. except pysftp.ConnectionException:
  165. pass
  166. # Copy cached backup to remote server
  167. with remote.open(
  168. os.path.join(rec.folder, filename),
  169. "wb") as destiny:
  170. shutil.copyfileobj(cached, destiny)
  171. successful |= rec
  172. # Remove old files for successful backups
  173. successful.cleanup()
  174. @api.model
  175. def action_backup_all(self):
  176. """Run all scheduled backups."""
  177. return self.search([]).action_backup()
  178. @api.multi
  179. @contextmanager
  180. def backup_log(self):
  181. """Log a backup result."""
  182. try:
  183. _logger.info("Starting database backup: %s", self.name)
  184. yield
  185. except:
  186. _logger.exception("Database backup failed: %s", self.name)
  187. escaped_tb = tools.html_escape(traceback.format_exc())
  188. self.message_post(
  189. "<p>%s</p><pre>%s</pre>" % (
  190. _("Database backup failed."),
  191. escaped_tb),
  192. subtype_id=self.env.ref(
  193. "auto_backup.mail_message_subtype_failure"
  194. ).id,
  195. )
  196. else:
  197. _logger.info("Database backup succeeded: %s", self.name)
  198. self.message_post(
  199. _("Database backup succeeded."),
  200. subtype_id=self.env.ref(
  201. "auto_backup.mail_message_subtype_success"
  202. ).id)
  203. @api.multi
  204. def cleanup(self):
  205. """Clean up old backups."""
  206. now = datetime.now()
  207. for rec in self.filtered("days_to_keep"):
  208. with rec.cleanup_log():
  209. oldest = self.filename(now - timedelta(days=rec.days_to_keep))
  210. if rec.method == "local":
  211. for name in iglob(os.path.join(rec.folder,
  212. "*.dump.zip")):
  213. if os.path.basename(name) < oldest:
  214. os.unlink(name)
  215. elif rec.method == "sftp":
  216. with rec.sftp_connection() as remote:
  217. for name in remote.listdir(rec.folder):
  218. if (name.endswith(".dump.zip") and
  219. os.path.basename(name) < oldest):
  220. remote.unlink('%s/%s' % (rec.folder, name))
  221. @api.multi
  222. @contextmanager
  223. def cleanup_log(self):
  224. """Log a possible cleanup failure."""
  225. self.ensure_one()
  226. try:
  227. _logger.info("Starting cleanup process after database backup: %s",
  228. self.name)
  229. yield
  230. except:
  231. _logger.exception("Cleanup of old database backups failed: %s")
  232. escaped_tb = tools.html_escape(traceback.format_exc())
  233. self.message_post(
  234. "<p>%s</p><pre>%s</pre>" % (
  235. _("Cleanup of old database backups failed."),
  236. escaped_tb),
  237. subtype_id=self.env.ref(
  238. "auto_backup.mail_message_subtype_failure"
  239. ).id)
  240. else:
  241. _logger.info("Cleanup of old database backups succeeded: %s",
  242. self.name)
  243. @api.model
  244. def filename(self, when):
  245. """Generate a file name for a backup.
  246. :param datetime.datetime when:
  247. Use this datetime instead of :meth:`datetime.datetime.now`.
  248. """
  249. return "{:%Y_%m_%d_%H_%M_%S}.dump.zip".format(when)
  250. @api.multi
  251. def sftp_connection(self):
  252. """Return a new SFTP connection with found parameters."""
  253. self.ensure_one()
  254. params = {
  255. "host": self.sftp_host,
  256. "username": self.sftp_user,
  257. "port": self.sftp_port,
  258. }
  259. _logger.debug(
  260. "Trying to connect to sftp://%(username)s@%(host)s:%(port)d",
  261. extra=params)
  262. if self.sftp_private_key:
  263. params["private_key"] = self.sftp_private_key
  264. if self.sftp_password:
  265. params["private_key_pass"] = self.sftp_password
  266. else:
  267. params["password"] = self.sftp_password
  268. return pysftp.Connection(**params)