mirror of
https://github.com/kopia/kopia.git
synced 2026-01-25 06:48:48 -05:00
- `repo.Repository` is now read-only and only has methods that can be supported over kopia server
- `repo.RepositoryWriter` has read-write methods that can be supported over kopia server
- `repo.DirectRepository` is read-only and contains all methods of `repo.Repository` plus some low-level methods for data inspection
- `repo.DirectRepositoryWriter` contains write methods for `repo.DirectRepository`
- `repo.Reader` removed and merged with `repo.Repository`
- `repo.Writer` became `repo.RepositoryWriter`
- `*repo.DirectRepository` struct became `repo.DirectRepository`
interface
Getting `{Direct}RepositoryWriter` requires using `NewWriter()` or `NewDirectWriter()` on a read-only repository and multiple simultaneous writers are supported at the same time, each writing to their own indexes and pack blobs.
`repo.Open` returns `repo.Repository` (which is also `repo.RepositoryWriter`).
* content: removed implicit flush on content manager close
* repo: added tests for WriteSession() and implicit flush behavior
* invalidate manifest manager after write session
* cli: disable maintenance in 'kopia server start'
Server will close the repository before completing.
* repo: unconditionally close RepositoryWriter in {Direct,}WriteSession
* repo: added panic in case somebody tries to create RepositoryWriter after closing repository
- used atomic to manage SharedManager.closed
* removed stale example
* linter: fixed spurious failures
Co-authored-by: Julio López <julio+gh@kasten.io>
119 lines
3.8 KiB
Go
119 lines
3.8 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/kopia/kopia/repo"
|
|
"github.com/kopia/kopia/repo/blob"
|
|
"github.com/kopia/kopia/repo/content"
|
|
"github.com/kopia/kopia/repo/encryption"
|
|
"github.com/kopia/kopia/repo/hashing"
|
|
"github.com/kopia/kopia/repo/object"
|
|
"github.com/kopia/kopia/repo/splitter"
|
|
"github.com/kopia/kopia/snapshot/policy"
|
|
)
|
|
|
|
var (
|
|
createCommand = repositoryCommands.Command("create", "Create new repository in a specified location.")
|
|
|
|
createBlockHashFormat = createCommand.Flag("block-hash", "Content hash algorithm.").PlaceHolder("ALGO").Default(hashing.DefaultAlgorithm).Enum(hashing.SupportedAlgorithms()...)
|
|
createBlockEncryptionFormat = createCommand.Flag("encryption", "Content encryption algorithm.").PlaceHolder("ALGO").Default(encryption.DefaultAlgorithm).Enum(encryption.SupportedAlgorithms(false)...)
|
|
createSplitter = createCommand.Flag("object-splitter", "The splitter to use for new objects in the repository").Default(splitter.DefaultAlgorithm).Enum(splitter.SupportedAlgorithms()...)
|
|
|
|
createOnly = createCommand.Flag("create-only", "Create repository, but don't connect to it.").Short('c').Bool()
|
|
)
|
|
|
|
func init() {
|
|
setupConnectOptions(createCommand)
|
|
}
|
|
|
|
func newRepositoryOptionsFromFlags() *repo.NewRepositoryOptions {
|
|
return &repo.NewRepositoryOptions{
|
|
BlockFormat: content.FormattingOptions{
|
|
Hash: *createBlockHashFormat,
|
|
Encryption: *createBlockEncryptionFormat,
|
|
},
|
|
|
|
ObjectFormat: object.Format{
|
|
Splitter: *createSplitter,
|
|
},
|
|
}
|
|
}
|
|
|
|
func ensureEmpty(ctx context.Context, s blob.Storage) error {
|
|
hasDataError := errors.Errorf("has data")
|
|
|
|
err := s.ListBlobs(ctx, "", func(cb blob.Metadata) error {
|
|
// nolint:wrapcheck
|
|
return hasDataError
|
|
})
|
|
|
|
if errors.Is(err, hasDataError) {
|
|
return errors.New("found existing data in storage location")
|
|
}
|
|
|
|
return errors.Wrap(err, "error listing blobs")
|
|
}
|
|
|
|
func runCreateCommandWithStorage(ctx context.Context, st blob.Storage) error {
|
|
err := ensureEmpty(ctx, st)
|
|
if err != nil {
|
|
return errors.Wrap(err, "unable to get repository storage")
|
|
}
|
|
|
|
options := newRepositoryOptionsFromFlags()
|
|
|
|
password, err := getPasswordFromFlags(ctx, true, false)
|
|
if err != nil {
|
|
return errors.Wrap(err, "getting password")
|
|
}
|
|
|
|
log(ctx).Infof("Initializing repository with:")
|
|
log(ctx).Infof(" block hash: %v", options.BlockFormat.Hash)
|
|
log(ctx).Infof(" encryption: %v", options.BlockFormat.Encryption)
|
|
log(ctx).Infof(" splitter: %v", options.ObjectFormat.Splitter)
|
|
|
|
if err := repo.Initialize(ctx, st, options, password); err != nil {
|
|
return errors.Wrap(err, "cannot initialize repository")
|
|
}
|
|
|
|
if *createOnly {
|
|
return nil
|
|
}
|
|
|
|
if err := runConnectCommandWithStorageAndPassword(ctx, st, password); err != nil {
|
|
return errors.Wrap(err, "unable to connect to repository")
|
|
}
|
|
|
|
return populateRepository(ctx, password)
|
|
}
|
|
|
|
func populateRepository(ctx context.Context, password string) error {
|
|
rep, err := repo.Open(ctx, repositoryConfigFileName(), password, applyOptionsFromFlags(ctx, nil))
|
|
if err != nil {
|
|
return errors.Wrap(err, "unable to open repository")
|
|
}
|
|
defer rep.Close(ctx) //nolint:errcheck
|
|
|
|
return repo.WriteSession(ctx, rep, repo.WriteSessionOptions{
|
|
Purpose: "populateRepository",
|
|
}, func(w repo.RepositoryWriter) error {
|
|
if err := policy.SetPolicy(ctx, w, policy.GlobalPolicySourceInfo, policy.DefaultPolicy); err != nil {
|
|
return errors.Wrap(err, "unable to set global policy")
|
|
}
|
|
|
|
printPolicy(policy.DefaultPolicy, nil)
|
|
printStdout("\n")
|
|
printStdout("To change the policy use:\n kopia policy set --global <options>\n")
|
|
printStdout("or\n kopia policy set <dir> <options>\n")
|
|
|
|
if err := setDefaultMaintenanceParameters(ctx, w); err != nil {
|
|
return errors.Wrap(err, "unable to set maintenance parameters")
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|