Strategies for Backing Up MongoDB

22
Engineering Manager, 10gen Jeff Yemin #MongoBoston Strategies for Backing Up MongoDB

description

For the first time this year, 10gen will be offering a track completely dedicated to Operations at MongoSV, 10gen's annual MongoDB user conference on December 4. Learn more at MongoSV.com Come learn about the different ways to back up your single servers, replica sets, and sharded clusters

Transcript of Strategies for Backing Up MongoDB

Page 1: Strategies for Backing Up MongoDB

Engineering Manager, 10gen

Jeff Yemin

#MongoBoston

Strategies for Backing Up MongoDB

Page 2: Strategies for Backing Up MongoDB

File and Directory Layout• A set of files per database

Page 3: Strategies for Backing Up MongoDB

Insert with write concern of {fsync : true}

Page 4: Strategies for Backing Up MongoDB

Archive the data directory

Page 5: Strategies for Backing Up MongoDB

Restore the data directory

Page 6: Strategies for Backing Up MongoDB

Start mongod on restored data directory

Page 7: Strategies for Backing Up MongoDB

Everything is fine, right?

• No, it's not

• But you can't tell until you look

Page 8: Strategies for Backing Up MongoDB

Try validating the collection• In the shell, run the validate command

Page 9: Strategies for Backing Up MongoDB

How can we get a clean backup?• kill mongod

• fsyncLock / fsyncUnlock

Page 10: Strategies for Backing Up MongoDB

How can we get a clean backup?• mongodump

Page 11: Strategies for Backing Up MongoDB

mongodump

• Snapshot of each collection– Does NOT represent a point in time, even for a

single collection

• Can NOT be combined with fsyncLock– Remember, you can't read…

• You CAN dump directly from data files to get a point in time backup– mongodump –dbpath

• Can be costlier than archiving as FS level

Page 12: Strategies for Backing Up MongoDB

Snaphot Query

1

2

3 4

5

6

7

8 9

Page 13: Strategies for Backing Up MongoDB

How can we get a clean backup?• journaling

Page 14: Strategies for Backing Up MongoDB

Journaling

• Write-ahead log

• Guarantees a consistent view even after a hard crash

• Default behavior as of 2.0

• Journal stored in –dbpath /journal folder

• --journalCommitInterval* (2ms - 300ms)

Page 15: Strategies for Backing Up MongoDB

Journaling implications for backup

• Logical Volume Manager (LVM)

• LVM snapshots to the rescue– lvcreate –size 100M –snapshot –name mdb-snap01 /dev/vg0/mongodb

• No shutdown or fsyncLock needed

• True point in time backup for a single instance

Page 16: Strategies for Backing Up MongoDB

Replica Sets

Page 17: Strategies for Backing Up MongoDB

Backing up a replica set

• Back up a (hidden) secondary– kill mongod– fsyncLock– mongodump– LVM snapshot

Page 18: Strategies for Backing Up MongoDB

Mongodump for replica sets• True point in time

– mongodump –oplog– mongorestore –-oplogreplay

• Snapshot query of each collection, then replay the oplog at the end– Similar to how a new secondary does an initial

sync

Page 19: Strategies for Backing Up MongoDB

Sharded clusters

Shard 1 Shard 2 Shard 3 Shard 4

5

9

1

6

10

2

7

11

3

8

12

4

17

21

13

18

22

14

19

23

15

20

24

16

29

33

25

30

34

26

31

35

27

32

36

28

41

45

37

42

46

38

43

47

39

44

48

40

mongos

balancerconfig

config

config

Chunks!

Page 20: Strategies for Backing Up MongoDB

Backing up a sharded cluster• mongodump through mongos

– (but no –oplog)

• mongorestore through mongos

Page 21: Strategies for Backing Up MongoDB

Backup a Sharded Cluster1. Stop Balancer, and wait till inactive

(state:0) db.settings.update( { _id: "balancer" }, { $set : { stopped: true } } , true )

2. Stop a config server Backup Data– Each shard– Config server (mongodump --db config)

3. Restart config server

4. Resume balancer

Page 22: Strategies for Backing Up MongoDB

Engineering Manager, 10gen

Jeff Yemin

#MongoBoston

Thank You