Beispiel #1
0
    def prune_archives(self, archives, repository):
        """
        Prune list of two tuples (delete, archive), all of which must be in the same `Repository`.

        Return `Statistics`.
        """
        # TODO Maybe commit some stuff here after a while, because this can seriously take some time.
        stats = Statistics()
        with open_repository(repository) as borg_repository:
            manifest, key = Manifest.load(borg_repository)
            with Cache(borg_repository, key, manifest, lock_wait=1) as cache:
                for delete, archive in archives:
                    assert archive.repository == repository
                    if delete:
                        log.info('Deleting archive %s [%s]', archive.name,
                                 archive.id)
                        archive.delete(manifest, stats, cache)
                    else:
                        log.info('Skipping archive %s [%s]', archive.name,
                                 archive.id)
                manifest.write()
                borg_repository.commit()
                cache.commit()
                transaction.commit()
        log.error(stats.summary.format(label='Deleted data:', stats=stats))
        return stats
Beispiel #2
0
def test_manifest_invalid_archive(rrp, monkeypatch):
    monkeypatch.delenv('BORG_PASSPHRASE')
    manifest, key = Manifest.load(rrp)
    manifest.archives['1234 not allowed'] = bytes(32), datetime.now()
    with pytest.raises(ValueError) as exc_info:
        manifest.write()
    assert exc_info.match('illegal archive push')
Beispiel #3
0
 def check_archives(self, repository, check):
     if Manifest.MANIFEST_ID not in check.chunks:
         log.error('Repository manifest not found!')
         check.manifest = check.rebuild_manifest()
     else:
         check.manifest, _ = Manifest.load(repository, key=check.key)
     check.rebuild_refcounts(sort_by='ts')
     check.orphan_chunks_check()
     check.finish()
Beispiel #4
0
 def ensure_cache(self, cache_path):
     if not cache_path.is_dir():
         log.info('No cache found, creating one')
     with open_repository(self.repository) as repository:
         manifest, key = Manifest.load(repository)
         with Cache(repository,
                    key,
                    manifest,
                    path=str(cache_path),
                    lock_wait=1) as cache:
             cache.commit()
         self.check_archive_chunks_cache()
Beispiel #5
0
    def synthesize_crypto(job):
        with open_repository(job.repository) as repository:
            if bin_to_hex(repository.id) != job.repository.repository_id:
                raise RepositoryIDMismatch(bin_to_hex(repository.id),
                                           job.repository.repository_id)
            manifest, key = Manifest.load(repository)
            client_key = synthesize_client_key(key, repository)
            if not isinstance(client_key, PlaintextKey):
                job.client_key_data = client_key.get_key_data()
                job.client_key_type = client_key.synthetic_type

            client_manifest = SyntheticManifest(client_key, repository.id)
            job.client_manifest_data = bin_to_hex(client_manifest.write())
            job.client_manifest_id_str = client_manifest.id_str
            transaction.get().note('Synthesized crypto for job %s' % job.id)
            transaction.commit()
Beispiel #6
0
def test_connection_finalization(rrp, monkeypatch, tmpdir):
    monkeypatch.delenv('BORG_PASSPHRASE')
    monkeypatch.setenv('BORG_CACHE_DIR', tmpdir.join('client-cache'))
    manifest, key = Manifest.load(rrp)
    with Cache(rrp, key, manifest) as cache:
        archive = Archive(rrp,
                          key,
                          manifest,
                          rrp.job.archive_name,
                          cache,
                          create=True)
        archive.write_checkpoint()
        archive.save()
    with pytest.raises(ValueError) as exc_info:
        rrp.get(bytes(32))
    assert exc_info.match('Transaction was doomed. Refusing to continue.')
Beispiel #7
0
    def handle(self, *args, **options):
        try:
            client = data_root().clients[options['client']]
        except KeyError:
            raise CommandError('Client %s not found' % options['client'])

        for repository in data_root().repositories:
            if repository.name == options['repository']:
                break
            if repository.id.startswith(options['repository']):
                break
            if repository.url == options['repository']:
                break
        else:
            raise CommandError('Repository %s not found' %
                               options['repository'])

        with open_repository(repository) as borg_repository:
            manifest, key = Manifest.load(borg_repository)
            with Cache(borg_repository, key, manifest, lock_wait=1) as cache:
                names = self.find_archives(manifest,
                                           options['archive'],
                                           regex=options['regex'])
                imported = 0

                pi = ProgressIndicatorPercent(
                    msg='Importing archives %4.1f %%: %s',
                    total=len(names),
                    step=0.1)
                for name in names:
                    imported += self.import_archive(manifest, cache,
                                                    repository, name, client)
                    pi.show(info=[name])
                pi.finish()

        print('Imported %d archives.' % imported, file=sys.stderr)
Beispiel #8
0
 def _load_repository_key(self):
     self._manifest, self._repository_key = Manifest.load(self.repository)
Beispiel #9
0
def test_manifest_clean(rrp, monkeypatch):
    monkeypatch.delenv('BORG_PASSPHRASE')
    manifest, key = Manifest.load(rrp)
    assert not manifest.archives