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.

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