import os from PyQt4.QtCore import * from zipfile import * import pcsbackuputils as utils HOME = os.path.expanduser("~") USER_HOST = "root" DEVICES_POINT = "%s/.pcsuite/devices/" % HOME class PcsBackupManager(QObject): def __init__(self): QObject.__init__(self) self._backupList = [] def loadBackups(self): return False def saveBackups(self): return False def getBackupList(self): return None def createBackup(self, backup_name, path, host_ip, categories, comment=""): return False def removeBackup(self, backup_name): return False def getBackupInfo(self, backupName): return None def renameBackup(self, backupName, newName): return False def changeBackupComment(self, backupName, new_comment): return False def listBackupContent(self, backupName): content = [] backupInfo = self.getBackupInfo(backupName) backupPath = backupInfo.getPath() fullPath = os.path.join(str(backupPath), str(backupName)) for entry in os.listdir(fullPath): if entry.endswith(".zip"): zipfile = utils.openZip(os.path.join(fullPath, entry), "r") for member in zipfile.namelist(): folders = member.split("/") memberName = "../" + "/".join([folders[-2], folders[-1]]) content.append(memberName) return content def restoreBackup(self, backupInfo, host_ip, categories): """ Restore a PC backup to device with given IP address. Attributes: String backupInfo - Object representing the backup String host_ip - IP address of device. Dictionary categories - dictionary with categories as keys and with value True if that category should be restored. """ self.setRestoreInProgress(True) # Set restore needed paths devicePath = os.path.join(DEVICES_POINT, "%s" % host_ip) mountPath = os.path.join(devicePath, "Root" ) tempPath = os.path.join(mountPath, "tmp/paths") restScriptsPath = ("/etc/osso-backup/restore.d/always") try: utils.mountDevice(USER_HOST, host_ip, mountPath) # Get backup location depending from backup source if backupInfo == None: return False if backupInfo.fromDevice: backup_path = backupInfo.getPath() else: backup_path = os.path.join(str(backupInfo.getPath()), str(backupInfo.getName())) # Get backup files list for each category and write it on a file # that will be needed by restore scripts. pathsDictonary = utils.getBackupFilesPath(backup_path) if utils.writeBackupFilesPath(pathsDictonary, tempPath) == False: return False # --- Initialize restore progress --- currentSize = 0 # Get total number of files to restore numberOfFiles = 0 for categ in pathsDictonary: for file in pathsDictonary[categ]: numberOfFiles += 1 # Get size of all categories being restored totalSize = 0 for file in os.listdir(backup_path): if file.endswith(".zip"): categ = file[:-4] if categories[categ]: catPath = os.path.join(backup_path, file) zip = utils.openZip(catPath) for member in zip.namelist(): totalSize += zip.getinfo(member).file_size # Extract zip files to device for entry in os.listdir(backup_path): category = entry[:-4] if entry.endswith(".zip") and categories[category]: zipPath = os.path.join(backup_path, entry) zip = utils.openZip(zipPath) # Update restore progress, extract current f print "member %s: %.2f" % (member, zip.getinfo(member).file_size)ile and emit # progress sinal for member in zip.namelist(): if not self.restoreInProgress: return 0 percentage = "%.1f" % self.computePercentage(totalSize, currentSize) status = (percentage, category, numberOfFiles, totalSize) self.emit(SIGNAL("restoreProgress"), status) zip.extract(member, devicePath) currentSize += zip.getinfo(member).file_size percentage = "%.1f" % ((currentSize / float(totalSize)) * 100) status = (percentage, category, numberOfFiles, totalSize) self.emit(SIGNAL("restoreProgress"), status) zip.close() # Execute restore scripts os.system("ssh %s@%s ..%s/*.sh %s" % (USER_HOST, host_ip, restScriptsPath, tempPath)) self.setRestoreInProgress(False) # --- Restore finished --- finally: utils.unmountDevice(mountPath) def computePercentage(self, totalSize, currentSize): if totalSize == 0: percentage = 100 else: percentage = (currentSize / float(totalSize)) * 100 if percentage > 100: percentage = 100 return percentage def copy(self, sourcePath, destinationPath): numberOfFiles = 0 for entry in os.listdir(sourcePath): zipPath = os.path.join(sourcePath, entry) if zipPath.endswith(".zip"): zip = utils.openZip(zipPath) numberOfFiles += len(zip.namelist()) totalSize = float(utils.getSize(sourcePath)) currentSize = 0 self.emit(SIGNAL("copyProgress"), ("0.00", numberOfFiles, totalSize)) for entry in os.listdir(sourcePath): if not self.copyInProgress: utils.removePath(destinationPath) return 0 entryPath = os.path.join(sourcePath, entry) utils.copy(entryPath, destinationPath) currentSize += utils.getSize(entryPath) progress = "%.2f" % ((currentSize / totalSize) * 100) self.emit(SIGNAL("copyProgress"), (progress, numberOfFiles, totalSize))