Beispiel #1
0
    def create(self, M):
        """Creates a new backup of M"""

        if M.isInstalled() and M.get('dbtype') != 'mysqli':
            raise BackupDBEngineNotSupported(
                'Cannot backup database engine %s' % M.get('dbtype'))

        name = M.get('identifier')
        if name == None:
            raise Exception('Cannot backup instance without identifier!')

        now = int(time.time())
        backup_identifier = self.createIdentifier(name)
        Wp = Workplace()

        # Copy whole directory, shutil will create topath
        topath = os.path.join(self.path, backup_identifier)
        path = Wp.getPath(name)
        debug('Copying instance directory')
        copy_tree(path, topath, preserve_symlinks=1)

        # Dump the whole database
        if M.isInstalled():
            debug('Dumping database')
            dumpto = os.path.join(topath, sqlfile)
            fd = open(dumpto, 'w')
            M.dbo().selectdb(M.get('dbname'))
            M.dbo().dump(fd)
        else:
            debug('Instance not installed. Do not dump database.')

        # Create a JSON file containing all known information
        debug('Saving instance information')
        jsonto = os.path.join(topath, jason)
        info = M.info()
        info['backup_origin'] = path
        info['backup_identifier'] = backup_identifier
        info['backup_time'] = now
        json.dump(info, open(jsonto, 'w'), sort_keys=True, indent=4)

        return True
Beispiel #2
0
    def create(self, M):
        """Creates a new backup of M"""

        if M.isInstalled() and M.get("dbtype") != "mysqli":
            raise BackupDBEngineNotSupported("Cannot backup database engine %s" % M.get("dbtype"))

        name = M.get("identifier")
        if name == None:
            raise Exception("Cannot backup instance without identifier!")

        now = int(time.time())
        backup_identifier = self.createIdentifier(name)
        Wp = Workplace()

        # Copy whole directory, shutil will create topath
        topath = os.path.join(self.path, backup_identifier)
        path = Wp.getPath(name)
        logging.info("Copying instance directory")
        copy_tree(path, topath, preserve_symlinks=1)

        # Dump the whole database
        if M.isInstalled():
            logging.info("Dumping database")
            dumpto = os.path.join(topath, sqlfile)
            fd = open(dumpto, "w")
            M.dbo().selectdb(M.get("dbname"))
            M.dbo().dump(fd)
        else:
            logging.info("Instance not installed. Do not dump database.")

        # Create a JSON file containing all known information
        logging.info("Saving instance information")
        jsonto = os.path.join(topath, jason)
        info = M.info()
        info["backup_origin"] = path
        info["backup_identifier"] = backup_identifier
        info["backup_time"] = now
        json.dump(info, open(jsonto, "w"), sort_keys=True, indent=4)

        return True
Beispiel #3
0
    def create(self, M):
        """Creates a new backup of M"""

        if M.isInstalled() and M.get('dbtype') != 'mysqli':
            raise BackupDBEngineNotSupported('Cannot backup database engine %s' % M.get('dbtype'))

        name = M.get('identifier')
        if name == None:
            raise Exception('Cannot backup instance without identifier!')

        now = int(time.time())
        backup_identifier = self.createIdentifier(name)
        Wp = Workplace()

        # Copy whole directory, shutil will create topath
        topath = os.path.join(self.path, backup_identifier)
        path = Wp.getPath(name)
        debug('Copying instance directory')
        copy_tree(path, topath, preserve_symlinks = 1)

        # Dump the whole database
        if M.isInstalled():
            debug('Dumping database')
            dumpto = os.path.join(topath, sqlfile)
            fd = open(dumpto, 'w')
            M.dbo().selectdb(M.get('dbname'))
            M.dbo().dump(fd)
        else:
            debug('Instance not installed. Do not dump database.')

        # Create a JSON file containing all known information
        debug('Saving instance information')
        jsonto = os.path.join(topath, jason)
        info = M.info()
        info['backup_origin'] = path
        info['backup_identifier'] = backup_identifier
        info['backup_time'] = now
        json.dump(info, open(jsonto, 'w'), sort_keys = True, indent = 4)

        return True
Beispiel #4
0
    def restore(self, destination=None):
        """Restores the backup"""

        identifier = self.get("identifier")
        if not identifier:
            raise Exception("Identifier is invalid! Cannot proceed.")

        Wp = Workplace()
        if destination == None:
            destination = self.get("backup_origin")
        if not destination:
            raise Exception("Wrong path to perform the restore!")

        if os.path.isdir(destination):
            raise BackupDirectoryExistsException("Destination directory already exists!")

        # Restoring database
        if self.get("installed") and os.path.isfile(self.sqlfile):
            dbname = self.get("dbname")
            dbo = DB(self.get("dbtype"), C.get("db.%s" % self.get("dbtype")))
            if dbo.dbexists(dbname):
                raise BackupDBExistsException("Database already exists!")

        # Copy tree to destination
        try:
            logging.info("Restoring instance directory")
            copy_tree(self.path, destination, preserve_symlinks=1)
            M = Wp.get(identifier)
            chmodRecursive(Wp.getPath(identifier, "data"), 0777)
        except Exception as e:
            raise Exception("Error while restoring directory\n%s\nto %s. Exception: %s" % (self.path, destination, e))

        # Restoring database
        if self.get("installed") and os.path.isfile(self.sqlfile):
            logging.info("Restoring database")
            content = ""
            f = open(self.sqlfile, "r")
            for l in f:
                content += l
            queries = content.split(";\n")
            content = None
            logging.info("%d queries to execute" % (len(queries)))

            dbo.createdb(dbname)
            dbo.selectdb(dbname)
            done = 0
            for query in queries:
                if len(query.strip()) == 0:
                    continue
                try:
                    dbo.execute(query)
                except:
                    logging.error("Query failed! You will have to fix this mually. %s", query)
                done += 1
                if done % 500 == 0:
                    logging.debug("%d queries done" % done)
            logging.info("%d queries done" % done)
            dbo.close()

        # Restoring symbolic link
        linkDir = os.path.join(Wp.www, identifier)
        wwwDir = Wp.getPath(identifier, "www")
        if os.path.islink(linkDir):
            os.remove(linkDir)
        if os.path.isfile(linkDir) or os.path.isdir(linkDir):  # No elif!
            logging.warning("Could not create symbolic link. Please manually create: ln -s %s %s" % (wwwDir, linkDir))
        else:
            os.symlink(wwwDir, linkDir)

        return M
