backup_command.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package command
  15. import (
  16. "fmt"
  17. "log"
  18. "os"
  19. "path"
  20. "time"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
  22. "github.com/coreos/etcd/etcdserver/etcdserverpb"
  23. "github.com/coreos/etcd/pkg/idutil"
  24. "github.com/coreos/etcd/pkg/pbutil"
  25. "github.com/coreos/etcd/snap"
  26. "github.com/coreos/etcd/wal"
  27. "github.com/coreos/etcd/wal/walpb"
  28. )
  29. func NewBackupCommand() cli.Command {
  30. return cli.Command{
  31. Name: "backup",
  32. Usage: "backup an etcd directory",
  33. Flags: []cli.Flag{
  34. cli.StringFlag{Name: "data-dir", Value: "", Usage: "Path to the etcd data dir"},
  35. cli.StringFlag{Name: "backup-dir", Value: "", Usage: "Path to the backup dir"},
  36. },
  37. Action: handleBackup,
  38. }
  39. }
  40. // handleBackup handles a request that intends to do a backup.
  41. func handleBackup(c *cli.Context) {
  42. srcSnap := path.Join(c.String("data-dir"), "member", "snap")
  43. destSnap := path.Join(c.String("backup-dir"), "member", "snap")
  44. srcWAL := path.Join(c.String("data-dir"), "member", "wal")
  45. destWAL := path.Join(c.String("backup-dir"), "member", "wal")
  46. if err := os.MkdirAll(destSnap, 0700); err != nil {
  47. log.Fatalf("failed creating backup snapshot dir %v: %v", destSnap, err)
  48. }
  49. ss := snap.New(srcSnap)
  50. snapshot, err := ss.Load()
  51. if err != nil && err != snap.ErrNoSnapshot {
  52. log.Fatal(err)
  53. }
  54. var walsnap walpb.Snapshot
  55. if snapshot != nil {
  56. walsnap.Index, walsnap.Term = snapshot.Metadata.Index, snapshot.Metadata.Term
  57. newss := snap.New(destSnap)
  58. if err := newss.SaveSnap(*snapshot); err != nil {
  59. log.Fatal(err)
  60. }
  61. }
  62. w, err := wal.OpenNotInUse(srcWAL, walsnap)
  63. if err != nil {
  64. log.Fatal(err)
  65. }
  66. defer w.Close()
  67. wmetadata, state, ents, err := w.ReadAll()
  68. switch err {
  69. case nil:
  70. case wal.ErrSnapshotNotFound:
  71. fmt.Printf("Failed to find the match snapshot record %+v in wal %v.", walsnap, srcWAL)
  72. fmt.Printf("etcdctl will add it back. Start auto fixing...")
  73. default:
  74. log.Fatal(err)
  75. }
  76. var metadata etcdserverpb.Metadata
  77. pbutil.MustUnmarshal(&metadata, wmetadata)
  78. idgen := idutil.NewGenerator(0, time.Now())
  79. metadata.NodeID = idgen.Next()
  80. metadata.ClusterID = idgen.Next()
  81. neww, err := wal.Create(destWAL, pbutil.MustMarshal(&metadata))
  82. if err != nil {
  83. log.Fatal(err)
  84. }
  85. defer neww.Close()
  86. if err := neww.Save(state, ents); err != nil {
  87. log.Fatal(err)
  88. }
  89. if err := neww.SaveSnapshot(walsnap); err != nil {
  90. log.Fatal(err)
  91. }
  92. }