Exemplo n.º 1
0
 def __init__(self, path, backupset, name):
     self.path = path
     self.backupset = backupset
     self.name = '/'.join((backupset, name))
     # Initialize an empty config
     # This will not be loaded until load_config is called
     config_path = os.path.join(self.path, 'backup.conf')
     self.config = BaseConfig({}, file_error=False)
     self.config.filename = config_path
     if os.path.exists(config_path):
         self.load_config()
     else:
         self.validate_config()
Exemplo n.º 2
0
 def __init__(self, path, backupset, name):
     self.path = path
     self.backupset = backupset
     self.name = '/'.join((backupset, name))
     # Initialize an empty config
     # This will not be loaded until load_config is called
     config_path = os.path.join(self.path, 'backup.conf')
     self.config = BaseConfig({}, file_error=False)
     self.config.filename = config_path
     if os.path.exists(config_path):
         self.load_config()
     else:
         self.validate_config()
Exemplo n.º 3
0
class Backup(object):
    """
    Representation of a backup instance.
    """
    def __init__(self, path, backupset, name):
        self.path = path
        self.backupset = backupset
        self.name = '/'.join((backupset, name))
        # Initialize an empty config
        # This will not be loaded until load_config is called
        config_path = os.path.join(self.path, 'backup.conf')
        self.config = BaseConfig({}, file_error=False)
        self.config.filename = config_path
        if os.path.exists(config_path):
            self.load_config()
        else:
            self.validate_config()

    def validate_config(self):
        self.config.validate_config(CONFIGSPEC, suppress_warnings=True)

    def load_config(self):
        """
        (Re)Load the config for this backup.
        """
        self.config.reload()
        self.validate_config()

    def purge(self, data_only=False):
        """
        Purge this backup.
        """
        assert(os.path.realpath(self.path) != '/')
        # purge the entire backup directory
        try:
            shutil.rmtree(self.path)
        except OSError, exc:
            if exc.errno != errno.ENOENT:
                raise
Exemplo n.º 4
0
class Backup(object):
    """
    Representation of a backup instance.
    """
    def __init__(self, path, backupset, name):
        self.path = path
        self.backupset = backupset
        self.name = '/'.join((backupset, name))
        # Initialize an empty config
        # This will not be loaded until load_config is called
        config_path = os.path.join(self.path, 'backup.conf')
        self.config = BaseConfig({}, file_error=False)
        self.config.filename = config_path
        if os.path.exists(config_path):
            self.load_config()
        else:
            self.validate_config()

    def validate_config(self):
        self.config.validate_config(CONFIGSPEC, suppress_warnings=True)

    def load_config(self):
        """
        (Re)Load the config for this backup.
        """
        self.config.reload()
        self.validate_config()

    def purge(self, data_only=False):
        """
        Purge this backup.
        """
        assert (os.path.realpath(self.path) != '/')
        # purge the entire backup directory
        try:
            shutil.rmtree(self.path)
        except OSError, exc:
            if exc.errno != errno.ENOENT:
                raise
