受欢迎的博客标签

MongoDB Backup Strategies on ubuntu18.04 64

Published

MongoDB Backup Methods

Back Up by Copying Underlying Data Files
Back Up with cp or rsync
Back Up with mongodump

MongoDB gives you three ways to acomplish it. In this post I'm using monogodump command for creating a backup and mongorestore for recreating the data. 

Table of Contents

 

oo

 

 

 

使用mongo自带命令来迁移数据.

一、备份单个集合

思路:先导出集合数据再导入到数据库中

 

mongoimport --host localhost --port 27018  --collection BlogPost --db iaspnetcore --file events.json

先导出集合
mongoexport --host "localhost" --port 27018 --collection BlogPost --db wwwstocksocom --out events.json

再导入集合
mongoimport --host localhost --port 27017  --collection BlogPost --db wwwstocksocom --file events.json
导出命令:mongoexport

语法:mongoexport -d dbname -c collectionname -o filepath --type json/csv -f field

-d:数据库名

-c:集合名称

-o : 导出数据文件的路径

-type : 导出数据类型,默认json

 

导入命令:mongoimport

语法:mongoimport -d dbname -c collectionname --file filename --headerline --type json/csv -f field

-d:数据库名

-c:集合名称

--file : 选择导入的文件

-type : 文件类型,默认json

-f : 字段,type为csv是必须设置此项

 

二、整体库迁移

 

windows 本机导入

cd \mongodb\bin
mongorestore --host 106.**.193.**  -u root --authenticationDatabase admin dump

 

cd /usr/bin
mongorestore --host 106.**.**.**  -u root --authenticationDatabase admin dump

step 1:查看数据库名称

cd x:\mongodb\bin

mongo --port  27018

退出

step 2:导出数据库

mongodump --port 27018 --db iaspnetcore

导出的数据存放目录缺省为dump

2019/11/19  07:00    <DIR>          .
2019/11/19  07:00    <DIR>          ..
2019/11/19  06:56    <DIR>          iaspnetcore
2019/11/19  06:51    <DIR>          Stock

2019/11/19  06:59    <DIR>          wwwstocksocom
               1 个文件              0 字节
               5 个目录 263,823,224,832 可用字节

 

带认证的数据库

#导出库
mongodump --port 30000 --db news_novel
#导入新库
on unbtun
mongorestore --db wwwstocksocom --drop /tmp/wwwstocksocom/wwwstocksocom

mongostore -h X.X.X.X:10000 -d news_novel --dir /opt/soft/mongodb/bin/dump/news_novel -u=admin -p=123456

 

mongodump --host <mongodb_host> --port <port>  -u <username>  --authenticationDatabase  <database>
说明
<mongodb_host>:自建MongoDB数据库的服务器地址,本机可使用127.0.0.1。
<port>:数据库服务的端口号,默认为27017。
<username>:自建MongoDB数据库的数据库账号。
<database>:鉴权数据库名,即数据库账号所属的数据库。

 

Back Up with cp or rsync

If your storage system does not support snapshots, you can copy the files directly using cp, rsync, or a similar tool. Since copying multiple files is not an atomic operation, you must stop all writes to the mongod before copying the files. Otherwise, you will copy the files in an invalid state.

Backups produced by copying the underlying data do not support point in time recovery for replica sets and are difficult to manage for larger sharded clusters. Additionally, these backups are larger because they include the indexes and duplicate underlying storage padding and fragmentation. mongodump, by contrast, creates smaller backups.

step 1。you need to stop mongo
And you can make a backup with ...

rsync -vahP /var/lib/mongodb/ /media/backup/

for a backup to a local directory or

rsync -vahP /var/lib/mongodb/ {user}@{url}:/{dir}/

for a backup to a remote system

Restoring: stop mongo and switch the 2 locations.

 

sudo mkdir /var/backups/mongobackups

#/var/backups/10-29-20/
sudo mongodump --db iaspnetcore --out /var/backups/mongobackups/`date +"%m-%d-%y"`

sudo crontab -e

https://www.digitalocean.com/community/tutorials/how-to-back-up-restore-and-migrate-a-mongodb-database-on-ubuntu-18-04

 

https://www.mongodb.com/backup-and-its-role-disaster-recovery