How to use MySQL to learn MongoDB backup and recovery

Source: Internet
Author: User
Tags mysql backup mongodump mongorestore

In the above article, we learned how to use MySQL to learn MongoDB authorization and permissions. In this article, we will continue our learning journey and learn how to back up and restore the two.

It is important to back up your database when database tables are lost or damaged. In the event of a system crash, you must be able to restore as much data as possible from your table to the State at the time of the crash.

1. MySQL backup and recovery

MySQL backup methods are generally divided into the following three types:

Directly copy database files

Back up a database using mysqlhotcopy

Back up a database using mysqldump

(1) directly copy database files

It is the most direct, fast, and convenient, but basically cannot implement Incremental backup. To ensure data consistency, run the following SQL statement before the back-to-back file:

Flush tables with read lock;

That is, the data in the memory is refreshed to the disk, and the data table is locked to ensure that no new data is written during the copy process. In this way, the backup data recovery is also very simple, just copy it back to the original database directory.

However, for an Innodb table, you also need to back up its log file, that is, the ib_logfile * file. Because when the Innodb table is corrupted, these log files can be recovered.

(2) Use mysqlhotcopy to back up the database

Mysqlhotcopy is a perl program. It uses lock tables, flush tables, and cp or scp to quickly back up databases. It is the fastest way to back up a database or a single table, but it can only run on a local server, and mysqlhotcopy can only back up MyISAM tables, there is no way to use Innodb tables.

(3) Use mysqldump to back up the database

Mysqldump is an SQL-level backup. It imports data tables into SQL script files and is suitable for upgrading between different MySQL versions. This is also the most popular backup method.

2. MongoDB backup and recovery

MongoDB provides two commands to back up (mongodump) and recover (mongorestore) databases.

(1) mongodump Backup Tool

Let's take a look at the help information of this tool:

 
 
  1. [root@localhost bin]# ./mongodump --help  
  2. options:  
  3. --help produce help message  
  4. -v [ --verbose ] be more verbose (include multiple times for more  
  5. verbosity e.g. -vvvvv)  
  6. -h [ --host ] arg mongo host to connect to ( /s1,s2 for  
  7. sets)  
  8. --port arg server port. Can also use --host hostname:port  
  9. --ipv6 enable IPv6 support (disabled by default)  
  10. -u [ --username ] arg username  
  11. -p [ --password ] arg password  
  12. --dbpath arg directly access mongod database files in the given  
  13. path, instead of connecting to a mongod server -  
  14. needs to lock the data directory, so cannot be used  
  15. if a mongod is currently accessing the same path  
  16. --directoryperdb if dbpath specified, each db is in a separate  
  17. directory  
  18. -d [ --db ] arg database to use  
  19. -c [ --collection ] arg collection to use (some commands)  
  20. -o [ --out ] arg (=dump) output directory or "-" for stdout  
  21. -q [ --query ] arg json query  
  22. --oplog Use oplog for point-in-time snapshotting  
  23. --repair try to recover a crashed database  
  24. [root@localhost bin]#  

For example, there is a database named "foo" in our system. Below we will demonstrate how to back up this database:

 
 
  1. [root@localhost bin]# ./mongodump -d foo -o /data/dump  
  2. connected to: 127.0.0.1  
  3. DATABASE: foo to /data/dump/foo  
  4. foo.system.indexes to /data/dump/foo/system.indexes.bson  
  5. 3 objects  
  6. foo.system.users to /data/dump/foo/system.users.bson  
  7. 1 objects  
  8. foo.t2 to /data/dump/foo/t2.bson  
  9. 1 objects  
  10. foo.t1 to /data/dump/foo/t1.bson  
  11. 2 objects  
  12. [root@localhost bin]#  

After the tool returns information, we can see that the data in foo has been backed up into a bson file. Next we will verify it in the backup directory:

 
 
  1. [Root @ localhost dump] # ll/data/dump/foo/
  2. Total 16
  3. -Rw-r -- 1 root 193 04-22 :55 system. indexes. bson
  4. -Rw-r -- 1 root 91 04-22 :55 system. users. bson
  5. -Rw-r -- 1 root 66 04-22 :55 t1.bson
  6. -Rw-r -- 1 root 49 04-22 :55 t2.bson
  7. [Root @ localhost dump] #

