Beispiel #1
0
def restoreFileOrDir(repoRoot, dirPath, filename, restoreDate, useZip):
    """ returns a file path to the file.  User is responsible for deleting file, as well as containing dir, after use. """
    filePath = joinPaths(dirPath, filename)
    filePath = rdiffQuotedPath(repoRoot).getQuotedPath(filePath)
    checkRepoPath(repoRoot, filePath)

    restoredFilename = filename
    if restoredFilename == "/":
        restoredFilename = "(root)"

    fileToRestore = joinPaths(repoRoot, dirPath, filename)
    dateString = str(restoreDate.getSeconds())
    rdiffOutputFile = joinPaths(
        tempfile.mkdtemp(),
        restoredFilename)  # TODO: make so this includes the username
    results = rdw_helpers.execute("rdiff-backup",
                                  "--restore-as-of=" + dateString,
                                  fileToRestore, rdiffOutputFile)
    if results['exitCode'] != 0 or not os.access(rdiffOutputFile, os.F_OK):
        error = results['stderr']
        if not error:
            error = 'rdiff-backup claimed success, but did not restore anything. This indicates a bug in rdiffWeb. Please report this to a developer.'
        raise UnknownError('Unable to restore! rdiff-backup output:\n' + error)
    if os.path.isdir(rdiffOutputFile):
        if useZip:
            rdw_helpers.recursiveZipDir(rdiffOutputFile,
                                        rdiffOutputFile + ".zip")
            rdw_helpers.removeDir(rdiffOutputFile)
            rdiffOutputFile = rdiffOutputFile + ".zip"
        else:
            rdw_helpers.recursiveTarDir(rdiffOutputFile,
                                        rdiffOutputFile + ".tar.gz")
            rdw_helpers.removeDir(rdiffOutputFile)
            rdiffOutputFile = rdiffOutputFile + ".tar.gz"
    return rdiffOutputFile
def restoreFileOrDir(repoRoot, dirPath, filename, restoreDate):
    """ returns a file path to the file.  User is responsible for deleting file, as well as containing dir, after use. """
    checkRepoPath(repoRoot, joinPaths(dirPath, filename))

    restoredFilename = filename
    if restoredFilename == "/":
        restoredFilename = "(root)"

    fileToRestore = joinPaths(repoRoot, dirPath, filename)
    dateString = str(restoreDate.getSeconds())
    rdiffOutputFile = joinPaths(
        tempfile.mkdtemp(),
        restoredFilename)  # TODO: make so this includes the username
    args = [
        "rdiff-backup", "--restore-as-of=" + dateString, fileToRestore,
        rdiffOutputFile
    ]
    os.spawnvp(os.P_WAIT, args[0], args)
    if not os.access(rdiffOutputFile, os.F_OK):
        raise UnknownError()
    if os.path.isdir(rdiffOutputFile):
        rdw_helpers.recursiveZipDir(rdiffOutputFile, rdiffOutputFile + ".zip")
        rdw_helpers.removeDir(rdiffOutputFile)
        rdiffOutputFile = rdiffOutputFile + ".zip"
    return rdiffOutputFile
def restoreFileOrDir(repoRoot, dirPath, filename, restoreDate, useZip):
   """ returns a file path to the file.  User is responsible for deleting file, as well as containing dir, after use. """
   filePath = joinPaths(dirPath, filename)
   filePath = rdiffQuotedPath(repoRoot).getQuotedPath(filePath)
   checkRepoPath(repoRoot, filePath)

   restoredFilename = filename
   if restoredFilename == "/":
      restoredFilename = "(root)"

   fileToRestore = joinPaths(repoRoot, dirPath, filename)
   dateString = str(restoreDate.getSeconds())
   rdiffOutputFile = joinPaths(tempfile.mkdtemp(), restoredFilename) # TODO: make so this includes the username
   results = rdw_helpers.execute("rdiff-backup", "--restore-as-of="+dateString, fileToRestore, rdiffOutputFile)
   if results['exitCode'] != 0 or not os.access(rdiffOutputFile, os.F_OK):
      error = results['stderr']
      if not error:
         error = 'rdiff-backup claimed success, but did not restore anything. This indicates a bug in rdiffWeb. Please report this to a developer.'
      raise UnknownError('Unable to restore! rdiff-backup output:\n'+error)
   if os.path.isdir(rdiffOutputFile):
      if useZip:
         rdw_helpers.recursiveZipDir(rdiffOutputFile, rdiffOutputFile+".zip")
         rdw_helpers.removeDir(rdiffOutputFile)
         rdiffOutputFile = rdiffOutputFile+".zip"
      else:
         rdw_helpers.recursiveTarDir(rdiffOutputFile, rdiffOutputFile+".tar.gz")
         rdw_helpers.removeDir(rdiffOutputFile)
         rdiffOutputFile = rdiffOutputFile+".tar.gz"
   return rdiffOutputFile
