Skip to content

Instantly share code, notes, and snippets.

@jayme-github
Created March 30, 2017 18:54
Show Gist options
  • Select an option

  • Save jayme-github/8b7a3fd6e2b5a3a73d23bd8234623132 to your computer and use it in GitHub Desktop.

Select an option

Save jayme-github/8b7a3fd6e2b5a3a73d23bd8234623132 to your computer and use it in GitHub Desktop.
Migrate restic swift backend to plural path names
package main
import (
"fmt"
"github.com/ncw/swift"
"strings"
)
func main() {
container := "restictestcontainer"
prefixMap := map[string]string{
"key/": "keys/",
"snapshot/": "snapshots/",
"lock/": "locks/",
}
c := swift.Connection{
StorageUrl: "http://127.0.0.1:12345/v1/AUTH_admin",
AuthToken: "AUTH_tkbffc8a34abc04de8b0ff32a28a341f02",
}
if !c.Authenticated() {
fmt.Println("Not authenticated")
return
}
for oldPrefix, newPrefix := range prefixMap {
c.ObjectsWalk(container, &swift.ObjectsOpts{Prefix: oldPrefix},
func(opts *swift.ObjectsOpts) (interface{}, error) {
objectNames, err := c.ObjectNames(container, opts)
if err != nil {
fmt.Println(err)
return nil, err
}
for _, objName := range objectNames {
newObjName := strings.TrimPrefix(objName, oldPrefix)
if newObjName == "" {
continue
}
newObjName = newPrefix + newObjName
fmt.Printf("%s -> %s\n", objName, newObjName)
err := c.ObjectMove(container, objName, container, newObjName)
if err != nil {
fmt.Printf("Failed to move %s:\n%v", objName, err)
}
}
return objectNames, nil
},
)
}
}
~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment