Exemple #1
0
    def backup(self) -> bool:
        config = Config()
        creds = self.get_credentials()

        errors = False
        with utils.environment('RCON_PASSWORD', creds['password']):
            try:
                # turn off auto-save and sync all data to the disk before backing up worlds
                self.prepare_mc_backup()

                for mount in self.filter_mounts():
                    backup_data = self.get_volume_backup_destination(
                        mount, '/minecraft')
                    logger.info('Backing up %s', mount.source)
                    vol_result = restic.backup_files(config.repository,
                                                     source=backup_data,
                                                     tags=self.tags)
                    logger.debug('Minecraft backup exit code: %s', vol_result)
                    if vol_result != 0:
                        logger.error(
                            'Minecraft backup exited with non-zero code: %s',
                            vol_result)
                        errors = True
            except Exception as ex:
                logger.error('Exception raised during minecraft backup')
                logger.exception(ex)
                errors = True

            # always always turn saving back on
            rcon.save_on(creds['host'], creds['port'])

        return errors
    def backup(self):
        config = Config()
        destination = self.backup_destination_path()

        commands.run(["mkdir", "-p", f"{destination}"])
        commands.run(self.dump_command())

        return restic.backup_files(config.repository,
                                   f"{destination}",
                                   tags=self.tags)
def start_backup_process(config, containers):
    """The actual backup process running inside the spawned container"""
    if not utils.is_true(os.environ.get('BACKUP_PROCESS_CONTAINER')):
        logger.error(
            "Cannot run backup process in this container. Use backup command instead. "
            "This will spawn a new container with the necessary mounts.")
        alerts.send(
            subject="Cannot run backup process in this container",
            body=
            ("Cannot run backup process in this container. Use backup command instead. "
             "This will spawn a new container with the necessary mounts."))
        exit(1)

    status(config, containers)
    errors = False

    # Did we actually get any volumes mounted?
    try:
        has_volumes = os.stat('/volumes') is not None
    except FileNotFoundError:
        logger.warning("Found no volumes to back up")
        has_volumes = False

    # Warn if there is nothing to do
    if len(containers.containers_for_backup()) == 0 and not has_volumes:
        logger.error("No containers for backup found")
        exit(1)

    if has_volumes:
        try:
            logger.info('Backing up volumes')
            vol_result = restic.backup_files(config.repository,
                                             source='/volumes')
            logger.debug('Volume backup exit code: %s', vol_result)
            if vol_result != 0:
                logger.error('Volume backup exited with non-zero code: %s',
                             vol_result)
                errors = True
        except Exception as ex:
            logger.error('Exception raised during volume backup')
            logger.exception(ex)
            errors = True

    # back up databases
    logger.info('Backing up databases')
    for container in containers.containers_for_backup():
        if container.database_backup_enabled:
            try:
                instance = container.instance
                logger.info('Backing up %s in service %s',
                            instance.container_type, instance.service_name)
                result = instance.backup()
                logger.debug('Exit code: %s', result)
                if result != 0:
                    logger.error(
                        'Backup command exited with non-zero code: %s', result)
                    errors = True
            except Exception as ex:
                logger.exception(ex)
                errors = True

    if errors:
        logger.error('Exit code: %s', errors)
        exit(1)

    # Only run cleanup if backup was successful
    result = cleanup(config, container)
    logger.debug('cleanup exit code: %s', result)
    if result != 0:
        logger.error('cleanup exit code: %s', result)
        exit(1)

    # Test the repository for errors
    logger.info("Checking the repository for errors")
    result = restic.check(config.repository)
    if result != 0:
        logger.error('Check exit code: %s', result)
        exit(1)

    logger.info('Backup completed')