Beispiel #4
0
def restoreFileOrDir(repoRoot, dirPath, filename, restoreDate, useZip):
    """This function is used to restore a directory tree or a file from the
      given respository. Users may specified the restore date and the
      archive format."""

    # Format the specified file name / repository path for validation
    filePath = joinPaths(dirPath, filename)
    filePath = RdiffQuotedPath(repoRoot).getQuotedPath(filePath)
    checkRepoPath(repoRoot, filePath)

    restoredFilename = filename
    if restoredFilename == "/":
        restoredFilename = "(root)"

    fileToRestore = joinPaths(repoRoot, dirPath, filename)
    dateString = str(restoreDate.getSeconds())
    rdiffOutputFile = joinPaths(
        tempfile.mkdtemp(),
        restoredFilename)  # TODO: make so this includes the username

    # Use rdiff-backup executable to restore the data into a specified location
    results = rdw_helpers.execute("rdiff-backup",
                                  "--restore-as-of=" + dateString,
                                  fileToRestore, rdiffOutputFile)

    # Check the result
    if results['exitCode'] != 0 or not os.access(rdiffOutputFile, os.F_OK):
        error = results['stderr']
        if not error:
            error = 'rdiff-backup claimed success, but did not restore anything. This indicates a bug in rdiffWeb. Please report this to a developer.'
        raise UnknownError('Unable to restore! rdiff-backup output:\n' + error)

    # The path restored is a directory and need to be archived using zip or tar
    if os.path.isdir(rdiffOutputFile):
        rdiffOutputDirectory = rdiffOutputFile
        try:
            if useZip:
                rdiffOutputFile = rdiffOutputFile + ZIP_SUFFIX
                _recursiveZipDir(rdiffOutputDirectory, rdiffOutputFile)
            else:
                rdiffOutputFile = rdiffOutputFile + TARGZ_SUFFIX
                _recursiveTarDir(rdiffOutputDirectory, rdiffOutputFile)
        finally:
            rdw_helpers.removeDir(rdiffOutputDirectory)

    return rdiffOutputFile
Beispiel #5
0
def restoreFileOrDir(repoRoot, dirPath, filename, restoreDate, useZip):
   """This function is used to restore a directory tree or a file from the
      given respository. Users may specified the restore date and the
      archive format."""
   
   # Format the specified file name / repository path for validation
   dirPath = dirPath.encode('utf-8')
   filename = filename.encode('utf-8')
   filePath = joinPaths(dirPath, filename)
   filePath = RdiffQuotedPath(repoRoot).getQuotedPath(filePath)
   checkRepoPath(repoRoot, filePath)

   restoredFilename = filename
   if restoredFilename == "/":
      restoredFilename = "(root)"

   fileToRestore = joinPaths(repoRoot, dirPath, filename)
   dateString = str(restoreDate.getSeconds())
   rdiffOutputFile = joinPaths(tempfile.mkdtemp(), restoredFilename) # TODO: make so this includes the username
   
   # Use rdiff-backup executable to restore the data into a specified location
   results = rdw_helpers.execute("rdiff-backup", "--restore-as-of=" + dateString, fileToRestore, rdiffOutputFile)
   
   # Check the result
   if results['exitCode'] != 0 or not os.access(rdiffOutputFile, os.F_OK):
      error = results['stderr']
      if not error:
         error = 'rdiff-backup claimed success, but did not restore anything. This indicates a bug in rdiffWeb. Please report this to a developer.'
      raise UnknownError('Unable to restore! rdiff-backup output:\n' + error)

   
   # The path restored is a directory and need to be archived using zip or tar
   if os.path.isdir(rdiffOutputFile):
      rdiffOutputDirectory = rdiffOutputFile
      try:
         if useZip:
            rdiffOutputFile = rdiffOutputFile + ZIP_SUFFIX
            _recursiveZipDir(rdiffOutputDirectory, rdiffOutputFile)
         else:
            rdiffOutputFile = rdiffOutputFile + TARGZ_SUFFIX
            _recursiveTarDir(rdiffOutputDirectory, rdiffOutputFile)
      finally:
         rdw_helpers.removeDir(rdiffOutputDirectory)
         
   return rdiffOutputFile
def restoreFileOrDir(repoRoot, dirPath, filename, restoreDate):
   """ returns a file path to the file.  User is responsible for deleting file, as well as containing dir, after use. """
   checkRepoPath(repoRoot, joinPaths(dirPath, filename))

   restoredFilename = filename
   if restoredFilename == "/":
      restoredFilename = "(root)"

   fileToRestore = joinPaths(repoRoot, dirPath, filename)
   dateString = str(restoreDate.getSeconds())
   rdiffOutputFile = joinPaths(tempfile.mkdtemp(), restoredFilename) # TODO: make so this includes the username
   args = [ "rdiff-backup", "--restore-as-of="+dateString, fileToRestore, rdiffOutputFile ]
   os.spawnvp(os.P_WAIT, args[0], args)
   if not os.access(rdiffOutputFile, os.F_OK):
      raise UnknownError()
   if os.path.isdir(rdiffOutputFile):
      rdw_helpers.recursiveZipDir(rdiffOutputFile, rdiffOutputFile+".zip")
      rdw_helpers.removeDir(rdiffOutputFile)
      rdiffOutputFile = rdiffOutputFile+".zip"
   return rdiffOutputFile
 def tearDown(self):
    if (os.access(self.destRoot, os.F_OK)):
          removeDir(self.destRoot)
