mirror of
https://github.com/guohuadeng/app-odoo.git
synced 2025-02-23 04:11:36 +02:00
#I9QR3B app_auto_backup增加从界面下载删除备份库的功能 V17.0
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from . import controllers
|
||||
from . import models
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
{
|
||||
'name': "Database auto backup,数据库自动备份",
|
||||
'version': '24.03.27',
|
||||
'version': '24.05.22',
|
||||
|
||||
'summary': 'Automated backups, optimized from auto_backup of Yenthe Van Ginneken',
|
||||
|
||||
@@ -31,7 +31,8 @@
|
||||
|
||||
# any module necessary for this one to work correctly
|
||||
'depends': [
|
||||
'base'
|
||||
'base',
|
||||
'app_odoo_customize'
|
||||
],
|
||||
'external_dependencies': {
|
||||
'python': ['paramiko'],
|
||||
@@ -43,5 +44,6 @@
|
||||
'security/ir.model.access.csv',
|
||||
'views/backup_view.xml',
|
||||
'data/backup_data.xml',
|
||||
'views/db_backup_details.xml',
|
||||
],
|
||||
}
|
||||
|
||||
3
app_auto_backup/controllers/__init__.py
Normal file
3
app_auto_backup/controllers/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from . import main
|
||||
30
app_auto_backup/controllers/main.py
Normal file
30
app_auto_backup/controllers/main.py
Normal file
@@ -0,0 +1,30 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import logging
|
||||
import os
|
||||
|
||||
from odoo import http, _
|
||||
from odoo.http import request, content_disposition
|
||||
from odoo.exceptions import AccessError, UserError
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class AppAutoBackup(http.Controller):
|
||||
|
||||
@http.route("/download/backupfile/<path:file_path>", type="http", auth="user")
|
||||
def download_backupfile(self, file_path, **kw):
|
||||
if os.path.exists(file_path):
|
||||
try:
|
||||
with open(file_path, 'rb') as file:
|
||||
file_content = file.read()
|
||||
file_name = file_path.split("/")[-1]
|
||||
headers = [
|
||||
('Content-Type', 'application/octet-stream'),
|
||||
('Content-Disposition', content_disposition(file_name)),
|
||||
]
|
||||
return request.make_response(file_content, headers)
|
||||
except Exception as e:
|
||||
raise UserError(e)
|
||||
else:
|
||||
return 'File not found'
|
||||
@@ -1,3 +1,4 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from . import db_backup
|
||||
from . import db_backup_details
|
||||
|
||||
@@ -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,11 +137,17 @@ class DbBackup(models.Model):
|
||||
fp = open(file_path, 'wb')
|
||||
self._take_dump(rec.name, fp, 'db.backup', rec.backup_type)
|
||||
fp.close()
|
||||
rec.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(
|
||||
_logger.warning(
|
||||
"Couldn't backup database %s. Bad database administrator password for server running at "
|
||||
"http://%s:%s" % (rec.name, rec.host, rec.port))
|
||||
_logger.debug("Exact error from the exception: %s", str(error))
|
||||
_logger.warning("Exact error from the exception: %s", str(error))
|
||||
continue
|
||||
|
||||
# Check if user wants to write to SFTP or not.
|
||||
@@ -262,6 +269,10 @@ 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)])
|
||||
if backup_details_id:
|
||||
backup_details_id.unlink()
|
||||
else:
|
||||
os.remove(fullpath)
|
||||
|
||||
# This is more or less the same as the default Odoo function at
|
||||
|
||||
37
app_auto_backup/models/db_backup_details.py
Normal file
37
app_auto_backup/models/db_backup_details.py
Normal file
@@ -0,0 +1,37 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
|
||||
from odoo import api, fields, models, _
|
||||
from odoo.exceptions import AccessError, UserError
|
||||
|
||||
|
||||
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()
|
||||
@@ -1,3 +1,4 @@
|
||||
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
|
||||
admin_access, db_backup admin access,model_db_backup,base.group_no_one,1,1,1,1
|
||||
admin_security_rule, Model db_backup admin access,model_db_backup,app_auto_backup.group_manager,1,1,1,1
|
||||
admin_db_backup_details, Model db_backup_details admin access,model_db_backup_details,app_auto_backup.group_manager,1,1,1,1
|
||||
|
||||
|
@@ -8,10 +8,8 @@
|
||||
<form string="Back-up view">
|
||||
<sheet>
|
||||
<div class="oe_button_box" name="button_box">
|
||||
<button name="action_view_cron" string="View Cron"
|
||||
type="object" class="oe_stat_button" icon="fa-clock-o"/>
|
||||
<button name="action_run_cron" string="Run Backup"
|
||||
type="object" class="oe_stat_button" icon="fa-play-circle"/>
|
||||
<button name="action_view_cron" string="View Cron" type="object" class="oe_stat_button" icon="fa-clock-o"/>
|
||||
<button name="action_run_cron" string="Run Backup" type="object" class="oe_stat_button" icon="fa-play-circle"/>
|
||||
</div>
|
||||
<group col="4" colspan="4">
|
||||
<separator col="2" string="Local backup configuration"/>
|
||||
@@ -29,8 +27,7 @@
|
||||
<separator col="2" string="SFTP"/>
|
||||
</group>
|
||||
<div style="width:50%;border-radius:10px;margin: 10px 0px;padding:15px 10px 15px 10px;
|
||||
background-repeat: no-repeat;background-position: 10px center;color: #9F6000;background-color: #FEEFB3;"
|
||||
invisible="sftp_write == False">
|
||||
background-repeat: no-repeat;background-position: 10px center;color: #9F6000;background-color: #FEEFB3;" invisible="sftp_write == False">
|
||||
<b>Warning:</b>
|
||||
Use SFTP with caution! This writes files to external servers under the path you specify.
|
||||
</div>
|
||||
@@ -39,14 +36,11 @@ background-repeat: no-repeat;background-position: 10px center;color: #9F6000;bac
|
||||
<field name="sftp_host" invisible="sftp_write == False" required="sftp_write == True"/>
|
||||
<field name="sftp_port" invisible="sftp_write == False" required="sftp_write == True"/>
|
||||
<field name="sftp_user" invisible="sftp_write == False" required="sftp_write == True"/>
|
||||
<field name="sftp_password" invisible="sftp_write == False" required="sftp_write == True"
|
||||
password="True"/>
|
||||
<field name="sftp_path" invisible="sftp_write == False" required="sftp_write == True"
|
||||
placeholder="For example: /odoo/backups/"/>
|
||||
<field name="sftp_password" invisible="sftp_write == False" required="sftp_write == True" password="True"/>
|
||||
<field name="sftp_path" invisible="sftp_write == False" required="sftp_write == True" placeholder="For example: /odoo/backups/"/>
|
||||
<field name="days_to_keep_sftp" invisible="sftp_write == False" required="sftp_write == True"/>
|
||||
<field name="send_mail_sftp_fail" invisible="sftp_write == False"/>
|
||||
<field name="email_to_notify" invisible="send_mail_sftp_fail == False or sftp_write == False"
|
||||
required="send_mail_sftp_fail == True"/>
|
||||
<field name="email_to_notify" invisible="send_mail_sftp_fail == False or sftp_write == False" required="send_mail_sftp_fail == True"/>
|
||||
<button name="test_sftp_connection" type="object" invisible="sftp_write == False" string="Test SFTP Connection"/>
|
||||
</group>
|
||||
<separator string="Help" colspan="2"/>
|
||||
@@ -71,6 +65,19 @@ background-repeat: no-repeat;background-position: 10px center;color: #9F6000;bac
|
||||
<a href="https://www.odooai.cn">Contact odooai.cn!</a>
|
||||
</p>
|
||||
</div>
|
||||
<notebook>
|
||||
<page name="backup_details" string="Backup records">
|
||||
<field name="backup_details_ids" readonly="1">
|
||||
<tree>
|
||||
<field name="name"/>
|
||||
<field name="file_path"/>
|
||||
<field name="url" invisible="0"/>
|
||||
<button name="action_download_file" type="object" title="Download File" class="fa fa-download"/>
|
||||
<button name="action_remove_file" type="object" title="Remove File" class="fa fa-trash"/>
|
||||
</tree>
|
||||
</field>
|
||||
</page>
|
||||
</notebook>
|
||||
</sheet>
|
||||
</form>
|
||||
</field>
|
||||
@@ -99,8 +106,7 @@ background-repeat: no-repeat;background-position: 10px center;color: #9F6000;bac
|
||||
<field name="view_id" ref="view_backup_config_tree"/>
|
||||
</record>
|
||||
|
||||
<menuitem id="auto_backup_menu" name="Back-ups" sequence="9"
|
||||
parent="app_odoo_customize.menu_app_group"/>
|
||||
<menuitem parent="auto_backup_menu" action="action_backup" id="backup_conf_menu"/>
|
||||
<menuitem id="auto_backup_menu" name="Back-ups" sequence="9" parent="app_odoo_customize.menu_app_group"/>
|
||||
<menuitem parent="auto_backup_menu" action="action_backup" id="backup_conf_menu" sequence="1"/>
|
||||
</data>
|
||||
</odoo>
|
||||
|
||||
24
app_auto_backup/views/db_backup_details.xml
Normal file
24
app_auto_backup/views/db_backup_details.xml
Normal file
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<odoo>
|
||||
<record id="db_backup_details_tree_view" model="ir.ui.view">
|
||||
<field name="name">db.backup.details.tree</field>
|
||||
<field name="model">db.backup.details</field>
|
||||
<field name="arch" type="xml">
|
||||
<tree>
|
||||
<field name="name"/>
|
||||
<field name="file_path"/>
|
||||
<field name="url" invisible="1"/>
|
||||
<button name="action_download_file" type="object" title="Download File" class="fa fa-download"/>
|
||||
<button name="action_remove_file" type="object" title="Remove File" class="fa fa-trash"/>
|
||||
</tree>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record id="action_db_backup_details" model="ir.actions.act_window">
|
||||
<field name="name">Database backups</field>
|
||||
<field name="res_model">db.backup.details</field>
|
||||
<field name="view_mode">tree</field>
|
||||
</record>
|
||||
|
||||
<menuitem id="menu_action_db_backup_details" action="action_db_backup_details" parent="auto_backup_menu" sequence="3"/>
|
||||
</odoo>
|
||||
Reference in New Issue
Block a user