Browse Source

Mar 15 [UPDT] : Updated 'auto_database_backup'

pull/254/merge
AjmalCybro 1 year ago
parent
commit
d543fb3934
  1. 2
      auto_database_backup/__manifest__.py
  2. 5
      auto_database_backup/doc/RELEASE_NOTES.md
  3. 83
      auto_database_backup/models/db_backup_configure.py
  4. 21
      auto_database_backup/views/db_backup_configure_views.xml

2
auto_database_backup/__manifest__.py

@ -22,7 +22,7 @@
{
'name': "Automatic Database Backup To Local Server, Remote Server, Google "
"Drive, Dropbox, Onedrive, Nextcloud and Amazon S3",
'version': '15.0.4.1.2',
'version': '15.0.5.1.0',
'category': 'Discuss,Extra Tools',
'summary': """Generate automatic backup of databases and store to local,
google drive, dropbox, nextcloud, amazon S3, onedrive or

5
auto_database_backup/doc/RELEASE_NOTES.md

@ -24,3 +24,8 @@
#### Version 15.0.4.1.2
#### ADD
- Nextcloud and Amazon S3 integration added. Backup can be stored into Nextcloud and Amazon S3.
#### 16.02.2024
#### Version 15.0.5.1.0
#### UPDT
- Fixed internal server error in Onedrive and Google Drive integration.

83
auto_database_backup/models/db_backup_configure.py

@ -260,6 +260,30 @@ class DbBackupConfigure(models.Model):
nextcloud_folder_key = fields.Char(string='Next Cloud Folder Id',
help="Field used to store the unique "
"identifier for a Nextcloud folder.")
gdrive_backup_error_test = fields.Boolean(string="Google Drive Error Test")
onedrive_backup_error_test = fields.Boolean(string="OneDrive Error Test")
@api.onchange('backup_destination')
def _onchange_backup_destination(self):
self.write({
"gdrive_backup_error_test": False,
"onedrive_backup_error_test": False
})
@api.onchange('gdrive_client_key', 'gdrive_client_secret',
'google_drive_folder', 'onedrive_client_key',
'onedrive_client_secret', 'onedrive_folder_key')
def _onchange_gdrive_backup_error_test(self):
if self.backup_destination == 'google_drive':
if self.gdrive_backup_error_test:
self.write({
"gdrive_backup_error_test": False
})
if self.backup_destination == 'onedrive':
if self.onedrive_backup_error_test:
self.write({
"onedrive_backup_error_test": False
})
def action_nextcloud(self):
"""If it has next_cloud_password, domain, and next_cloud_user_name
@ -453,11 +477,13 @@ class DbBackupConfigure(models.Model):
'gdrive_token_validity': fields.Datetime.now() + timedelta(
seconds=expires_in) if expires_in else False,
})
except requests.HTTPError:
error_msg = _(
"Something went wrong during your token generation. Maybe "
"your Authorization Code is invalid")
raise UserError(error_msg)
if self.gdrive_backup_error_test:
self.write({
'gdrive_backup_error_test': False
})
except Exception:
if not self.gdrive_backup_error_test:
self.write({"gdrive_backup_error_test": True})
@api.depends('onedrive_access_token', 'onedrive_refresh_token')
def _compute_is_onedrive_token_generated(self):
@ -566,10 +592,13 @@ class DbBackupConfigure(models.Model):
'onedrive_token_validity': fields.Datetime.now() + timedelta
(seconds=expires_in) if expires_in else False,
})
except requests.HTTPError as error:
_logger.exception("Bad microsoft onedrive request : %s !",
error.response.content)
raise error
if self.onedrive_backup_error_test:
self.write({
'onedrive_backup_error_test': False
})
except Exception:
if not self.onedrive_backup_error_test:
self.write({"onedrive_backup_error_test": True})
def get_dropbox_auth_url(self):
"""Return dropbox authorization url"""
@ -580,11 +609,15 @@ class DbBackupConfigure(models.Model):
def set_dropbox_refresh_token(self, auth_code):
"""Generate and set the dropbox refresh token from authorization code"""
try:
dbx_auth = dropbox.oauth.DropboxOAuth2FlowNoRedirect(
self.dropbox_client_key, self.dropbox_client_secret,
token_access_type='offline')
outh_result = dbx_auth.finish(auth_code)
self.dropbox_refresh_token = outh_result.refresh_token
except Exception:
raise ValidationError(
'Please Enter Valid Authentication Code')
@api.constrains('db_name')
def _check_db_credentials(self):
@ -764,6 +797,7 @@ class DbBackupConfigure(models.Model):
client.close()
# Google Drive backup
elif rec.backup_destination == 'google_drive':
try:
if (rec.gdrive_token_validity is not False and
rec.gdrive_token_validity <= fields.Datetime.now()):
rec.generate_gdrive_refresh_token()
@ -815,8 +849,16 @@ class DbBackupConfigure(models.Model):
_logger.info('Google Drive Exception: %s', e)
if rec.notify_user:
mail_template_failed.send_mail(rec.id, force_send=True)
except Exception:
if rec.notify_user:
mail_template_failed.send_mail(rec.id, force_send=True)
raise ValidationError(
'Please check the credentials before activation')
else:
raise ValidationError('Please check connection')
# Dropbox backup
elif rec.backup_destination == 'dropbox':
try:
temp = tempfile.NamedTemporaryFile(
suffix='.%s' % rec.backup_format)
with open(temp.name, "wb+") as tmp:
@ -847,8 +889,16 @@ class DbBackupConfigure(models.Model):
_logger.info('Dropbox Exception: %s', error)
if rec.notify_user:
mail_template_failed.send_mail(rec.id, force_send=True)
except Exception:
if rec.notify_user:
mail_template_failed.send_mail(rec.id, force_send=True)
raise ValidationError(
'Please check the credentials before activation')
else:
raise ValidationError('Please check connection')
# Onedrive Backup
elif rec.backup_destination == 'onedrive':
try:
if (rec.onedrive_token_validity is not False and
rec.onedrive_token_validity <= fields.Datetime.now()):
rec.generate_onedrive_refresh_token()
@ -887,6 +937,13 @@ class DbBackupConfigure(models.Model):
_logger.info('Onedrive Exception: %s', error)
if rec.notify_user:
mail_template_failed.send_mail(rec.id, force_send=True)
except Exception:
if rec.notify_user:
mail_template_failed.send_mail(rec.id, force_send=True)
raise ValidationError(
'Please check the credentials before activation')
else:
raise ValidationError('Please check connection')
# amazon S3 backup
elif rec.backup_destination == 'amazon_s3':
if rec.aws_access_key and rec.aws_secret_access_key:
@ -965,6 +1022,7 @@ class DbBackupConfigure(models.Model):
force_send=True)
# nextcloud backup
elif rec.backup_destination == 'next_cloud':
try:
if rec.domain and rec.next_cloud_password and \
rec.next_cloud_user_name:
try:
@ -1039,3 +1097,10 @@ class DbBackupConfigure(models.Model):
remote_file_path = f"/{folder_name}/{rec.db_name}_" \
f"{backup_time}.{rec.backup_format}"
nc.put_file(remote_file_path, backup_file_path)
except Exception:
if rec.notify_user:
mail_template_failed.send_mail(rec.id, force_send=True)
raise ValidationError(
'Please check the credentials before activation')
else:
raise ValidationError('Please check connection')

