Skip to content

Commit

Permalink
[IMP] auto_backup: better handling of SFTP connections and cleanup
Browse files Browse the repository at this point in the history
[IMP] auto_backup: better handling of SFTP connections and cleanup
Closes #188
  • Loading branch information
Yenthe666 authored Aug 27, 2020
2 parents aac5ed4 + 3419d00 commit 4d539e9
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions auto_backup/models/db_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,15 @@ def schedule_backup(self):
# Navigate in to the correct folder.
sftp.chdir(path_to_write_to)

_logger.debug("Checking expired files")
# Loop over all files in the directory from the back-ups.
# We will check the creation date of every back-up.
for file in sftp.listdir(path_to_write_to):
if rec.name in file:
# Get the full path
fullpath = os.path.join(path_to_write_to, file)
# Get the timestamp from the file on the external server
timestamp = sftp.stat(fullpath).st_atime
timestamp = sftp.stat(fullpath).st_mtime
createtime = datetime.datetime.fromtimestamp(timestamp)
now = datetime.datetime.now()
delta = now - createtime
Expand All @@ -218,8 +219,14 @@ def schedule_backup(self):
sftp.unlink(file)
# Close the SFTP session.
sftp.close()
s.close()
except Exception as e:
_logger.debug('Exception! We couldn\'t back up to the FTP server..')
try:
sftp.close()
s.close()
except:
pass
_logger.error('Exception! We couldn\'t back up to the FTP server. Here is what we got back instead: %s' % str(e))
# At this point the SFTP backup failed. We will now check if the user wants
# an e-mail notification about this.
if rec.send_mail_sftp_fail:
Expand Down

0 comments on commit 4d539e9

Please sign in to comment.