美文网首页我爱编程
mongodb的备份与还原

mongodb的备份与还原

作者: 花式开心 | 来源:发表于2018-07-24 10:06 被阅读461次

    如果你开启了身份认证,请确保认证用户拥有备份和还原的权限。

    一、数据库备份

    使用 mongodump 命令备份数据

    mongodump概述

    mongodump常用参数

    • --db:指定导出的数据库
    • --collection:指定导出的集合
    • --excludeCollection:指定不导出的集合
    • --host :远程ip
    • --username:开启身份验证后,用户的登录名
    • -- password:用户的密码
    • --out(指定输出目录):如果不使用这个参数,mongodump将输出文件保存在当前工作目录中名为dump的目录中
    • --archive:导出归档文件,最后只会生成一个文件
    • --gzip:压缩归档的数据库文件,文件的后缀名为.gz

    备份集合

    备份指定数据库的指定文档
    mongodump --db database --collection collectionName
    

    成功备份之后,每个集合(collection)都会生成一个后缀名为.json和.bson的文件,备份文件的目录为


    文件目录结构
    image.png
    排除指定集合,备份剩下的集合
    mongodump --db database --excludeCollection=collection1  --excludeCollection=collection2 
    

    仅导出除collection1,collection2 的其他文档。

    开启身份认证的情况备份

    如果用户开启身份认证之后,具有备份(backup)还原(restore)权限的用户才能备份数据,如果没有该角色的用户,大家使用db.createUser()自行创建,或者看我之前写的文章mongodb身份认证
    使用mongodump命令时,进行身份认证

    #这种方法适用于远程备份,指定远程ip,端口,用户名,密码,输出目录
    #这个命令适用于远程备份,会备份所有数据库
    mongodump --host 127.0.0.1 --port 27017 --username user--password "pass" --out filePath
    
    导出为归档(Archive)文件

    使用--archive参数,导出的数据压缩为一个文件
    注意: --archive 与 --out 不能一起用
    错误的用法:mongodump --archive=fileName --out filePath

    导出一个归档文件
    #将在终端所在的目录下生成一个指定名称的数据库文件
    mongodump --archive=filename --db databse
    
    使用--gizp参数,压缩归档文件
    mongodump --archive=filename.gz --gzip --db database 
    
    如果没有指定--archive参数,会在当前目录下的dump文件夹里生成压缩文件
    mongodump  --gzip --db database 
    
    使用管道符,先备份再还原

    下面这个命令就是将远程数据库备份到本地,这个应该是最常用的命令了

    mongodump --host ipAdress --port 27017 --username user--password "pass" --archive | mongorestore --archive 
    

    二、数据库还原

    使用 mongorestore 命令还原

    mongorestore常用参数(这里只列与mongodump不同的参数),使用

    • --nsInclude :指定还原的集合,支持通配符(*)
    • --nsExclude:指定不还原的集合,支持通配符(*)
    • --nsFrom:修改集合名称,原来集合的名称,支持使用变量
    • --nsTo:修改集合名称,修改之后集合的名称,支持使用变量

    还原指定数据库的指定集合

    这里有两种写法

    #写法1
    mongorestore --collection collection --db database filePath
    #写法2(推荐写法)
    mongorestore --nsInclude database.collections filePath
    

    注:写法1的filePath是json,bson文件所在的目录,一般为"./dump/database",写法2的filePath是数据库备份的根目录,一般为 "./dump"

    使用通配符 * 导出匹配的集合

    #--nsInclude指定要 还原 的集合,--nsExclude指定 不还原 的集合
    mongorestore --nsInclude database.* --nsExclude database.*   filePath
    

    在还原过程中修改集合的名称(单集合)

    mongorestore --nsInclude database.collection --nsFrom database.collectio --nsTo newCollectionName
    
    高级应用:使用模式匹配在备份过程中修改集合名称

    假设现在有如下的集合,集合名称分别为

    • sales_customer1
    • sales_customer2
    • sales_customer3
    • users_customer1
    • users_customer2
    • users_customer3
      我们可以看到上面的集合名称都是有规律的,都是A_B这种模式的名称,A匹配sales,users,B匹配customer1,customer2,customer3,我们可以使用如下命令修改集合名称
    #$$之间的值可以当成一个变量使用,可以看到下边的命令将A和B的位置调换,了,注意这里的单引号不能省略
    mongorestore --nsInclude 'database.*' --nsFrom 'data.$A$_$B$' --nsTo '$B$.$A$'
    

    最终还原之后的,mongodb会把集合名称形如A_B的改为B_A,如下所示

    • customer1_ sales
    • customer2_ sales
    • customer3_ sales
    • customer1_users
    • customer2_users
    • customer3_users

    将本地备份数据还原到远程服务器上

    mongorestore --host ip  --port 27017 --username user--password 'pass' filePath
    

    还原归档的文件

    mongorestore --archive=filename --db database
    

    还原压缩的文件

    mongorestore --gzip --archive=filename --db database
    

    相关文章

      网友评论

        本文标题:mongodb的备份与还原

        本文链接:https://www.haomeiwen.com/subject/nwtemftx.html