3 from PyQt4.QtCore import *
6 import pcsbackuputils as utils
9 HOME = os.path.expanduser("~")
11 DEVICES_POINT = "%s/.pcsuite/devices/" % HOME
14 class PcsBackupManager(QObject):
17 QObject.__init__(self)
20 def loadBackups(self):
23 def saveBackups(self):
26 def getBackupList(self):
29 def createBackup(self, backup_name, path, host_ip, categories, comment=""):
32 def removeBackup(self, backup_name):
35 def getBackupInfo(self, backupName):
38 def renameBackup(self, backupName, newName):
41 def changeBackupComment(self, backupName, new_comment):
44 def listBackupContent(self, backupName):
46 backupInfo = self.getBackupInfo(backupName)
47 backupPath = backupInfo.getPath()
48 fullPath = os.path.join(str(backupPath), str(backupName))
50 for entry in os.listdir(fullPath):
51 if entry.endswith(".zip"):
52 zipfile = utils.openZip(os.path.join(fullPath, entry), "r")
53 for member in zipfile.namelist():
54 folders = member.split("/")
55 memberName = "../" + "/".join([folders[-2], folders[-1]])
56 content.append(memberName)
59 def restoreBackup(self, backupInfo, host_ip, categories):
60 """ Restore a PC backup to device with given IP address.
63 String backupInfo - Object representing the backup
64 String host_ip - IP address of device.
65 Dictionary categories - dictionary with categories as keys and with
66 value True if that category should be restored.
69 self.setRestoreInProgress(True)
70 # Set restore needed paths
71 devicePath = os.path.join(DEVICES_POINT, "%s" % host_ip)
72 mountPath = os.path.join(devicePath, "Root" )
73 tempPath = os.path.join(mountPath, "tmp/paths")
74 restScriptsPath = ("/etc/osso-backup/restore.d/always")
76 utils.mountDevice(USER_HOST, host_ip, mountPath)
77 # Get backup location depending from backup source
78 if backupInfo == None:
80 if backupInfo.fromDevice:
81 backup_path = backupInfo.getPath()
83 backup_path = os.path.join(str(backupInfo.getPath()),
84 str(backupInfo.getName()))
85 # Get backup files list for each category and write it on a file
86 # that will be needed by restore scripts.
87 pathsDictonary = utils.getBackupFilesPath(backup_path)
88 if utils.writeBackupFilesPath(pathsDictonary, tempPath) == False:
90 # --- Initialize restore progress ---
92 # Get total number of files to restore
94 for categ in pathsDictonary:
95 for file in pathsDictonary[categ]:
97 # Get size of all categories being restored
99 for file in os.listdir(backup_path):
100 if file.endswith(".zip"):
102 if categories[categ]:
103 catPath = os.path.join(backup_path, file)
104 zip = utils.openZip(catPath)
105 for member in zip.namelist():
106 totalSize += zip.getinfo(member).file_size
107 # Extract zip files to device
108 for entry in os.listdir(backup_path):
109 category = entry[:-4]
110 if entry.endswith(".zip") and categories[category]:
111 zipPath = os.path.join(backup_path, entry)
112 zip = utils.openZip(zipPath)
113 # Update restore progress, extract current f print "member %s: %.2f" % (member, zip.getinfo(member).file_size)ile and emit
115 for member in zip.namelist():
116 if not self.restoreInProgress:
118 percentage = "%.1f" % self.computePercentage(totalSize,
121 status = (percentage, category, numberOfFiles, totalSize)
122 self.emit(SIGNAL("restoreProgress"), status)
123 zip.extract(member, devicePath)
124 currentSize += zip.getinfo(member).file_size
125 percentage = "%.1f" % ((currentSize / float(totalSize)) * 100)
126 status = (percentage, category, numberOfFiles, totalSize)
127 self.emit(SIGNAL("restoreProgress"), status)
129 # Execute restore scripts
130 os.system("ssh %s@%s ..%s/*.sh %s" % (USER_HOST, host_ip,
131 restScriptsPath, tempPath))
132 self.setRestoreInProgress(False)
133 # --- Restore finished ---
135 utils.unmountDevice(mountPath)
138 def computePercentage(self, totalSize, currentSize):
142 percentage = (currentSize / float(totalSize)) * 100
147 def copy(self, sourcePath, destinationPath):
149 for entry in os.listdir(sourcePath):
150 zipPath = os.path.join(sourcePath, entry)
151 if zipPath.endswith(".zip"):
152 zip = utils.openZip(zipPath)
153 numberOfFiles += len(zip.namelist())
154 totalSize = float(utils.getSize(sourcePath))
156 self.emit(SIGNAL("copyProgress"), ("0.00", numberOfFiles, totalSize))
157 for entry in os.listdir(sourcePath):
158 if not self.copyInProgress:
159 utils.removePath(destinationPath)
161 entryPath = os.path.join(sourcePath, entry)
162 utils.copy(entryPath, destinationPath)
163 currentSize += utils.getSize(entryPath)
164 progress = "%.2f" % ((currentSize / totalSize) * 100)
165 self.emit(SIGNAL("copyProgress"), (progress, numberOfFiles,