From 85dadced9e03f6df01dad6eeeb348738c9163af1 Mon Sep 17 00:00:00 2001 From: Alexandre Beloin Date: Thu, 19 Feb 2015 10:39:07 -0500 Subject: [PATCH] Fix backup issue with invalid restore folder --- SickBeard.py | 15 +++++++++------ sickbeard/__init__.py | 42 +++++++++++++++++++++++------------------- sickbeard/helpers.py | 7 +++++++ 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/SickBeard.py b/SickBeard.py index 309687e3..6037bd52 100755 --- a/SickBeard.py +++ b/SickBeard.py @@ -262,12 +262,15 @@ class SickRage(object): os.chdir(sickbeard.DATA_DIR) # Check if we need to perform a restore first - restoreDir = os.path.join(sickbeard.DATA_DIR, 'restore') - if self.consoleLogging and os.path.exists(restoreDir): - if self.restoreDB(restoreDir, sickbeard.DATA_DIR): - sys.stdout.write("Restore: restoring DB and config.ini successful...\n") - else: - sys.stdout.write("Restore: restoring DB and config.ini FAILED!\n") + try: + restoreDir = os.path.join(sickbeard.DATA_DIR, 'restore') + if self.consoleLogging and os.path.exists(restoreDir): + if self.restoreDB(restoreDir, sickbeard.DATA_DIR): + sys.stdout.write("Restore: restoring DB and config.ini successful...\n") + else: + sys.stdout.write("Restore: restoring DB and config.ini FAILED!\n") + except Exception as e: + sys.stdout.write("Restore: restoring DB and config.ini FAILED!\n") # Load the config and publish it to the sickbeard package if self.consoleLogging and not os.path.isfile(sickbeard.CONFIG_FILE): diff --git a/sickbeard/__init__.py b/sickbeard/__init__.py index a477606d..946713c0 100755 --- a/sickbeard/__init__.py +++ b/sickbeard/__init__.py @@ -633,29 +633,33 @@ def initialize(consoleLogging=True): CACHE_DIR = None # Check if we need to perform a restore of the cache folder - restoreDir = os.path.join(DATA_DIR, 'restore') - if os.path.exists(restoreDir): - def restoreCache(srcDir, dstDir): - import ntpath - import shutil + try: + restoreDir = os.path.join(DATA_DIR, 'restore') + if os.path.exists(restoreDir) and os.path.exists(os.path.join(restoreDir, 'cache')): + def restoreCache(srcDir, dstDir): + import ntpath + import shutil - def path_leaf(path): - head, tail = ntpath.split(path) - return tail or ntpath.basename(head) + def path_leaf(path): + head, tail = ntpath.split(path) + return tail or ntpath.basename(head) - try: - if os.path.isdir(dstDir): - bakFilename = '{0}-{1}'.format(path_leaf(dstDir), datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d_%H%M%S')) - shutil.move(dstDir, os.path.join(ntpath.dirname(dstDir), bakFilename)) + try: + if os.path.isdir(dstDir): + bakFilename = '{0}-{1}'.format(path_leaf(dstDir), datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d_%H%M%S')) + shutil.move(dstDir, os.path.join(ntpath.dirname(dstDir), bakFilename)) - shutil.move(srcDir, dstDir) - logger.log(u"Restore: restoring cache successful", logger.INFO) - except Exception as e: - logger.log(u"Restore: restoring cache failed", logger.ERROR) - finally: - os.rmdir(restoreDir) + shutil.move(srcDir, dstDir) + logger.log(u"Restore: restoring cache successful", logger.INFO) + except Exception as e: + logger.log(u"Restore: restoring cache failed: {0}".format(str(e)), logger.ERROR) - restoreCache(os.path.join(restoreDir, 'cache'), CACHE_DIR) + restoreCache(os.path.join(restoreDir, 'cache'), CACHE_DIR) + except Exception as e: + logger.log(u"Restore: restoring cache failed: {0}".format(str(e)), logger.ERROR) + finally: + if os.path.exists(os.path.join(DATA_DIR, 'restore')): + os.rmdir(os.path.join(DATA_DIR, 'restore')) # clean cache folders if CACHE_DIR: diff --git a/sickbeard/helpers.py b/sickbeard/helpers.py index c13af25d..033ee36e 100644 --- a/sickbeard/helpers.py +++ b/sickbeard/helpers.py @@ -1080,9 +1080,16 @@ def backupConfigZip(fileList, archive, arcname = None): def restoreConfigZip(archive, targetDir): + import ntpath try: if not os.path.exists(targetDir): os.mkdir(targetDir) + else: + def path_leaf(path): + head, tail = ntpath.split(path) + return tail or ntpath.basename(head) + bakFilename = '{0}-{1}'.format(path_leaf(targetDir), datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d_%H%M%S')) + shutil.move(targetDir, os.path.join(ntpath.dirname(targetDir), bakFilename)) zip_file = zipfile.ZipFile(archive, 'r') for member in zip_file.namelist():