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 " 'name': "Automatic Database Backup To Local Server, Remote Server, Google "
"Drive, Dropbox, Onedrive, Nextcloud and Amazon S3", "Drive, Dropbox, Onedrive, Nextcloud and Amazon S3",
'version': '15.0.4.1.2', 'version': '15.0.5.1.0',
'category': 'Discuss,Extra Tools', 'category': 'Discuss,Extra Tools',
'summary': """Generate automatic backup of databases and store to local, 'summary': """Generate automatic backup of databases and store to local,
google drive, dropbox, nextcloud, amazon S3, onedrive or 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 #### Version 15.0.4.1.2
#### ADD #### ADD
- Nextcloud and Amazon S3 integration added. Backup can be stored into Nextcloud and Amazon S3. - 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', nextcloud_folder_key = fields.Char(string='Next Cloud Folder Id',
help="Field used to store the unique " help="Field used to store the unique "
"identifier for a Nextcloud folder.") "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): def action_nextcloud(self):
"""If it has next_cloud_password, domain, and next_cloud_user_name """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( 'gdrive_token_validity': fields.Datetime.now() + timedelta(
seconds=expires_in) if expires_in else False, seconds=expires_in) if expires_in else False,
}) })
except requests.HTTPError: if self.gdrive_backup_error_test:
error_msg = _( self.write({
"Something went wrong during your token generation. Maybe " 'gdrive_backup_error_test': False
"your Authorization Code is invalid") })
raise UserError(error_msg) except Exception:
if not self.gdrive_backup_error_test:
self.write({"gdrive_backup_error_test": True})
@api.depends('onedrive_access_token', 'onedrive_refresh_token') @api.depends('onedrive_access_token', 'onedrive_refresh_token')
def _compute_is_onedrive_token_generated(self): def _compute_is_onedrive_token_generated(self):
@ -566,10 +592,13 @@ class DbBackupConfigure(models.Model):
'onedrive_token_validity': fields.Datetime.now() + timedelta 'onedrive_token_validity': fields.Datetime.now() + timedelta
(seconds=expires_in) if expires_in else False, (seconds=expires_in) if expires_in else False,
}) })
except requests.HTTPError as error: if self.onedrive_backup_error_test:
_logger.exception("Bad microsoft onedrive request : %s !", self.write({
error.response.content) 'onedrive_backup_error_test': False
raise error })
except Exception:
if not self.onedrive_backup_error_test:
self.write({"onedrive_backup_error_test": True})
def get_dropbox_auth_url(self): def get_dropbox_auth_url(self):
"""Return dropbox authorization url""" """Return dropbox authorization url"""
@ -580,11 +609,15 @@ class DbBackupConfigure(models.Model):
def set_dropbox_refresh_token(self, auth_code): def set_dropbox_refresh_token(self, auth_code):
"""Generate and set the dropbox refresh token from authorization code""" """Generate and set the dropbox refresh token from authorization code"""
try:
dbx_auth = dropbox.oauth.DropboxOAuth2FlowNoRedirect( dbx_auth = dropbox.oauth.DropboxOAuth2FlowNoRedirect(
self.dropbox_client_key, self.dropbox_client_secret, self.dropbox_client_key, self.dropbox_client_secret,
token_access_type='offline') token_access_type='offline')
outh_result = dbx_auth.finish(auth_code) outh_result = dbx_auth.finish(auth_code)
self.dropbox_refresh_token = outh_result.refresh_token self.dropbox_refresh_token = outh_result.refresh_token
except Exception:
raise ValidationError(
'Please Enter Valid Authentication Code')
@api.constrains('db_name') @api.constrains('db_name')
def _check_db_credentials(self): def _check_db_credentials(self):
@ -764,6 +797,7 @@ class DbBackupConfigure(models.Model):
client.close() client.close()
# Google Drive backup # Google Drive backup
elif rec.backup_destination == 'google_drive': elif rec.backup_destination == 'google_drive':
try:
if (rec.gdrive_token_validity is not False and if (rec.gdrive_token_validity is not False and
rec.gdrive_token_validity <= fields.Datetime.now()): rec.gdrive_token_validity <= fields.Datetime.now()):
rec.generate_gdrive_refresh_token() rec.generate_gdrive_refresh_token()
@ -815,8 +849,16 @@ class DbBackupConfigure(models.Model):
_logger.info('Google Drive Exception: %s', e) _logger.info('Google Drive Exception: %s', e)
if rec.notify_user: if rec.notify_user:
mail_template_failed.send_mail(rec.id, force_send=True) 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 # Dropbox backup
elif rec.backup_destination == 'dropbox': elif rec.backup_destination == 'dropbox':
try:
temp = tempfile.NamedTemporaryFile( temp = tempfile.NamedTemporaryFile(
suffix='.%s' % rec.backup_format) suffix='.%s' % rec.backup_format)
with open(temp.name, "wb+") as tmp: with open(temp.name, "wb+") as tmp:
@ -847,8 +889,16 @@ class DbBackupConfigure(models.Model):
_logger.info('Dropbox Exception: %s', error) _logger.info('Dropbox Exception: %s', error)
if rec.notify_user: if rec.notify_user:
mail_template_failed.send_mail(rec.id, force_send=True) 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 # Onedrive Backup
elif rec.backup_destination == 'onedrive': elif rec.backup_destination == 'onedrive':
try:
if (rec.onedrive_token_validity is not False and if (rec.onedrive_token_validity is not False and
rec.onedrive_token_validity <= fields.Datetime.now()): rec.onedrive_token_validity <= fields.Datetime.now()):
rec.generate_onedrive_refresh_token() rec.generate_onedrive_refresh_token()
@ -887,6 +937,13 @@ class DbBackupConfigure(models.Model):
_logger.info('Onedrive Exception: %s', error) _logger.info('Onedrive Exception: %s', error)
if rec.notify_user: if rec.notify_user:
mail_template_failed.send_mail(rec.id, force_send=True) 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 # amazon S3 backup
elif rec.backup_destination == 'amazon_s3': elif rec.backup_destination == 'amazon_s3':
if rec.aws_access_key and rec.aws_secret_access_key: if rec.aws_access_key and rec.aws_secret_access_key:
@ -965,6 +1022,7 @@ class DbBackupConfigure(models.Model):
force_send=True) force_send=True)
# nextcloud backup # nextcloud backup
elif rec.backup_destination == 'next_cloud': elif rec.backup_destination == 'next_cloud':
try:
if rec.domain and rec.next_cloud_password and \ if rec.domain and rec.next_cloud_password and \
rec.next_cloud_user_name: rec.next_cloud_user_name:
try: try:
@ -1039,3 +1097,10 @@ class DbBackupConfigure(models.Model):
remote_file_path = f"/{folder_name}/{rec.db_name}_" \ remote_file_path = f"/{folder_name}/{rec.db_name}_" \
f"{backup_time}.{rec.backup_format}" f"{backup_time}.{rec.backup_format}"
nc.put_file(remote_file_path, backup_file_path) 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"> <field name="arch" type="xml">
<form> <form>
<sheet> <sheet>
<field name="gdrive_backup_error_test" invisible="1"/>
<field name="onedrive_backup_error_test" invisible="1"/>
<div class="oe_title"> <div class="oe_title">
<h1> <h1>
<field name="name" placeholder="Name..."/> <field name="name" placeholder="Name..."/>
@ -128,7 +130,21 @@
</button> </button>
</div> </div>
</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)]}"> <div attrs="{'invisible': ['|', ('backup_destination', '!=', 'onedrive'), ('is_onedrive_token_generated', '=', True)]}">
<button class="btn btn-link" <button class="btn btn-link"
name="action_get_onedrive_auth_code" name="action_get_onedrive_auth_code"
@ -146,6 +162,9 @@
</button> </button>
</div> </div>
</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="dropbox_refresh_token" invisible="1"/>
<field name="is_dropbox_token_generated" invisible="1"/> <field name="is_dropbox_token_generated" invisible="1"/>
<field name="dropbox_folder" attrs="{'invisible': [('backup_destination', '!=', 'dropbox')], 'required': [('backup_destination', '=', 'dropbox')]}"/> <field name="dropbox_folder" attrs="{'invisible': [('backup_destination', '!=', 'dropbox')], 'required': [('backup_destination', '=', 'dropbox')]}"/>

Loading…
Cancel
Save