Beispiel #8
0
 def tearDown(self):
     if (os.access(self.destRoot, os.F_OK)):
         rdw_helpers.removeDir(self.destRoot)
Beispiel #9
0
 def __del__(self):
     rdw_helpers.removeDir(self.dirPath)
Beispiel #10
0
 def cleanRestoredFile(self, filePath):
     (containingFolder, file) = os.path.split(filePath)
     removeDir(containingFolder)
Beispiel #11
0
 def tearDown(self):
     # Remove sandbox dirs
     for sandboxDir in [self.sourceDir, self.destDir, self.restoreDir]:
         if (os.access(sandboxDir, os.F_OK)):
             removeDir(sandboxDir)
 def cleanRestoredFile(self, filePath):
    (containingFolder, file) = os.path.split(filePath)
    removeDir(containingFolder)
 def tearDown(self):
    # Remove sandbox dirs
    for sandboxDir in [self.sourceDir, self.destDir, self.restoreDir]:
       if (os.access(sandboxDir, os.F_OK)):
          removeDir(sandboxDir)
Beispiel #14
0
 def __del__(self):
    rdw_helpers.removeDir(self.dirPath)
Beispiel #15
0
class rdiffLocationsPage(page_main.rdiffPage):
    ''' Shows the locations page. Will show all available destination
   backup directories. This is the root (/) page '''
    def index(self, **kwargs):
        # Handle repository deletion
        if cherrypy.request.method == "POST" and 'repo' in cherrypy.request.params:
            # Delete the repository
            return self._handle_deletion(cherrypy.request.params['repo'])

        return self._generate_page()

    index.exposed = True

    def getParmsForPage(self,
                        root,
                        repos,
                        allowRepoDeletion=False,
                        message='',
                        error=''):
        repoList = []
        for userRepo in repos:
            try:
                repoHistory = librdiff.getLastBackupHistoryEntry(
                    rdw_helpers.joinPaths(root, userRepo))
            except librdiff.FileError:
                repoSize = "0"
                repoDate = "Error"
                repoList.append({
                    "repoName":
                    userRepo,
                    "repoSize":
                    repoSize,
                    "repoDate":
                    repoDate,
                    "repoBrowseUrl":
                    self.buildBrowseUrl(userRepo, "/", False),
                    "repoHistoryUrl":
                    self.buildHistoryUrl(userRepo),
                    'failed':
                    True
                })
            else:
                repoSize = rdw_helpers.formatFileSizeStr(repoHistory.size)
                if repoHistory.inProgress:
                    repoSize = "In Progress"
                repoDate = repoHistory.date.getDisplayString()
                repoList.append({
                    "repoName":
                    userRepo,
                    "repoSize":
                    repoSize,
                    "repoDate":
                    repoDate,
                    "repoBrowseUrl":
                    self.buildBrowseUrl(userRepo, "/", False),
                    "repoHistoryUrl":
                    self.buildHistoryUrl(userRepo),
                    'failed':
                    False
                })

        self._sortLocations(repoList)
        # Make second pass through list, setting the 'altRow' attribute
        for i in range(0, len(repoList)):
            repoList[i]['altRow'] = (i % 2 == 0)
        # Calculate disk usage
        diskUsage = ''
        diskUsageCommand = rdw_config.getConfigSetting('diskUsageCommand')
        if diskUsageCommand:
            diskUsage = subprocess.Popen(
                [
                    diskUsageCommand,
                    self.getUsername(),
                    self.getUserDB().getUserRoot(self.getUsername())
                ],
                stdout=subprocess.PIPE).communicate()[0]
            try:
                diskUsageNum = int(diskUsage)
            except:
                pass
            else:
                diskUsage = rdw_helpers.formatFileSizeStr(diskUsageNum)
        # Allow repository deletion?
        return {
            "title": "browse",
            "repos": repoList,
            "diskUsage": diskUsage,
            "allowRepoDeletion": allowRepoDeletion,
            "message": message,
            "error": error
        }

    def _handle_deletion(self, repo):
        try:
            self.validateUserPath(repo)
        except rdw_helpers.accessDeniedError, error:
            return self._generate_page(error=str(error))
        if not repo in self.getUserDB().getUserRepoPaths(self.getUsername()):
            return self._generate_page(error="Access is denied.")
        if not self.getUserDB().allowRepoDeletion(self.getUsername()):
            return self._generate_page(
                error="Deleting backups is not allowed.")

        fullPath = rdw_helpers.joinPaths(
            self.getUserDB().getUserRoot(self.getUsername()), repo)
        rdw_helpers.removeDir(fullPath)
        repos = self.getUserDB().getUserRepoPaths(self.getUsername())
        repos.remove(repo)
        self.getUserDB().setUserRepos(self.getUsername(), repos)
        return self._generate_page(
            message="The backup location \"%s\" was successfully deleted." %
            repo)