21
auto_database_backup/views/db_backup_configure_views.xml

@ -20,6 +20,8 @@
<field name="arch" type="xml">
<form>
<sheet>
<field name="gdrive_backup_error_test" invisible="1"/>
<field name="onedrive_backup_error_test" invisible="1"/>
<div class="oe_title">
<h1>
<field name="name" placeholder="Name..."/>
@ -128,7 +130,21 @@
</button>
</div>
</div>
<div>
<div class="alert alert-danger" role="alert" style="margin-bottom:0px;width: 229%;" attrs="{'invisible': [('gdrive_backup_error_test', '=', False)]}">
Something went wrong during your token generation. Maybe your Authorization Code is invalid
</div>
<div attrs="{'invisible': [('backup_destination', '!=', 'onedrive')]}">
<div attrs="{'invisible': ['|', ('backup_destination', '!=', 'onedrive'), ('is_onedrive_token_generated', '=', False)]}">
<i class="text-success fa fa-check"/>
Refresh token set
</div>
<div attrs="{'invisible': ['|', ('backup_destination', '!=', 'onedrive'), ('is_onedrive_token_generated', '=', True)]}">
<i class="fa fa-exclamation-triangle text-warning"/>
No refresh token set
</div>
</div>
<div attrs="{'invisible': [('backup_destination', '!=', 'onedrive')]}">
<div attrs="{'invisible': ['|', ('backup_destination', '!=', 'onedrive'), ('is_onedrive_token_generated', '=', True)]}">
<button class="btn btn-link"
name="action_get_onedrive_auth_code"
@ -146,6 +162,9 @@
</button>
</div>
</div>
<div class="alert alert-danger" role="alert" style="margin-bottom:0px;width: 91%%;" attrs="{'invisible': [('onedrive_backup_error_test', '=', False)]}">
Bad microsoft onedrive request. Maybe your Authorization Code is invalid
</div>
<field name="dropbox_refresh_token" invisible="1"/>
<field name="is_dropbox_token_generated" invisible="1"/>
<field name="dropbox_folder" attrs="{'invisible': [('backup_destination', '!=', 'dropbox')], 'required': [('backup_destination', '=', 'dropbox')]}"/>

Loading…
Cancel
Save