Always remove temp files and close their descriptors
The inputs temp file was left behind when no inputs existed, and both temp files if rsync failed to start. The descriptors returned by mkstemp were also never closed. Remove the temp files when run() exits in any way, and write them through the mkstemp descriptors.
This commit is contained in:
@@ -494,6 +494,20 @@ class Backup:
|
|||||||
def run(self) -> int:
|
def run(self) -> int:
|
||||||
"""Perform the backup"""
|
"""Perform the backup"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
return self._run()
|
||||||
|
finally:
|
||||||
|
self._remove_temp_files()
|
||||||
|
|
||||||
|
def _remove_temp_files(self) -> None:
|
||||||
|
for path in [self._inputs_path, self._exclude_path]:
|
||||||
|
if path != '':
|
||||||
|
try:
|
||||||
|
os.remove(path)
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def _run(self) -> int:
|
||||||
logger.info('Starting backup...')
|
logger.info('Starting backup...')
|
||||||
|
|
||||||
_notify('Starting backup...')
|
_notify('Starting backup...')
|
||||||
@@ -501,10 +515,10 @@ class Backup:
|
|||||||
self.define_backup_dir()
|
self.define_backup_dir()
|
||||||
self.find_last_backup()
|
self.find_last_backup()
|
||||||
|
|
||||||
_, self._inputs_path = mkstemp(prefix='tmp_inputs', text=True)
|
fd, self._inputs_path = mkstemp(prefix='tmp_inputs', text=True)
|
||||||
count = 0
|
count = 0
|
||||||
|
|
||||||
with open(self._inputs_path, 'w', encoding='utf-8') as fp:
|
with os.fdopen(fd, 'w', encoding='utf-8') as fp:
|
||||||
for i in self.inputs:
|
for i in self.inputs:
|
||||||
if not os.path.exists(i):
|
if not os.path.exists(i):
|
||||||
logger.warning('Input %s not found. Skipping', i)
|
logger.warning('Input %s not found. Skipping', i)
|
||||||
@@ -522,9 +536,9 @@ class Backup:
|
|||||||
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
_, self._exclude_path = mkstemp(prefix='tmp_exclude', text=True)
|
fd, self._exclude_path = mkstemp(prefix='tmp_exclude', text=True)
|
||||||
|
|
||||||
with open(self._exclude_path, 'w', encoding='utf-8') as fp:
|
with os.fdopen(fd, 'w', encoding='utf-8') as fp:
|
||||||
if self.exclude is not None:
|
if self.exclude is not None:
|
||||||
for e in self.exclude:
|
for e in self.exclude:
|
||||||
fp.write(e)
|
fp.write(e)
|
||||||
@@ -591,9 +605,6 @@ class Backup:
|
|||||||
else:
|
else:
|
||||||
logger.warning('Backup not completed successfully. Old backups will not be removed')
|
logger.warning('Backup not completed successfully. Old backups will not be removed')
|
||||||
|
|
||||||
os.remove(self._inputs_path)
|
|
||||||
os.remove(self._exclude_path)
|
|
||||||
|
|
||||||
if self._remote:
|
if self._remote:
|
||||||
assert self._ssh is not None
|
assert self._ssh is not None
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user