backup_command.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Copyright 2015 The etcd Authors
  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. "encoding/binary"
  17. "log"
  18. "os"
  19. "path"
  20. "path/filepath"
  21. "regexp"
  22. "time"
  23. "github.com/coreos/etcd/etcdserver/etcdserverpb"
  24. "github.com/coreos/etcd/etcdserver/membership"
  25. "github.com/coreos/etcd/pkg/fileutil"
  26. "github.com/coreos/etcd/pkg/idutil"
  27. "github.com/coreos/etcd/pkg/pbutil"
  28. "github.com/coreos/etcd/raft/raftpb"
  29. "github.com/coreos/etcd/snap"
  30. "github.com/coreos/etcd/wal"
  31. "github.com/coreos/etcd/wal/walpb"
  32. bolt "github.com/coreos/bbolt"
  33. "github.com/urfave/cli"
  34. )
  35. func NewBackupCommand() cli.Command {
  36. return cli.Command{
  37. Name: "backup",
  38. Usage: "backup an etcd directory",
  39. ArgsUsage: " ",
  40. Flags: []cli.Flag{
  41. cli.StringFlag{Name: "data-dir", Value: "", Usage: "Path to the etcd data dir"},
  42. cli.StringFlag{Name: "wal-dir", Value: "", Usage: "Path to the etcd wal dir"},
  43. cli.StringFlag{Name: "backup-dir", Value: "", Usage: "Path to the backup dir"},
  44. cli.StringFlag{Name: "backup-wal-dir", Value: "", Usage: "Path to the backup wal dir"},
  45. cli.BoolFlag{Name: "with-v3", Usage: "Backup v3 backend data"},
  46. },
  47. Action: handleBackup,
  48. }
  49. }
  50. // handleBackup handles a request that intends to do a backup.
  51. func handleBackup(c *cli.Context) error {
  52. var srcWAL string
  53. var destWAL string
  54. withV3 := c.Bool("with-v3")
  55. srcSnap := filepath.Join(c.String("data-dir"), "member", "snap")
  56. destSnap := filepath.Join(c.String("backup-dir"), "member", "snap")
  57. if c.String("wal-dir") != "" {
  58. srcWAL = c.String("wal-dir")
  59. } else {
  60. srcWAL = filepath.Join(c.String("data-dir"), "member", "wal")
  61. }
  62. if c.String("backup-wal-dir") != "" {
  63. destWAL = c.String("backup-wal-dir")
  64. } else {
  65. destWAL = filepath.Join(c.String("backup-dir"), "member", "wal")
  66. }
  67. if err := fileutil.CreateDirAll(destSnap); err != nil {
  68. log.Fatalf("failed creating backup snapshot dir %v: %v", destSnap, err)
  69. }
  70. walsnap := saveSnap(destSnap, srcSnap)
  71. metadata, state, ents := loadWAL(srcWAL, walsnap, withV3)
  72. saveDB(filepath.Join(destSnap, "db"), filepath.Join(srcSnap, "db"), state.Commit, withV3)
  73. idgen := idutil.NewGenerator(0, time.Now())
  74. metadata.NodeID = idgen.Next()
  75. metadata.ClusterID = idgen.Next()
  76. neww, err := wal.Create(destWAL, pbutil.MustMarshal(&metadata))
  77. if err != nil {
  78. log.Fatal(err)
  79. }
  80. defer neww.Close()
  81. if err := neww.Save(state, ents); err != nil {
  82. log.Fatal(err)
  83. }
  84. if err := neww.SaveSnapshot(walsnap); err != nil {
  85. log.Fatal(err)
  86. }
  87. return nil
  88. }
  89. func saveSnap(destSnap, srcSnap string) (walsnap walpb.Snapshot) {
  90. ss := snap.New(srcSnap)
  91. snapshot, err := ss.Load()
  92. if err != nil && err != snap.ErrNoSnapshot {
  93. log.Fatal(err)
  94. }
  95. if snapshot != nil {
  96. walsnap.Index, walsnap.Term = snapshot.Metadata.Index, snapshot.Metadata.Term
  97. newss := snap.New(destSnap)
  98. if err = newss.SaveSnap(*snapshot); err != nil {
  99. log.Fatal(err)
  100. }
  101. }
  102. return walsnap
  103. }
  104. func loadWAL(srcWAL string, walsnap walpb.Snapshot, v3 bool) (etcdserverpb.Metadata, raftpb.HardState, []raftpb.Entry) {
  105. w, err := wal.OpenForRead(srcWAL, walsnap)
  106. if err != nil {
  107. log.Fatal(err)
  108. }
  109. defer w.Close()
  110. wmetadata, state, ents, err := w.ReadAll()
  111. switch err {
  112. case nil:
  113. case wal.ErrSnapshotNotFound:
  114. log.Printf("Failed to find the match snapshot record %+v in wal %v.", walsnap, srcWAL)
  115. log.Printf("etcdctl will add it back. Start auto fixing...")
  116. default:
  117. log.Fatal(err)
  118. }
  119. re := path.Join(membership.StoreMembersPrefix, "[[:xdigit:]]{1,16}", "attributes")
  120. memberAttrRE := regexp.MustCompile(re)
  121. removed := uint64(0)
  122. i := 0
  123. remove := func() {
  124. ents = append(ents[:i], ents[i+1:]...)
  125. removed++
  126. i--
  127. }
  128. for i = 0; i < len(ents); i++ {
  129. ents[i].Index -= removed
  130. if ents[i].Type == raftpb.EntryConfChange {
  131. log.Println("ignoring EntryConfChange raft entry")
  132. remove()
  133. continue
  134. }
  135. var raftReq etcdserverpb.InternalRaftRequest
  136. var v2Req *etcdserverpb.Request
  137. if pbutil.MaybeUnmarshal(&raftReq, ents[i].Data) {
  138. v2Req = raftReq.V2
  139. } else {
  140. v2Req = &etcdserverpb.Request{}
  141. pbutil.MustUnmarshal(v2Req, ents[i].Data)
  142. }
  143. if v2Req != nil && v2Req.Method == "PUT" && memberAttrRE.MatchString(v2Req.Path) {
  144. log.Println("ignoring member attribute update on", v2Req.Path)
  145. remove()
  146. continue
  147. }
  148. if v2Req != nil {
  149. continue
  150. }
  151. if v3 || raftReq.Header == nil {
  152. continue
  153. }
  154. log.Println("ignoring v3 raft entry")
  155. remove()
  156. }
  157. state.Commit -= removed
  158. var metadata etcdserverpb.Metadata
  159. pbutil.MustUnmarshal(&metadata, wmetadata)
  160. return metadata, state, ents
  161. }
  162. // saveDB copies the v3 backend and strips cluster information.
  163. func saveDB(destDB, srcDB string, idx uint64, v3 bool) {
  164. // open src db to safely copy db state
  165. if v3 {
  166. var src *bolt.DB
  167. ch := make(chan *bolt.DB, 1)
  168. go func() {
  169. src, err := bolt.Open(srcDB, 0444, &bolt.Options{ReadOnly: true})
  170. if err != nil {
  171. log.Fatal(err)
  172. }
  173. ch <- src
  174. }()
  175. select {
  176. case src = <-ch:
  177. case <-time.After(time.Second):
  178. log.Println("waiting to acquire lock on", srcDB)
  179. src = <-ch
  180. }
  181. defer src.Close()
  182. tx, err := src.Begin(false)
  183. if err != nil {
  184. log.Fatal(err)
  185. }
  186. // copy srcDB to destDB
  187. dest, err := os.Create(destDB)
  188. if err != nil {
  189. log.Fatal(err)
  190. }
  191. if _, err := tx.WriteTo(dest); err != nil {
  192. log.Fatal(err)
  193. }
  194. dest.Close()
  195. if err := tx.Rollback(); err != nil {
  196. log.Fatal(err)
  197. }
  198. }
  199. db, err := bolt.Open(destDB, 0644, &bolt.Options{})
  200. if err != nil {
  201. log.Fatal(err)
  202. }
  203. tx, err := db.Begin(true)
  204. if err != nil {
  205. log.Fatal(err)
  206. }
  207. // remove membership information; should be clobbered by --force-new-cluster
  208. for _, bucket := range []string{"members", "members_removed", "cluster"} {
  209. tx.DeleteBucket([]byte(bucket))
  210. }
  211. // update consistent index to match hard state
  212. if !v3 {
  213. idxBytes := make([]byte, 8)
  214. binary.BigEndian.PutUint64(idxBytes, idx)
  215. b, err := tx.CreateBucketIfNotExists([]byte("meta"))
  216. if err != nil {
  217. log.Fatal(err)
  218. }
  219. b.Put([]byte("consistent_index"), idxBytes)
  220. }
  221. if err := tx.Commit(); err != nil {
  222. log.Fatal(err)
  223. }
  224. if err := db.Close(); err != nil {
  225. log.Fatal(err)
  226. }
  227. }