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