Skip to content

Instantly share code, notes, and snippets.

@cortezcristian
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cortezcristian/1726012189de117568c4 to your computer and use it in GitHub Desktop.
Save cortezcristian/1726012189de117568c4 to your computer and use it in GitHub Desktop.
## MongoDump
Normal db dump
```bash
$ mongodump -db <dbname> --out /home/data/backup-<dbname>
```
[Mongo Dump Reference](http://docs.mongodb.org/manual/reference/program/mongodump/#bin.mongodump)
## Stream Backup
Solution to do backup when you don't have enought disk space.
```bash
$ mongo-dump-stream mongodb://localhost/<dbname>
| ssh root@other-server-here.com mongo-load-stream mongodb://localhost/<dbname>
```
## Restore Backup
```bash
$ mongorestore --db <dbname> dump/<dbname>/
```
## Get Usable Sites
```
db.<collection>.find({$and:[
{html:{$exists : true}},
{html:{$ne : ""}},
{html: /\w{16,}/i }
]}).pretty()
> it
> it
```
Modify Usable <collection> to add a flag:
```
db.<collection>.update(
{$and:[
{html:{$exists : true}},
{html:{$ne : ""}},
{html:{$ne : "滚粗!"}},
{html:{$ne : "\n"}},
{html:{ $not: /^403.*Forbidden.*/i }},
{title:{ $not: /.*404.*/i }},
{title:{ $not: /.*error.*404.*/i }},
{title:{ $not: /.*error.*500.*/i }},
{title:{$ne : "Not Found"}},
{html: /\w{16,}/i }
]},
{ $set: { usable : true } },
{ multi: true }
);
```
## Dump a set of usable files
Particular set dump [for one collection](http://stackoverflow.com/questions/16347134/how-to-use-mongodump-for-1-collection)
To get the subset [see reference](http://stackoverflow.com/questions/7828817/is-it-possible-to-mongodump-the-last-x-records-from-a-collection):
```bash
$ mongo
> use <dbname>
> db.<collection>.find({ usable : true }).count()
248699
> db.<collection>.find({ usable : true }).sort({_id:1}).skip(248699 - 300).limit(1)
<mongo-hash>
```
```bash
$ mongodump --db <dbname> --collection <collection> --query '{ usable : true , _id: {$gte: ObjectId("<mongo-hash>")} }' --out /home/data/usable-300
```
i.e.
```
{ "_id" : ObjectId("XXXXXXXXXXXXXX"), "created" : ISODate("2015-05-02T07:38:04.200Z"), "updated" : ISODate("2015-05-08T20:55:40.555Z"), "url" : "http://secret-url.com", ....
$ mongodump --db <dbname> --collection <collection> --query '{ usable : true , _id: {$gte: ObjectId("55447edcad0b715255de2e05")} }' --out /home/data/usable-300
connected to: 127.0.0.1
2015-05-28T08:02:35.347+0000 DATABASE: <dbname> to /home/data/usable-300/<dbname>
2015-05-28T08:02:35.351+0000 <dbname>.<collection> to /home/data/usable-300/<dbname>/<collection>.bson
2015-05-28T08:02:35.894+0000 300 documents
2015-05-28T08:02:35.894+0000 Metadata for <dbname>.<collection> to /home/data/usable-300/<dbname>/<collection>.metadata.json
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment