Skip to content

Commit

Permalink
[IMP] auto_backup: pre-commit auto fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
michelerusti committed Feb 10, 2025
1 parent 98edb0f commit fc81f02
Show file tree
Hide file tree
Showing 18 changed files with 175 additions and 200 deletions.
82 changes: 42 additions & 40 deletions auto_backup/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ Database Auto-Backup
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github
:target: https://github.com/OCA/server-tools/tree/16.0/auto_backup
:target: https://github.com/OCA/server-tools/tree/18.0/auto_backup
:alt: OCA/server-tools
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/server-tools-16-0/server-tools-16-0-auto_backup
:target: https://translation.odoo-community.org/projects/server-tools-18-0/server-tools-18-0-auto_backup
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/server-tools&target_branch=16.0
:target: https://runboat.odoo-community.org/builds?repo=OCA/server-tools&target_branch=18.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|
Expand All @@ -38,16 +38,17 @@ A tool for all your back-ups, internal and external!
Installation
============

Before installing this module, you need to execute::
Before installing this module, you need to execute:

pip3 install pysftp==0.2.9
::

pip3 install pysftp==0.2.9

Configuration
=============

Go to *Settings -> Database Structure -> Automated Backup* to
create your configurations for each database that you needed
to backups.
Go to *Settings -> Database Structure -> Automated Backup* to create
your configurations for each database that you needed to backups.

Usage
=====
Expand All @@ -58,10 +59,10 @@ through an encrypted tunnel. You can even specify how long local backups
and external backups should be kept, automatically!

Connect with an FTP Server
~~~~~~~~~~~~~~~~~~~~~~~~~~
--------------------------

Keep your data safe, through an SSH tunnel!
-------------------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Want to go even further and write your backups to an external server?
You can with this module! Specify the credentials to the server, specify
Expand All @@ -70,60 +71,61 @@ through an SSH (encrypted) tunnel, thanks to pysftp, so your data is
safe!

Test connection
~~~~~~~~~~~~~~~
---------------

Checks your credentials in one click
------------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Want to make sure if the connection details are correct and if Odoo can
automatically write them to the remote server? Simply click on the ‘Test
SFTP Connection’ button and you will get message telling you if
everything is OK, or what is wrong!

E-mail on backup failure
~~~~~~~~~~~~~~~~~~~~~~~~
------------------------

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.
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
|Try me on Runbot|

.. |Try me on Runbot| image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:target: https://runbot.odoo-community.org/runbot/149/11.0

Known issues / Roadmap
======================

* On larger databases, it is possible that backups will die due to Odoo server
settings. In order to circumvent this without frivolously changing settings,
you need to run the backup from outside of the main Odoo instance. How to do
this is outlined in `this blog post
<https://blog.laslabs.com/2016/10/running-python-scripts-within-odoos-environment/>`_.
* Backups won't work if list_db=False is configured in the instance.
- On larger databases, it is possible that backups will die due to Odoo
server settings. In order to circumvent this without frivolously
changing settings, you need to run the backup from outside of the
main Odoo instance. How to do this is outlined in `this blog
post <https://blog.laslabs.com/2016/10/running-python-scripts-within-odoos-environment/>`__.
- Backups won't work if list_db=False is configured in the instance.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20auto_backup%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20auto_backup%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~
-------

* Yenthe Van Ginneken
* Agile Business Group
Expand All @@ -132,20 +134,20 @@ Authors
* AdaptiveCity

Contributors
~~~~~~~~~~~~

* Yenthe Van Ginneken <[email protected]>
* Alessio Gerace <[email protected]>
* Jairo Llopis <[email protected]>
* Dave Lasley <[email protected]>
* Andrea Stirpe <[email protected]>
* Aitor Bouzas <[email protected]>
* Simone Vanin <[email protected]>
* Vu Nguyen Anh <[email protected]>
* Alex Comba <[email protected]>
------------

- Yenthe Van Ginneken <[email protected]>
- Alessio Gerace <[email protected]>
- Jairo Llopis <[email protected]>
- Dave Lasley <[email protected]>
- Andrea Stirpe <[email protected]>
- Aitor Bouzas <[email protected]>
- Simone Vanin <[email protected]>
- Vu Nguyen Anh <[email protected]>
- Alex Comba <[email protected]>

Maintainers
~~~~~~~~~~~
-----------

This module is maintained by the OCA.

Expand All @@ -157,6 +159,6 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/16.0/auto_backup>`_ project on GitHub.
This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/18.0/auto_backup>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
49 changes: 19 additions & 30 deletions auto_backup/models/db_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,9 @@ def _compute_name(self):
"""Get the right summary for this job."""
for rec in self:
if rec.method == "local":
rec.name = "%s @ localhost" % rec.folder
rec.name = f"{rec.folder} @ localhost"
elif rec.method == "sftp":
rec.name = "sftp://%s@%s:%d%s" % (
rec.sftp_user,
rec.sftp_host,
rec.sftp_port,
rec.folder,
)
rec.name = f"sftp://{rec.sftp_user}@{rec.sftp_host}:{rec.sftp_port}{rec.folder}"

@api.constrains("folder", "method")
def _check_folder(self):
Expand Down Expand Up @@ -156,7 +151,7 @@ def action_backup(self):
try:
os.makedirs(rec.folder, exist_ok=True)
except OSError as exc:
_logger.exception("Action backup - OSError: %s" % exc)
_logger.exception(f"Action backup - OSError: {exc}")

with open(os.path.join(rec.folder, filename), "wb") as destiny:
# Copy the cached backup
Expand All @@ -177,7 +172,6 @@ def action_backup(self):
for rec in sftp:
filename = self.filename(datetime.now(), ext=rec.backup_format)
with rec.backup_log():

cached = db.dump_db(
self.env.cr.dbname, None, backup_format=rec.backup_format
)
Expand All @@ -188,9 +182,7 @@ def action_backup(self):
try:
remote.makedirs(rec.folder, exist_ok=True)
except pysftp.ConnectionException as exc:
_logger.exception(
"pysftp ConnectionException: %s" % exc
)
_logger.exception(f"pysftp ConnectionException: {exc}")

# Copy cached backup to remote server
with remote.open(
Expand All @@ -211,18 +203,17 @@ def action_backup_all(self):
def backup_log(self):
"""Log a backup result."""
try:
_logger.info("Starting database backup: %s", self.name)
_logger.info(f"Starting database backup: {self.name}")
yield
except Exception:
_logger.exception("Database backup failed: %s", self.name)
_logger.exception(f"Database backup failed: {self.name}")
escaped_tb = tools.html_escape(traceback.format_exc())
self.message_post( # pylint: disable=translation-required
body="<p>%s</p><pre>%s</pre>"
% (_("Database backup failed."), escaped_tb),
body=f"<p>{_('Database backup failed.')}</p><pre>{escaped_tb}</pre>",
subtype_id=self.env.ref("auto_backup.mail_message_subtype_failure").id,
)
else:
_logger.info("Database backup succeeded: %s", self.name)
_logger.info(f"Database backup succeeded: {self.name}")
self.message_post(body=_("Database backup succeeded."))

def cleanup(self):
Expand All @@ -231,46 +222,44 @@ def cleanup(self):
for rec in self.filtered("days_to_keep"):
with rec.cleanup_log():
bu_format = rec.backup_format
file_extension = bu_format == "zip" and "dump.zip" or bu_format
file_extension = "dump.zip" if bu_format == "zip" else 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, "*.%s" % file_extension)
):
for name in iglob(os.path.join(rec.folder, f"*.{file_extension}")):
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(".%s" % file_extension)
name.endswith(f".{file_extension}")
and os.path.basename(name) < oldest
):
remote.unlink("{}/{}".format(rec.folder, name))
remote.unlink(f"{rec.folder}/{name}")

@contextmanager
def cleanup_log(self):
"""Log a possible cleanup failure."""
self.ensure_one()
try:
_logger.info(
"Starting cleanup process after database backup: %s", self.name
)
_logger.info(f"Starting cleanup process after database backup: {self.name}")
yield
except Exception:
_logger.exception("Cleanup of old database backups failed: %s")
_logger.exception(f"Cleanup of old database backups failed: {self.name}")
escaped_tb = tools.html_escape(traceback.format_exc())
self.message_post( # pylint: disable=translation-required
body="<p>%s</p><pre>%s</pre>"
% (_("Cleanup of old database backups failed."), escaped_tb),
body=(
f"<p>{_('Cleanup of old database backups failed.')}</p>"
f"<pre>{escaped_tb}</pre>"
),
subtype_id=self.env.ref("auto_backup.failure").id,
)
else:
_logger.info("Cleanup of old database backups succeeded: %s", self.name)
_logger.info(f"Cleanup of old database backups succeeded: {self.name}")

@staticmethod
def filename(when, ext="zip"):
Expand Down
3 changes: 3 additions & 0 deletions auto_backup/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
2 changes: 2 additions & 0 deletions auto_backup/readme/CONFIGURE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Go to *Settings -\> Database Structure -\> Automated Backup* to create
your configurations for each database that you needed to backups.
3 changes: 0 additions & 3 deletions auto_backup/readme/CONFIGURE.rst

This file was deleted.

9 changes: 9 additions & 0 deletions auto_backup/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- Yenthe Van Ginneken \<<[email protected]>\>
- Alessio Gerace \<<[email protected]>\>
- Jairo Llopis \<<[email protected]>\>
- Dave Lasley \<<[email protected]>\>
- Andrea Stirpe \<<[email protected]>\>
- Aitor Bouzas \<<[email protected]>\>
- Simone Vanin \<<[email protected]>\>
- Vu Nguyen Anh \<<[email protected]>\>
- Alex Comba \<<[email protected]>\>
9 changes: 0 additions & 9 deletions auto_backup/readme/CONTRIBUTORS.rst

This file was deleted.

File renamed without changes.
3 changes: 3 additions & 0 deletions auto_backup/readme/INSTALL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Before installing this module, you need to execute:

pip3 install pysftp==0.2.9
3 changes: 0 additions & 3 deletions auto_backup/readme/INSTALL.rst

This file was deleted.

6 changes: 6 additions & 0 deletions auto_backup/readme/ROADMAP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- On larger databases, it is possible that backups will die due to Odoo
server settings. In order to circumvent this without frivolously
changing settings, you need to run the backup from outside of the main
Odoo instance. How to do this is outlined in [this blog
post](https://blog.laslabs.com/2016/10/running-python-scripts-within-odoos-environment/).
- Backups won't work if list_db=False is configured in the instance.
6 changes: 0 additions & 6 deletions auto_backup/readme/ROADMAP.rst

This file was deleted.

37 changes: 37 additions & 0 deletions auto_backup/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Keep your Odoo data safe with this module. Take automated back-ups,
remove them automatically and even write them to an external server
through an encrypted tunnel. You can even specify how long local backups
and external backups should be kept, automatically!

## Connect with an FTP Server

### Keep your data safe, through an SSH tunnel!

Want to go even further and write your backups to an external server?
You can with this module! Specify the credentials to the server, specify
a path and everything will be backed up automatically. This is done
through an SSH (encrypted) tunnel, thanks to pysftp, so your data is
safe!

## Test connection

### Checks your credentials in one click

Want to make sure if the connection details are correct and if Odoo can
automatically write them to the remote server? Simply click on the ‘Test
SFTP Connection’ button and you will get message telling you if
everything is OK, or what is wrong!

## E-mail on backup failure

### 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.

[![Try me on Runbot](https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas)](https://runbot.odoo-community.org/runbot/149/11.0)
Loading

0 comments on commit fc81f02

Please sign in to comment.