Backup config before writing to it

Fixes #234
This commit is contained in:
planet36 2020-10-08 17:11:32 -04:00
parent 296e1257a2
commit ebae1e1440
1 changed files with 7 additions and 4 deletions

View File

@ -71,6 +71,7 @@ KeyError: 'ConfigBase::get_item: unknown key algo'
""" """
import os import os
import shutil
from copy import copy from copy import copy
from configobj import ConfigObj, flatten_errors from configobj import ConfigObj, flatten_errors
from validate import Validator from validate import Validator
@ -719,12 +720,14 @@ class ConfigBase(Borg):
if not os.path.isdir(config_dir): if not os.path.isdir(config_dir):
os.makedirs(config_dir) os.makedirs(config_dir)
try: try:
temp_file = self.command_line_options.config + '.tmp' backup_file = self.command_line_options.config + '~'
with open(temp_file, 'wb') as fh: shutil.copy2(self.command_line_options.config, backup_file)
with open(self.command_line_options.config, 'wb') as fh:
parser.write(fh) parser.write(fh)
os.rename(temp_file, self.command_line_options.config) os.remove(backup_file)
except Exception as ex: except Exception as ex:
err('ConfigBase::save: Unable to save config: %s' % ex) err('ConfigBase::save: Unable to save config: %s' % ex)