[MIG] auto_backup: Migration to 13.0
parent
230890d127
commit
fedd9384df
|
@ -6,7 +6,7 @@
|
||||||
{
|
{
|
||||||
"name": "Database Auto-Backup",
|
"name": "Database Auto-Backup",
|
||||||
"summary": "Backups database",
|
"summary": "Backups database",
|
||||||
"version": "12.0.1.0.0",
|
"version": "13.0.1.0.0",
|
||||||
"author": "Yenthe Van Ginneken, "
|
"author": "Yenthe Van Ginneken, "
|
||||||
"Agile Business Group, "
|
"Agile Business Group, "
|
||||||
"Grupo ESOC Ingenieria de Servicios, "
|
"Grupo ESOC Ingenieria de Servicios, "
|
||||||
|
@ -16,7 +16,7 @@
|
||||||
"license": "AGPL-3",
|
"license": "AGPL-3",
|
||||||
"website": "https://github.com/OCA/server-tools/",
|
"website": "https://github.com/OCA/server-tools/",
|
||||||
"category": "Tools",
|
"category": "Tools",
|
||||||
"depends": ["mail",],
|
"depends": ["mail"],
|
||||||
"data": [
|
"data": [
|
||||||
"data/ir_cron.xml",
|
"data/ir_cron.xml",
|
||||||
"data/mail_message_subtype.xml",
|
"data/mail_message_subtype.xml",
|
||||||
|
@ -24,5 +24,5 @@
|
||||||
"view/db_backup_view.xml",
|
"view/db_backup_view.xml",
|
||||||
],
|
],
|
||||||
"installable": True,
|
"installable": True,
|
||||||
"external_dependencies": {"python": ["pysftp"],},
|
"external_dependencies": {"python": ["pysftp"]},
|
||||||
}
|
}
|
||||||
|
|
|
@ -98,7 +98,6 @@ class DbBackup(models.Model):
|
||||||
"""Default to ``backups`` folder inside current server datadir."""
|
"""Default to ``backups`` folder inside current server datadir."""
|
||||||
return os.path.join(tools.config["data_dir"], "backups", self.env.cr.dbname)
|
return os.path.join(tools.config["data_dir"], "backups", self.env.cr.dbname)
|
||||||
|
|
||||||
@api.multi
|
|
||||||
@api.depends("folder", "method", "sftp_host", "sftp_port", "sftp_user")
|
@api.depends("folder", "method", "sftp_host", "sftp_port", "sftp_user")
|
||||||
def _compute_name(self):
|
def _compute_name(self):
|
||||||
"""Get the right summary for this job."""
|
"""Get the right summary for this job."""
|
||||||
|
@ -113,7 +112,6 @@ class DbBackup(models.Model):
|
||||||
rec.folder,
|
rec.folder,
|
||||||
)
|
)
|
||||||
|
|
||||||
@api.multi
|
|
||||||
@api.constrains("folder", "method")
|
@api.constrains("folder", "method")
|
||||||
def _check_folder(self):
|
def _check_folder(self):
|
||||||
"""Do not use the filestore or you will backup your backups."""
|
"""Do not use the filestore or you will backup your backups."""
|
||||||
|
@ -128,7 +126,6 @@ class DbBackup(models.Model):
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@api.multi
|
|
||||||
def action_sftp_test_connection(self):
|
def action_sftp_test_connection(self):
|
||||||
"""Check if the SFTP settings are correct."""
|
"""Check if the SFTP settings are correct."""
|
||||||
try:
|
try:
|
||||||
|
@ -143,7 +140,6 @@ class DbBackup(models.Model):
|
||||||
_logger.info("Connection Test Failed!", exc_info=True)
|
_logger.info("Connection Test Failed!", exc_info=True)
|
||||||
raise exceptions.Warning(_("Connection Test Failed!"))
|
raise exceptions.Warning(_("Connection Test Failed!"))
|
||||||
|
|
||||||
@api.multi
|
|
||||||
def action_backup(self):
|
def action_backup(self):
|
||||||
"""Run selected backups."""
|
"""Run selected backups."""
|
||||||
backup = None
|
backup = None
|
||||||
|
@ -206,7 +202,6 @@ class DbBackup(models.Model):
|
||||||
"""Run all scheduled backups."""
|
"""Run all scheduled backups."""
|
||||||
return self.search([]).action_backup()
|
return self.search([]).action_backup()
|
||||||
|
|
||||||
@api.multi
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def backup_log(self):
|
def backup_log(self):
|
||||||
"""Log a backup result."""
|
"""Log a backup result."""
|
||||||
|
@ -225,16 +220,21 @@ class DbBackup(models.Model):
|
||||||
_logger.info("Database backup succeeded: %s", self.name)
|
_logger.info("Database backup succeeded: %s", self.name)
|
||||||
self.message_post(body=_("Database backup succeeded."))
|
self.message_post(body=_("Database backup succeeded."))
|
||||||
|
|
||||||
@api.multi
|
|
||||||
def cleanup(self):
|
def cleanup(self):
|
||||||
"""Clean up old backups."""
|
"""Clean up old backups."""
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
for rec in self.filtered("days_to_keep"):
|
for rec in self.filtered("days_to_keep"):
|
||||||
with rec.cleanup_log():
|
with rec.cleanup_log():
|
||||||
oldest = self.filename(now - timedelta(days=rec.days_to_keep))
|
bu_format = rec.backup_format
|
||||||
|
file_extension = bu_format == "zip" and "dump.zip" or bu_format
|
||||||
|
oldest = self.filename(
|
||||||
|
now - timedelta(days=rec.days_to_keep), bu_format
|
||||||
|
)
|
||||||
|
|
||||||
if rec.method == "local":
|
if rec.method == "local":
|
||||||
for name in iglob(os.path.join(rec.folder, "*.dump.zip")):
|
for name in iglob(
|
||||||
|
os.path.join(rec.folder, "*.%s" % file_extension)
|
||||||
|
):
|
||||||
if os.path.basename(name) < oldest:
|
if os.path.basename(name) < oldest:
|
||||||
os.unlink(name)
|
os.unlink(name)
|
||||||
|
|
||||||
|
@ -242,12 +242,11 @@ class DbBackup(models.Model):
|
||||||
with rec.sftp_connection() as remote:
|
with rec.sftp_connection() as remote:
|
||||||
for name in remote.listdir(rec.folder):
|
for name in remote.listdir(rec.folder):
|
||||||
if (
|
if (
|
||||||
name.endswith(".dump.zip")
|
name.endswith(".%s" % file_extension)
|
||||||
and os.path.basename(name) < oldest
|
and os.path.basename(name) < oldest
|
||||||
):
|
):
|
||||||
remote.unlink("{}/{}".format(rec.folder, name))
|
remote.unlink("{}/{}".format(rec.folder, name))
|
||||||
|
|
||||||
@api.multi
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def cleanup_log(self):
|
def cleanup_log(self):
|
||||||
"""Log a possible cleanup failure."""
|
"""Log a possible cleanup failure."""
|
||||||
|
@ -280,7 +279,6 @@ class DbBackup(models.Model):
|
||||||
when, ext="dump.zip" if ext == "zip" else ext
|
when, ext="dump.zip" if ext == "zip" else ext
|
||||||
)
|
)
|
||||||
|
|
||||||
@api.multi
|
|
||||||
def sftp_connection(self):
|
def sftp_connection(self):
|
||||||
"""Return a new SFTP connection with found parameters."""
|
"""Return a new SFTP connection with found parameters."""
|
||||||
self.ensure_one()
|
self.ensure_one()
|
||||||
|
|
|
@ -202,9 +202,7 @@ class TestDbBackup(common.TransactionCase):
|
||||||
def test_sftp_connection_init_key(self, pysftp):
|
def test_sftp_connection_init_key(self, pysftp):
|
||||||
""" It should initiate SFTP connection w/ proper args and key """
|
""" It should initiate SFTP connection w/ proper args and key """
|
||||||
rec_id = self.new_record()
|
rec_id = self.new_record()
|
||||||
rec_id.write(
|
rec_id.write({"sftp_private_key": "pkey", "sftp_password": "pkeypass"})
|
||||||
{"sftp_private_key": "pkey", "sftp_password": "pkeypass",}
|
|
||||||
)
|
|
||||||
rec_id.sftp_connection()
|
rec_id.sftp_connection()
|
||||||
pysftp.Connection.assert_called_once_with(
|
pysftp.Connection.assert_called_once_with(
|
||||||
host=rec_id.sftp_host,
|
host=rec_id.sftp_host,
|
||||||
|
|
Loading…
Reference in New Issue