Exemplo n.º 5
0
class Backup(object):
    """
    Representation of a backup instance.
    """
    def __init__(self, path, backupset, name):
        self.path = path
        self.backupset = backupset
        self.name = '/'.join((backupset, name))
        # Initialize an empty config
        # This will not be loaded until load_config is called
        config_path = os.path.join(self.path, 'backup.conf')
        self.config = BaseConfig({}, file_error=False)
        self.config.filename = config_path
        if os.path.exists(config_path):
            self.load_config()
        else:
            self.validate_config()

    def validate_config(self):
        """
        Validate configuration
        """
        self.config.validate_config(CONFIGSPEC, suppress_warnings=True)

    def load_config(self):
        """
        (Re)Load the config for this backup.
        """
        self.config.reload()
        self.validate_config()

    def purge(self):
        """
        Purge the entire backup directory
        """
        assert os.path.realpath(self.path) != '/'
        try:
            shutil.rmtree(self.path)
        except OSError as exc:
            if exc.errno != errno.ENOENT:
                raise

    def exists(self):
        """
        Check if this backup exists on disk
        """
        return os.path.exists(self.path)

    def prepare(self):
        """
        Prepare this backup on disk.  Ensures the path to this backup is created,
        but does not flush any other backup metadata.
        """
        os.makedirs(self.path)
        LOGGER.info("Creating backup path %s", self.path)

    def flush(self):
        """
        Flush this backup to disk.  Ensure the path to this backup is created
        and write the backup.conf to the backup directory.
        """
        LOGGER.debug("Writing out config to %s", self.config.filename)
        self.config.write()

    def _formatted_config(self):
        from holland.core.util.fmt import format_bytes, format_datetime
        cfg = dict(self.config['holland:backup'])
        cfg['stop-time'] = format_datetime(cfg['stop-time'])
        cfg['start-time'] = format_datetime(cfg['start-time'])
        cfg['estimated-size'] = format_bytes(cfg['estimated-size'])
        cfg['on-disk-size'] = format_bytes(cfg['on-disk-size'])
        return cfg

    def info(self):
        """
        get plugin info
        """
        from string import Template
        from textwrap import dedent
        tmpl = Template("""
        backup-plugin   = ${plugin}
        backup-started  = ${start-time}
        backup-finished = ${stop-time}
        estimated size  = ${estimated-size}
        on-disk size    = ${on-disk-size}
        """)
        info_str = tmpl.safe_substitute(self._formatted_config())
        info_str = "\t" + dedent(str).lstrip()
        info_str = "\n\t\t".join(str.splitlines())
        return info_str

    def __str__(self):
        """
        format plugin info
        """
        from textwrap import dedent
        from holland.core.util.fmt import format_bytes, format_datetime

        return dedent("""
        Backup: %s
        start-time:     %s
        stop-time:      %s
        estimated-size: %s
        on-disk-size:   %s
        """).strip() % (
            self.name,
            format_datetime(self.config.lookup('holland:backup.start-time')),
            format_datetime(self.config.lookup('holland:backup.stop-time')),
            format_bytes(self.config.lookup('holland:backup.estimated-size')),
            format_bytes(self.config.lookup('holland:backup.on-disk-size'))
        )

    def __cmp__(self, other):
        _cmp = lambda x, y: (x > y) - (x < y)
        return _cmp(self.config['holland:backup']['start-time'],
                    other.config['holland:backup']['start-time'])

    __repr__ = __str__
Exemplo n.º 6
0
class Backup(object):
    """
    Representation of a backup instance.
    """

    def __init__(self, path, backupset, name):
        self.path = path
        self.backupset = backupset
        self.name = "/".join((backupset, name))
        # Initialize an empty config
        # This will not be loaded until load_config is called
        config_path = os.path.join(self.path, "backup.conf")
        self.config = BaseConfig({}, file_error=False)
        self.config.filename = config_path
        if os.path.exists(config_path):
            self.load_config()
        else:
            self.validate_config()

    def validate_config(self):
        self.config.validate_config(CONFIGSPEC)

    def load_config(self):
        """
        (Re)Load the config for this backup.
        """
        self.config.reload()
        self.validate_config()

    def purge(self, data_only=False):
        """
        Purge this backup.
        """
        assert os.path.realpath(self.path) != "/"
        if data_only:
            # only purge data - preserve metadata
            # FIXME: this would be a more sensical scheme
            shutil.rmtree(os.path.join(self.path, "data"))
        else:
            # purge the entire backup directory
            shutil.rmtree(self.path)

    def exists(self):
        """
        Check if this backup exists on disk
        """
        return os.path.exists(self.path)

    def prepare(self):
        """
        Prepare this backup on disk.  Ensures the path to this backup is created,
        but does not flush any other backup metadata.
        """
        if not self.exists():
            os.makedirs(self.path)

    def flush(self):
        """
        Flush this backup to disk.  Ensure the path to this backup is created
        and write the backup.conf to the backup directory.
        """
        self.prepare()
        LOGGER.debug("Writing out config to %s", self.config.filename)
        self.config.write()

    def _formatted_config(self):
        from holland.core.util.fmt import format_bytes, format_datetime

        cfg = dict(self.config["holland:backup"])
        cfg["stop-time"] = format_datetime(cfg["stop-time"])
        cfg["start-time"] = format_datetime(cfg["start-time"])
        cfg["estimated-size"] = format_bytes(cfg["estimated-size"])
        cfg["on-disk-size"] = format_bytes(cfg["on-disk-size"])
        return cfg

    def info(self):
        from holland.core.util.template import Template
        from textwrap import dedent, wrap

        tmpl = Template(
            """
        backup-plugin   = ${plugin}
        backup-started  = ${start-time}
        backup-finished = ${stop-time}
        estimated size  = ${estimated-size}
        on-disk size    = ${on-disk-size}
        """
        )
        str = tmpl.safe_substitute(self._formatted_config())
        str = "\t" + dedent(str).lstrip()
        str = "\n\t\t".join(str.splitlines())
        return str

    def __str__(self):
        from textwrap import dedent
        from holland.core.util.fmt import format_bytes, format_datetime

        return (
            dedent(
                """
        Backup: %s
        start-time:     %s
        stop-time:      %s
        estimated-size: %s
        on-disk-size:   %s
        """
            ).strip()
            % (
                self.name,
                format_datetime(self.config.lookup("holland:backup.start-time")),
                format_datetime(self.config.lookup("holland:backup.stop-time")),
                format_bytes(self.config.lookup("holland:backup.estimated-size")),
                format_bytes(self.config.lookup("holland:backup.on-disk-size")),
            )
        )

    def __cmp__(self, other):
        return self.config["holland:backup"]["start-time"] - other.config["holland:backup"]["start-time"]