The results show that the table in the foo database has been successfully backed up. Next we will demonstrate how to restore the backup back.

(2) mongorestore restoration Tool

Let's take a look at the help information of this tool:

 
 
  1. [root@localhost bin]# ./mongorestore --help  
  2. usage: ./mongorestore [options] [directory or filename to restore from]  
  3. options:  
  4. --help produce help message  
  5. -v [ --verbose ] be more verbose (include multiple times for more  
  6. verbosity e.g. -vvvvv)  
  7. -h [ --host ] arg mongo host to connect to ( /s1,s2 for sets)  
  8. --port arg server port. Can also use --host hostname:port  
  9. --ipv6 enable IPv6 support (disabled by default)  
  10. -u [ --username ] arg username  
  11. -p [ --password ] arg password  
  12. --dbpath arg directly access mongod database files in the given  
  13. path, instead of connecting to a mongod server -  
  14. needs to lock the data directory, so cannot be used  
  15. if a mongod is currently accessing the same path  
  16. --directoryperdb if dbpath specified, each db is in a separate  
  17. directory  
  18. -d [ --db ] arg database to use  
  19. -c [ --collection ] arg collection to use (some commands)  
  20. --objcheck validate object before inserting  
  21. --filter arg filter to apply before inserting  
  22. --drop drop each collection before import  
  23. --oplogReplay replay oplog for point-in-time restore  
  24. [root@localhost bin]# 

For example, we deleted the "foo" database first:

 
 
  1. [root@localhost bin]# ./mongo  
  2. MongoDB shell version: 1.8.1  
  3. connecting to: test  
  4. > use foo  
  5. switched to db foo  
  6. > db.dropDatabase();  
  7. { "dropped" : "foo", "ok" : 1 }  
  8. > show dbs  
  9. admin 0.0625GB  
  10. local (empty)  
  11. test 0.0625GB  

Next we will demonstrate how to restore this database:

 
 
  1. [root@localhost bin]# ./mongorestore --directoryperdb /data/dump  
  2. connected to: 127.0.0.1  
  3. Sun Apr 22 12:01:27 /data/dump/foo/t1.bson  
  4. Sun Apr 22 12:01:27 going into namespace [foo.t1]  
  5. Sun Apr 22 12:01:27 2 objects found  
  6. Sun Apr 22 12:01:27 /data/dump/foo/t2.bson  
  7. Sun Apr 22 12:01:27 going into namespace [foo.t2]  
  8. Sun Apr 22 12:01:27 1 objects found  
  9. Sun Apr 22 12:01:27 /data/dump/foo/system.users.bson  
  10. Sun Apr 22 12:01:27 going into namespace [foo.system.users]  
  11. Sun Apr 22 12:01:27 1 objects found  
  12. Sun Apr 22 12:01:27 /data/dump/foo/system.indexes.bson  
  13. Sun Apr 22 12:01:27 going into namespace [foo.system.indexes]  
  14. Sun Apr 22 12:01:27 { name: "_id_", ns: "foo.system.users", key: { _id: 1 }, v: 0 }  
  15. Sun Apr 22 12:01:27 { name: "_id_", ns: "foo.t2", key: { _id: 1 }, v: 0 }  
  16. Sun Apr 22 12:01:27 { name: "_id_", ns: "foo.t1", key: { _id: 1 }, v: 0 }  
  17. Sun Apr 22 12:01:27 3 objects found  
  18. [root@localhost bin]# 

Using the tool to return information, we can see that the data in foo has been recovered. Next we will go to the library to verify:

 
 
  1. [root@localhost bin]# ./mongo  
  2. MongoDB shell version: 1.8.1  
  3. connecting to: test  
  4. > use foo  
  5. switched to db foo  
  6. > show collections;  
  7. system.indexes  
  8. system.users  
  9. t1  
  10. t2  

It turns out that the foo database table has been successfully restored.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.