Beispiel #1
0
func runInit(gopts GlobalOptions, args []string) error {
	if gopts.Repo == "" {
		return errors.Fatal("Please specify repository location (-r)")
	}

	be, err := create(gopts.Repo)
	if err != nil {
		return errors.Fatalf("create backend at %s failed: %v\n", gopts.Repo, err)
	}

	if gopts.password == "" {
		gopts.password, err = ReadPasswordTwice(gopts,
			"enter password for new backend: ",
			"enter password again: ")
		if err != nil {
			return err
		}
	}

	s := repository.New(be)

	err = s.Init(gopts.password)
	if err != nil {
		return errors.Fatalf("create key in backend at %s failed: %v\n", gopts.Repo, err)
	}

	Verbosef("created restic backend %v at %s\n", s.Config().ID[:10], gopts.Repo)
	Verbosef("\n")
	Verbosef("Please note that knowledge of your password is required to access\n")
	Verbosef("the repository. Losing your password means that your data is\n")
	Verbosef("irrecoverably lost.\n")

	return nil
}
Beispiel #2
0
// OpenRepository reads the password and opens the repository.
func OpenRepository(opts GlobalOptions) (*repository.Repository, error) {
	if opts.Repo == "" {
		return nil, errors.Fatal("Please specify repository location (-r)")
	}

	be, err := open(opts.Repo)
	if err != nil {
		return nil, err
	}

	s := repository.New(be)

	if opts.password == "" {
		opts.password, err = ReadPassword(opts, "enter password for repository: ")
		if err != nil {
			return nil, err
		}
	}

	err = s.SearchKey(opts.password, maxKeys)
	if err != nil {
		return nil, errors.Fatalf("unable to open repo: %v", err)
	}

	return s, nil
}
Beispiel #3
0
func (cmd CmdInit) Execute(args []string) error {
	if cmd.global.Repo == "" {
		return errors.New("Please specify repository location (-r)")
	}

	be, err := create(cmd.global.Repo)
	if err != nil {
		cmd.global.Exitf(1, "creating backend at %s failed: %v\n", cmd.global.Repo, err)
	}

	if cmd.global.password == "" {
		cmd.global.password = cmd.global.ReadPasswordTwice(
			"enter password for new backend: ",
			"enter password again: ")
	}

	s := repository.New(be)

	err = s.Init(cmd.global.password)
	if err != nil {
		cmd.global.Exitf(1, "creating key in backend at %s failed: %v\n", cmd.global.Repo, err)
	}

	cmd.global.Verbosef("created restic backend %v at %s\n", s.Config.ID[:10], cmd.global.Repo)
	cmd.global.Verbosef("\n")
	cmd.global.Verbosef("Please note that knowledge of your password is required to access\n")
	cmd.global.Verbosef("the repository. Losing your password means that your data is\n")
	cmd.global.Verbosef("irrecoverably lost.\n")

	return nil
}
Beispiel #4
0
func TestCheckerModifiedData(t *testing.T) {
	be := mem.New()

	repository.TestUseLowSecurityKDFParameters(t)

	repo := repository.New(be)
	test.OK(t, repo.Init(test.TestPassword))

	arch := archiver.New(repo)
	_, id, err := arch.Snapshot(nil, []string{"."}, nil, nil)
	test.OK(t, err)
	t.Logf("archived as %v", id.Str())

	beError := &errorBackend{Backend: be}
	checkRepo := repository.New(beError)
	test.OK(t, checkRepo.SearchKey(test.TestPassword, 5))

	chkr := checker.New(checkRepo)

	hints, errs := chkr.LoadIndex()
	if len(errs) > 0 {
		t.Fatalf("expected no errors, got %v: %v", len(errs), errs)
	}

	if len(hints) > 0 {
		t.Errorf("expected no hints, got %v: %v", len(hints), hints)
	}

	beError.ProduceErrors = true
	errFound := false
	for _, err := range checkPacks(chkr) {
		t.Logf("pack error: %v", err)
	}

	for _, err := range checkStruct(chkr) {
		t.Logf("struct error: %v", err)
	}

	for _, err := range checkData(chkr) {
		t.Logf("struct error: %v", err)
		errFound = true
	}

	if !errFound {
		t.Fatal("no error found, checker is broken")
	}
}
Beispiel #5
0
// OpenLocalRepo opens the local repository located at dir.
func OpenLocalRepo(t testing.TB, dir string) *repository.Repository {
	be, err := local.Open(dir)
	OK(t, err)

	repo := repository.New(be)
	err = repo.SearchKey(TestPassword)
	OK(t, err)

	return repo
}
Beispiel #6
0
func SetupRepo() *repository.Repository {
	tempdir, err := ioutil.TempDir(TestTempDir, "restic-test-")
	if err != nil {
		panic(err)
	}

	// create repository below temp dir
	b, err := local.Create(filepath.Join(tempdir, "repo"))
	if err != nil {
		panic(err)
	}

	repo := repository.New(b)
	err = repo.Init(TestPassword)
	if err != nil {
		panic(err)
	}

	return repo
}
Beispiel #7
0
// OpenRepository reads the password and opens the repository.
func (o GlobalOptions) OpenRepository() (*repository.Repository, error) {
	if o.Repo == "" {
		return nil, errors.New("Please specify repository location (-r)")
	}

	be, err := open(o.Repo)
	if err != nil {
		return nil, err
	}

	s := repository.New(be)

	if o.password == "" {
		o.password = o.ReadPassword("enter password for repository: ")
	}

	err = s.SearchKey(o.password)
	if err != nil {
		return nil, fmt.Errorf("unable to open repo: %v", err)
	}

	return s, nil
}
func testArchiverDuplication(t *testing.T) {
	_, err := io.ReadFull(rand.Reader, DupID[:])
	if err != nil {
		t.Fatal(err)
	}

	repo := repository.New(forgetfulBackend())

	err = repo.Init("foo")
	if err != nil {
		t.Fatal(err)
	}

	arch := restic.NewArchiver(repo)

	wg := &sync.WaitGroup{}
	done := make(chan struct{})
	for i := 0; i < parallelSaves; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			for {
				select {
				case <-done:
					return
				default:
				}

				id := randomID()

				if repo.Index().Has(id) {
					continue
				}

				buf := make([]byte, 50)

				err := arch.Save(pack.Data, buf, id)
				if err != nil {
					t.Fatal(err)
				}
			}
		}()
	}

	saveIndex := func() {
		defer wg.Done()

		ticker := time.NewTicker(testSaveIndexTime)
		defer ticker.Stop()

		for {
			select {
			case <-done:
				return
			case <-ticker.C:
				err := repo.SaveFullIndex()
				if err != nil {
					t.Fatal(err)
				}
			}
		}
	}

	wg.Add(1)
	go saveIndex()

	<-time.After(testTimeout)
	close(done)

	wg.Wait()
}