backup_command.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. ArgsUsage: " ",
  34. Flags: []cli.Flag{
  35. cli.StringFlag{Name: "data-dir", Value: "", Usage: "Path to the etcd data dir"},
  36. cli.StringFlag{Name: "wal-dir", Value: "", Usage: "Path to the etcd wal dir"},
  37. cli.StringFlag{Name: "backup-dir", Value: "", Usage: "Path to the backup dir"},
  38. cli.StringFlag{Name: "backup-wal-dir", Value: "", Usage: "Path to the backup wal dir"},
  39. },
  40. Action: handleBackup,
  41. }
  42. }
  43. // handleBackup handles a request that intends to do a backup.
  44. func handleBackup(c *cli.Context) {
  45. var srcWAL string
  46. var destWAL string
  47. srcSnap := path.Join(c.String("data-dir"), "member", "snap")
  48. destSnap := path.Join(c.String("backup-dir"), "member", "snap")
  49. if c.String("wal-dir") != "" {
  50. srcWAL = c.String("wal-dir")
  51. } else {
  52. srcWAL = path.Join(c.String("data-dir"), "member", "wal")
  53. }
  54. if c.String("backup-wal-dir") != "" {
  55. destWAL = c.String("backup-wal-dir")
  56. } else {
  57. destWAL = path.Join(c.String("backup-dir"), "member", "wal")
  58. }
  59. if err := os.MkdirAll(destSnap, 0700); err != nil {
  60. log.Fatalf("failed creating backup snapshot dir %v: %v", destSnap, err)
  61. }
  62. ss := snap.New(srcSnap)
  63. snapshot, err := ss.Load()
  64. if err != nil && err != snap.ErrNoSnapshot {
  65. log.Fatal(err)
  66. }
  67. var walsnap walpb.Snapshot
  68. if snapshot != nil {
  69. walsnap.Index, walsnap.Term = snapshot.Metadata.Index, snapshot.Metadata.Term
  70. newss := snap.New(destSnap)
  71. if err = newss.SaveSnap(*snapshot); err != nil {
  72. log.Fatal(err)
  73. }
  74. }
  75. w, err := wal.OpenForRead(srcWAL, walsnap)
  76. if err != nil {
  77. log.Fatal(err)
  78. }
  79. defer w.Close()
  80. wmetadata, state, ents, err := w.ReadAll()
  81. switch err {
  82. case nil:
  83. case wal.ErrSnapshotNotFound:
  84. fmt.Printf("Failed to find the match snapshot record %+v in wal %v.", walsnap, srcWAL)
  85. fmt.Printf("etcdctl will add it back. Start auto fixing...")
  86. default:
  87. log.Fatal(err)
  88. }
  89. var metadata etcdserverpb.Metadata
  90. pbutil.MustUnmarshal(&metadata, wmetadata)
  91. idgen := idutil.NewGenerator(0, time.Now())
  92. metadata.NodeID = idgen.Next()
  93. metadata.ClusterID = idgen.Next()
  94. neww, err := wal.Create(destWAL, pbutil.MustMarshal(&metadata))
  95. if err != nil {
  96. log.Fatal(err)
  97. }
  98. defer neww.Close()
  99. if err := neww.Save(state, ents); err != nil {
  100. log.Fatal(err)
  101. }
  102. if err := neww.SaveSnapshot(walsnap); err != nil {
  103. log.Fatal(err)
  104. }
  105. }