Browse Source

Merge pull request #3 from grupoesoc/PR-203-autobackup

[auto_backup] Fixes
pull/203/head
Alessio Gerace 9 years ago
parent
commit
4347758ed3
  1. 6
      auto_backup/README.rst
  2. 26
      auto_backup/__init__.py
  3. 24
      auto_backup/__openerp__.py
  4. 23
      auto_backup/models/__init__.py
  5. 49
      auto_backup/models/db_backup.py
  6. 23
      auto_backup/tests/__init__.py
  7. 23
      auto_backup/tests/test_auto_backup.py
  8. 60
      auto_backup/view/db_backup_view.xml
  9. 1
      requirements.txt

6
auto_backup/README.rst

@ -62,6 +62,12 @@ Stay informed of problems, automatically!
Do you want to know if the database backup succeeded or failed? Subscribe to
the corresponding backup setting notification type.
Run backups when you want
-------------------------
From the backups configuration list, press *More > Execute backup(s)* to
manually execute the selected processes.
.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/149/8.0

26
auto_backup/__init__.py

@ -1,23 +1,7 @@
# -*- encoding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
# $Id$
# Copyright (C) 2015 Agile Business Group <http://www.agilebg.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# -*- coding: utf-8 -*-
# © 2004-2009 Tiny SPRL (<http://tiny.be>).
# © 2015 Agile Business Group <http://www.agilebg.com>
# © 2016 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis
# License GPL-3.0 or later (http://www.gnu.org/licenses/gpl.html).
from . import models

24
auto_backup/__openerp__.py

@ -1,24 +1,8 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
# $Id$
# Copyright (C) 2015 Agile Business Group <http://www.agilebg.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# © 2004-2009 Tiny SPRL (<http://tiny.be>).
# © 2015 Agile Business Group <http://www.agilebg.com>
# © 2016 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis
# License GPL-3.0 or later (http://www.gnu.org/licenses/gpl.html).
{
"name": "Database Auto-Backup",

23
auto_backup/models/__init__.py

@ -1,23 +1,6 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
# $Id$
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# © 2004-2009 Tiny SPRL (<http://tiny.be>).
# © 2016 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis
# License GPL-3.0 or later (http://www.gnu.org/licenses/gpl.html).
from . import db_backup

49
auto_backup/models/db_backup.py

@ -1,23 +1,9 @@
# -*- coding: utf-8 -*-
##############################################################################
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
# Copyright 2015 Agile Business Group <http://www.agilebg.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# © 2004-2009 Tiny SPRL (<http://tiny.be>).
# © 2015 Agile Business Group <http://www.agilebg.com>
# © 2016 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis
# License GPL-3.0 or later (http://www.gnu.org/licenses/gpl.html).
import os
import shutil
import tempfile
@ -40,6 +26,7 @@ class DbBackup(models.Model):
_inherit = "mail.thread"
_sql_constraints = [
("name_unique", "UNIQUE(name)", "Cannot duplicate a configuration."),
("days_to_keep_positive", "CHECK(days_to_keep >= 0)",
"I cannot remove backups from the future. Ask Doc for that."),
]
@ -104,10 +91,11 @@ class DbBackup(models.Model):
@api.model
def _default_folder(self):
"""Default to ``backups`` folder inside current database datadir."""
"""Default to ``backups`` folder inside current server datadir."""
return os.path.join(
tools.config.filestore(self.env.cr.dbname),
"backups")
tools.config["data_dir"],
"backups",
self.env.cr.dbname)
@api.multi
@api.depends("folder", "method", "sftp_host", "sftp_port", "sftp_user")
@ -120,6 +108,18 @@ class DbBackup(models.Model):
rec.name = "sftp://%s@%s:%d%s" % (
rec.sftp_user, rec.sftp_host, rec.sftp_port, rec.folder)
@api.constrains("folder", "method")
@api.multi
def _check_folder(self):
"""Do not use the filestore or you will backup your backups."""
for s in self:
if (s.method == "local" and
s.folder.startswith(
tools.config.filestore(self.env.cr.dbname))):
raise exceptions.ValidationError(
_("Do not save backups on your filestore, or you will "
"backup your backups too!"))
@api.multi
def action_sftp_test_connection(self):
"""Check if the SFTP settings are correct."""
@ -223,13 +223,14 @@ class DbBackup(models.Model):
if rec.method == "local":
for name in iglob(os.path.join(rec.folder,
"*.dump.zip")):
if name < oldest:
if os.path.basename(name) < oldest:
os.unlink(name)
elif rec.method == "sftp":
with rec.sftp_connection() as remote:
for name in remote.listdir(rec.folder):
if name.endswith(".dump.zip") and name < oldest:
if (name.endswith(".dump.zip") and
os.path.basename(name) < oldest):
remote.unlink(name)
@api.multi

