2017-06-07 20:54:37 +00:00
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-07-02 09:14:44 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2017-06-07 20:54:37 +00:00
|
|
|
"path"
|
2017-07-23 12:21:03 +00:00
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
"github.com/restic/restic/internal/backend"
|
2022-10-15 14:23:39 +00:00
|
|
|
"github.com/restic/restic/internal/backend/layout"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/backend/s3"
|
|
|
|
"github.com/restic/restic/internal/debug"
|
|
|
|
"github.com/restic/restic/internal/errors"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2017-06-07 20:54:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
register(&S3Layout{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// S3Layout migrates a repository on an S3 backend from the "s3legacy" to the
|
|
|
|
// "default" layout.
|
|
|
|
type S3Layout struct{}
|
|
|
|
|
|
|
|
// Check tests whether the migration can be applied.
|
2023-05-18 17:18:09 +00:00
|
|
|
func (m *S3Layout) Check(_ context.Context, repo restic.Repository) (bool, string, error) {
|
2023-10-01 09:40:12 +00:00
|
|
|
be := backend.AsBackend[*s3.Backend](repo.Backend())
|
2022-05-01 12:52:00 +00:00
|
|
|
if be == nil {
|
2017-06-07 20:54:37 +00:00
|
|
|
debug.Log("backend is not s3")
|
2022-09-03 09:49:31 +00:00
|
|
|
return false, "backend is not s3", nil
|
2017-06-07 20:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if be.Layout.Name() != "s3legacy" {
|
|
|
|
debug.Log("layout is not s3legacy")
|
2022-09-03 09:49:31 +00:00
|
|
|
return false, "not using the legacy s3 layout", nil
|
2017-06-07 20:54:37 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 09:49:31 +00:00
|
|
|
return true, "", nil
|
2017-06-07 20:54:37 +00:00
|
|
|
}
|
|
|
|
|
2022-06-04 21:45:00 +00:00
|
|
|
func (m *S3Layout) RepoCheck() bool {
|
|
|
|
return false
|
2022-05-01 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2017-07-02 09:14:44 +00:00
|
|
|
func retry(max int, fail func(err error), f func() error) error {
|
|
|
|
var err error
|
|
|
|
for i := 0; i < max; i++ {
|
|
|
|
err = f()
|
|
|
|
if err == nil {
|
2020-12-11 08:41:59 +00:00
|
|
|
return nil
|
2017-07-02 09:14:44 +00:00
|
|
|
}
|
|
|
|
if fail != nil {
|
|
|
|
fail(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// maxErrors for retrying renames on s3.
|
|
|
|
const maxErrors = 20
|
|
|
|
|
2022-10-15 14:23:39 +00:00
|
|
|
func (m *S3Layout) moveFiles(ctx context.Context, be *s3.Backend, l layout.Layout, t restic.FileType) error {
|
2017-07-02 09:14:44 +00:00
|
|
|
printErr := func(err error) {
|
|
|
|
fmt.Fprintf(os.Stderr, "renaming file returned error: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
return be.List(ctx, t, func(fi backend.FileInfo) error {
|
|
|
|
h := backend.Handle{Type: t, Name: fi.Name}
|
2017-06-07 20:54:37 +00:00
|
|
|
debug.Log("move %v", h)
|
2017-07-02 09:14:44 +00:00
|
|
|
|
2018-01-21 16:25:36 +00:00
|
|
|
return retry(maxErrors, printErr, func() error {
|
2020-09-19 19:57:02 +00:00
|
|
|
return be.Rename(ctx, h, l)
|
2017-07-02 09:14:44 +00:00
|
|
|
})
|
2018-01-21 16:25:36 +00:00
|
|
|
})
|
2017-06-07 20:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply runs the migration.
|
|
|
|
func (m *S3Layout) Apply(ctx context.Context, repo restic.Repository) error {
|
2023-10-01 09:40:12 +00:00
|
|
|
be := backend.AsBackend[*s3.Backend](repo.Backend())
|
2022-05-01 12:52:00 +00:00
|
|
|
if be == nil {
|
2017-06-07 20:54:37 +00:00
|
|
|
debug.Log("backend is not s3")
|
|
|
|
return errors.New("backend is not s3")
|
|
|
|
}
|
|
|
|
|
2022-10-15 14:23:39 +00:00
|
|
|
oldLayout := &layout.S3LegacyLayout{
|
2017-07-02 08:47:03 +00:00
|
|
|
Path: be.Path(),
|
|
|
|
Join: path.Join,
|
|
|
|
}
|
|
|
|
|
2022-10-15 14:23:39 +00:00
|
|
|
newLayout := &layout.DefaultLayout{
|
2017-06-07 20:54:37 +00:00
|
|
|
Path: be.Path(),
|
|
|
|
Join: path.Join,
|
|
|
|
}
|
|
|
|
|
2017-07-02 08:47:03 +00:00
|
|
|
be.Layout = oldLayout
|
|
|
|
|
2017-06-07 20:54:37 +00:00
|
|
|
for _, t := range []restic.FileType{
|
|
|
|
restic.SnapshotFile,
|
2020-08-16 09:16:38 +00:00
|
|
|
restic.PackFile,
|
2017-07-02 08:47:20 +00:00
|
|
|
restic.KeyFile,
|
2017-06-07 20:54:37 +00:00
|
|
|
restic.LockFile,
|
|
|
|
} {
|
|
|
|
err := m.moveFiles(ctx, be, newLayout, t)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
be.Layout = newLayout
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name for this migration.
|
|
|
|
func (m *S3Layout) Name() string {
|
|
|
|
return "s3_layout"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Desc returns a short description what the migration does.
|
|
|
|
func (m *S3Layout) Desc() string {
|
|
|
|
return "move files from 's3legacy' to the 'default' repository layout"
|
|
|
|
}
|