#I9QR3B app_auto_backup增加从界面下载删除备份库的功能

This commit is contained in:
Chill
2024-05-22 15:53:27 +08:00
parent f48575cdf8
commit 9927cf1804
10 changed files with 170 additions and 65 deletions

View File

@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from . import db_backup
from . import db_backup_details

View File

@@ -73,6 +73,7 @@ class DbBackup(models.Model):
email_to_notify = fields.Char('E-mail to notify',
help='Fill in the e-mail where you want to be notified that the backup failed on '
'the FTP.')
backup_details_ids = fields.One2many('db.backup.details', 'db_backup_id', 'Backup Details')
def test_sftp_connection(self, context=None):
self.ensure_one()
@@ -136,6 +137,12 @@ class DbBackup(models.Model):
fp = open(file_path, 'wb')
self._take_dump(rec.name, fp, 'db.backup', rec.backup_type)
fp.close()
self.backup_details_ids.create({
'name': bkp_file,
'file_path': file_path,
'url': '/download/backupfile/%s' % file_path,
'db_backup_id': rec.id,
})
except Exception as error:
_logger.debug(
"Couldn't backup database %s. Bad database administrator password for server running at "
@@ -262,6 +269,8 @@ class DbBackup(models.Model):
# Only delete files (which are .dump and .zip), no directories.
if os.path.isfile(fullpath) and (".dump" in f or '.zip' in f):
_logger.info("Delete local out-of-date file: %s", fullpath)
backup_details_id = self.env['db.backup.details'].search([('file_path', '=', fullpath)])
backup_details_id.unlink()
os.remove(fullpath)
# This is more or less the same as the default Odoo function at

View File

@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
import os
from odoo import api, fields, models
class DbBackupDetails(models.Model):
_name = 'db.backup.details'
_description = 'Database Backup Details'
name = fields.Char(string='Name')
file_path = fields.Char(string="File Path")
url = fields.Char(string='URL')
db_backup_id = fields.Many2one('db.backup', 'Database Backup')
def action_download_file(self):
self.ensure_one()
if not self.file_path or not self.url:
raise UserError(_("File Path or URL not found."))
else:
return {
'type': 'ir.actions.act_url',
'url': self.url,
'target': 'new',
}
def unlink(self):
if self.file_path:
if os.path.exists(self.file_path):
os.remove(self.file_path)
return super(DbBackupDetails, self).unlink()
def action_remove_file(self):
self.ensure_one()
self.unlink()