This is the 6th day of my participation in the August More Text Challenge

After creating a snapshot, mount the snapshot image to the file system and copy data from the snapshot using Linux LVM logical volume management. The generated backup contains a complete copy of all the data to back up the database.

Note: Mongodb data and log directories need to be started in the logical volume mount directory

The snapshot backup

db.fsyncLock(); Mongodb_bak /dev/vg01 # mongodb_bak /dev/vg01 # create snapshot umount / dev/vg01 mongodb_bak # uninstall dd if = / dev/vg01 / mongodb_bak | gzip > bak / 20210817. # gz compressed archive the fsyncUnlock (); Mysql > unlock databaseCopy the code

Snapshot Direct Restore

db.fsyncLock(); Mongodb_restore /dev/vg01: mongodb_restore /dev/vg01: mongodb_restore /dev/vg01: mongodb_restore /dev/vg01: mongodb_restore /dev/vg01 /dev/vg01/mongodb_restore # mongodb_restore Mongodb_bak if=/dev/vg01/mongodb_bak of=/dev/vg01/ mongodb_bak Mount the new snapshot to data directory db.fsynCUNLock (); Mysql > unlock databaseCopy the code

Restore from a compressed file

Umount/dev/vg01 / mongodb_bak # uninstall lv lvcreate - size # 1 g - name MDB - new vg0 create lv gzip c - d - 20210817. The gz | dd Of =/dev/vg0/mdb-new # mount /dev/vg01/mdb-new /mongodb #Copy the code

Advantages and disadvantages

  1. fast
  2. Point-in-time snapshot backup is supported
  3. Provide incremental backup

disadvantages

  1. Need file system support for point-in-time snapshots (LVM)
  2. You need to enable the log function
  3. Snapshots create a mirror image of the entire disk, so storing data files, configurations, and logs on one logical disk saves space

Copy the file

Backup file

# service mongod stop stop mongod echo ` date + % Y H % m % d % % m % S ` | xargs -i {} sh -c 'mkdir. / bak / {}; Cp -d /mongodb/data./bak/{}Copy the code

Restore from file

# service mongod stop stop mongod echo bak_ ` date + % Y H % m % d % % m % S ` | xargs -i {} sh -c 'mkdir. / bak / {}; Cp -a bak/20210818025815/data /mongodb Restart mongodbCopy the code

Advantages and disadvantages

  1. No file system is required to support snapshot

disadvantages

  1. You must stop all writes to Mongod before backing up the copy, otherwise it will be an invalid backup
  2. Replica set point in time recovery is not supported and large shard clusters are difficult to manage
  3. Backup files take up more space (including indexes and duplicate underlying file populations and fragments)

mongodump

Mongodump backup

Mongodump - uri = "mongo: / / 127.0.0.1:27017 # export the entire instance mongodump - uri =" mongo: / / 127.0.0.1:27017 / database" - out = / dump / ` date + % Y % m % d # ` export database and specify the location specified mongodump - uri = "mongo: / / 127.0.0.1:27017 / database" -- gzip -- out = / dump / ` date + % Y % m % d # ` export specified database and compression mongodump - uri = "mongo: / / 127.0.0.1:27017" - the oplog # export oplog, mongodump need to open a copy of the set - a uri = "mongo: / / 127.0.0.1:27017 / database" - specifies a collection mongodump excludeCollection = # users out - a uri = "mongo: / / 127.0.0.1:27017 / database" - archive = ` date + % Y % m % d ` archive # export archive fileCopy the code

Mongodump reduction

Mongorestore - uri = "mongo: / / 127.0.0.1:27017 /" - db = database dump / # 20210818 / database/reduction mongorestore specified database - a uri = "mongo: / / 127.0.0.1:27017 /" - db = database - collection = collection/dump / 20210818 / database/collection. Bson # Specifies a collection mongorestore reduction - a uri = "mongo: / / 127.0.0.1:27017 /" -- archive = 20210818. Archive reduction mongorestore # from the archive file - a uri = "mongo: / / 127.0.0.1:27017 /" -- archive = 20210818. Archive - dryRun - reduction mongorestore verbose # try - a uri = "mongo: / / 127.0.0.1:27017 /" -- gzip # from compressed file reduction mongorestore -- uri = "mongo: / / 127.0.0.1:27017 /" -- gzip --nsInclude=db1.user* --nsInclude=test.* # Restore specified database/collectionCopy the code

Advantages and disadvantages

  1. Backing up and restoring small mongoDB clusters is easier and more efficient, with less space for backup files (only files are backed up, not indexes)
  2. The application can continue to modify data during the backup process (record oplog and use the –oplog option to achieve consistent data state)

disadvantages

  1. The backup database does not contain the Local database, and only the documents of the database are backed up, but the indexes of the database are not backed up. Therefore, indexes must be rebuilt after restoration
  2. Backing up and restoring large mogoDB clusters is not ideal (inefficient)
  3. Backup affects performance of running Mongod (generates network traffic)
  4. If the backup data is larger than the system memory, the query operation will cause page errors
  5. The formats of different versions of Mongodump are not compatible. Do not use the new version of Mongodump to back up data of the old version

Problems and solutions

The error message The solution
Implicit TCP FastOpen unavailable. If TCP FastOpen is required rm -f /tmp/mongodb-27017.sock && service mongod restart

reference

MongoDB Backup Methods

mongodump

GitHub