Fix backup issue with invalid restore folder

This commit is contained in:
Alexandre Beloin 2015-02-19 10:39:07 -05:00
parent 0a73bd1405
commit 85dadced9e
3 changed files with 39 additions and 25 deletions

View File

@ -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):

View File

@ -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:

View File

@ -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():