Exemplo n.º 7
0
class Backup(object):
    """
    Representation of a backup instance.
    """

    def __init__(self, path, backupset, name):
        self.path = path
        self.backupset = backupset
        self.name = "/".join((backupset, name))
        # Initialize an empty config
        # This will not be loaded until load_config is called
        config_path = os.path.join(self.path, "backup.conf")
        self.config = BaseConfig({}, file_error=False)
        self.config.filename = config_path
        if os.path.exists(config_path):
            self.load_config()
        else:
            self.validate_config()

    def validate_config(self):
        """
        Validate configuration
        """
        self.config.validate_config(CONFIGSPEC, suppress_warnings=True)

    def load_config(self):
        """
        (Re)Load the config for this backup.
        """
        self.config.reload()
        self.validate_config()

    def purge(self):
        """
        Purge the entire backup directory
        """
        assert os.path.realpath(self.path) != "/"
        try:
            shutil.rmtree(self.path)
        except OSError as exc:
            if exc.errno != errno.ENOENT:
                raise

    def exists(self):
        """
        Check if this backup exists on disk
        """
        return os.path.exists(self.path)

    def prepare(self):
        """
        Prepare this backup on disk.  Ensures the path to this backup is created,
        but does not flush any other backup metadata.
        """
        os.makedirs(self.path)
        LOG.info("Creating backup path %s", self.path)

    def flush(self):
        """
        Flush this backup to disk.  Ensure the path to this backup is created
        and write the backup.conf to the backup directory.
        """
        LOG.debug("Writing out config to %s", self.config.filename)
        self.config.write()

    def _formatted_config(self):
        from holland.core.util.fmt import format_bytes, format_datetime

        cfg = dict(self.config["holland:backup"])
        cfg["stop-time"] = format_datetime(cfg["stop-time"])
        cfg["start-time"] = format_datetime(cfg["start-time"])
        cfg["estimated-size"] = format_bytes(cfg["estimated-size"])
        cfg["on-disk-size"] = format_bytes(cfg["on-disk-size"])
        return cfg

    def info(self):
        """
        get plugin info
        """
        from string import Template
        from textwrap import dedent

        tmpl = Template(
            """
        backup-plugin   = ${plugin}
        backup-started  = ${start-time}
        backup-finished = ${stop-time}
        estimated size  = ${estimated-size}
        on-disk size    = ${on-disk-size}
        """
        )
        info_str = tmpl.safe_substitute(self._formatted_config())
        info_str = "\t" + dedent(str).lstrip()
        info_str = "\n\t\t".join(str.splitlines())
        return info_str

    def __str__(self):
        """
        format plugin info
        """
        from textwrap import dedent
        from holland.core.util.fmt import format_bytes, format_datetime

        return (
            dedent(
                """
        Backup: %s
        start-time:     %s
        stop-time:      %s
        estimated-size: %s
        on-disk-size:   %s
        """
            ).strip()
            % (
                self.name,
                format_datetime(self.config.lookup("holland:backup.start-time")),
                format_datetime(self.config.lookup("holland:backup.stop-time")),
                format_bytes(self.config.lookup("holland:backup.estimated-size")),
                format_bytes(self.config.lookup("holland:backup.on-disk-size")),
            )
        )

    def __cmp__(self, other):
        _cmp = lambda x, y: (x > y) - (x < y)
        return _cmp(
            self.config["holland:backup"]["start-time"],
            other.config["holland:backup"]["start-time"],
        )

    __repr__ = __str__