Beispiel #5
0
    def restore(self, destination=None):
        """Restores the backup"""

        identifier = self.get('identifier')
        if not identifier:
            raise Exception('Identifier is invalid! Cannot proceed.')

        Wp = Workplace()
        if destination == None:
            destination = self.get('backup_origin')
        if not destination:
            raise Exception('Wrong path to perform the restore!')

        if os.path.isdir(destination):
            raise BackupDirectoryExistsException(
                'Destination directory already exists!')

        # Restoring database
        if self.get('installed') and os.path.isfile(self.sqlfile):
            dbname = self.get('dbname')
            dbo = DB(self.get('dbtype'), C.get('db.%s' % self.get('dbtype')))
            if dbo.dbexists(dbname):
                raise BackupDBExistsException('Database already exists!')

        # Copy tree to destination
        try:
            debug('Restoring instance directory')
            copy_tree(self.path, destination, preserve_symlinks=1)
            M = Wp.get(identifier)
            chmodRecursive(Wp.getPath(identifier, 'data'), 0777)
        except Exception as e:
            raise Exception(
                'Error while restoring directory\n%s\nto %s. Exception: %s' %
                (self.path, destination, e))

        # Restoring database
        if self.get('installed') and os.path.isfile(self.sqlfile):
            debug('Restoring database')
            content = ''
            f = open(self.sqlfile, 'r')
            for l in f:
                content += l
            queries = content.split(';\n')
            content = None
            debug("%d queries to execute" % (len(queries)))

            dbo.createdb(dbname)
            dbo.selectdb(dbname)
            done = 0
            for query in queries:
                if len(query.strip()) == 0: continue
                try:
                    dbo.execute(query)
                except:
                    debug('Query failed! You will have to fix this mually.')
                    debug(query)
                done += 1
                if done % 500 == 0:
                    debug("%d queries done" % done)
            debug('%d queries done' % done)
            dbo.close()

        # Restoring symbolic link
        linkDir = os.path.join(Wp.www, identifier)
        wwwDir = Wp.getPath(identifier, 'www')
        if os.path.islink(linkDir):
            os.remove(linkDir)
        if os.path.isfile(linkDir) or os.path.isdir(linkDir):  # No elif!
            debug('Could not create symbolic link')
            debug('Please manually create: ln -s %s %s' % (wwwDir, linkDir))
        else:
            os.symlink(wwwDir, linkDir)

        return M
Beispiel #6
0
    def restore(self, destination = None):
        """Restores the backup"""

        identifier = self.get('identifier')
        if not identifier:
            raise Exception('Identifier is invalid! Cannot proceed.')

        Wp = Workplace()
        if destination == None:
            destination = self.get('backup_origin')
        if not destination:
            raise Exception('Wrong path to perform the restore!')

        if os.path.isdir(destination):
            raise BackupDirectoryExistsException('Destination directory already exists!')

        # Restoring database
        if self.get('installed') and os.path.isfile(self.sqlfile):
            dbname = self.get('dbname')
            dbo = DB(self.get('dbtype'), C.get('db.%s' % self.get('dbtype')))
            if dbo.dbexists(dbname):
                raise BackupDBExistsException('Database already exists!')

        # Copy tree to destination
        try:
            debug('Restoring instance directory')
            copy_tree(self.path, destination, preserve_symlinks = 1)
            M = Wp.get(identifier)
            chmodRecursive(Wp.getPath(identifier, 'data'), 0777)
        except Exception as e:
            raise Exception('Error while restoring directory\n%s\nto %s. Exception: %s' % (self.path, destination, e))

        # Restoring database
        if self.get('installed') and os.path.isfile(self.sqlfile):
            debug('Restoring database')
            content = ''
            f = open(self.sqlfile, 'r')
            for l in f:
                content += l
            queries = content.split(';\n')
            content = None
            debug("%d queries to execute" % (len(queries)))

            dbo.createdb(dbname)
            dbo.selectdb(dbname)
            done = 0
            for query in queries:
                if len(query.strip()) == 0: continue
                try:
                    dbo.execute(query)
                except:
                    debug('Query failed! You will have to fix this mually.')
                    debug(query)
                done += 1
                if done % 500 == 0:
                    debug("%d queries done" % done)
            debug('%d queries done' % done)
            dbo.close()

        # Restoring symbolic link
        linkDir = os.path.join(Wp.www, identifier)
        wwwDir = Wp.getPath(identifier, 'www')
        if os.path.islink(linkDir):
            os.remove(linkDir)
        if os.path.isfile(linkDir) or os.path.isdir(linkDir): # No elif!
            debug('Could not create symbolic link')
            debug('Please manually create: ln -s %s %s' % (wwwDir, linkDir))
        else:
            os.symlink(wwwDir, linkDir)

        return M