23
auto_backup/tests/__init__.py

@ -1,22 +1,7 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright 2015 Agile Business Group <http://www.agilebg.com>
# Copyright (C) 2015 Alessio Gerace <alesiso.gerace@agilebg.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# © 2015 Agile Business Group <http://www.agilebg.com>
# © 2015 Alessio Gerace <alesiso.gerace@agilebg.com>
# © 2016 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import test_auto_backup

23
auto_backup/tests/test_auto_backup.py

@ -1,23 +1,8 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright 2015 Agile Business Group <http://www.agilebg.com>
# Copyright (C) 2015 Alessio Gerace <alesiso.gerace@agilebg.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# © 2015 Agile Business Group <http://www.agilebg.com>
# © 2015 Alessio Gerace <alesiso.gerace@agilebg.com>
# © 2016 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import os
from datetime import datetime

60
auto_backup/view/db_backup_view.xml

@ -10,39 +10,33 @@
<form>
<h1><field name="name"/></h1>
<group string="Local backup configuration">
<group string="Basic backup configuration">
<field name="folder"/>
<field name="days_to_keep"/>
<field name="method"/>
</group>
<group
string="SFTP Settings"
attrs="{
'invisible': [
('method', '!=', 'sftp'),
],
}">
<div attrs="{'invisible': [('method', '!=', 'sftp')]}">
<div class="bg-warning text-warning">
<h3>Warning:</h3>
Use SFTP with caution! This writes files to external servers under the path you specify.
</div>
<field name="sftp_host" placeholder="sftp.example.com"/>
<field name="sftp_port"/>
<field name="sftp_user" placeholder="john"/>
<field name="sftp_password"/>
<field
name="sftp_private_key"
placeholder="/home/odoo/.ssh/id_rsa"/>
<button
name="action_sftp_test_connection"
type="object"
string="Test SFTP Connection"
icon="gtk-network"/>
</group>
<group string="SFTP Settings">
<field name="sftp_host" placeholder="sftp.example.com"/>
<field name="sftp_port"/>
<field name="sftp_user" placeholder="john"/>
<field name="sftp_password"/>
<field
name="sftp_private_key"
placeholder="/home/odoo/.ssh/id_rsa"/>
<button
name="action_sftp_test_connection"
type="object"
string="Test SFTP Connection"
icon="gtk-network"/>
</group>
</div>
<separator string="Help" colspan="2"/>
<div>
This configures the scheduler for automatic backup of the given database running on given host at given port on regular intervals.
<br/>
Automatic backups of the database can be scheduled as follows:
<ol>
<li>Go to Settings / Technical / Automation / Scheduled Actions.</li>
@ -90,5 +84,25 @@
action="action_backup_conf_form"
id="backup_conf_menu"/>
<!-- Execute backup from "More" menu -->
<record id="action_server_backup" model="ir.actions.server">
<field name="name">Execute backup(s)</field>
<field name="model_id" ref="model_db_backup"/>
<field name="code">
object.action_backup()
</field>
</record>
<record model="ir.values" id="action_backup">
<field name="name">Execute backup(s)</field>
<field name="action_id" ref="action_server_backup" />
<field
name="value"
eval="'ir.actions.server,%d' % ref('action_server_backup')" />
<field name="key">action</field>
<field name="model_id" ref="model_db_backup" />
<field name="model">db.backup</field>
<field name="key2">client_action_multi</field>
</record>
</data>
</openerp>

1
requirements.txt

@ -1,3 +1,4 @@
python-ldap
unidecode
validate_email
pysftp
Loading…
Cancel
Save