What command do I use and run?
Or you can make backup script on Windows, remember to add Winrar to %PATH%
bin\mongodump --db=COL1 -o D:\BACK\COL1
rar.exe a -ep1 -r COL1.rar COL1
rename COL1.rar "COL1_%date:~10,4%_%date:~7,2%_%date:~4,2%_%time:~0,2%_%time:~3,2%.rar"
#rmdir /s /q COL1 -> don;t run this on your mongodb/ dir !!!!!
take mongodb backup for particular db and delete 7 days old backup using bin sh command :-
#!/bin/bash
MONGO_DATABASE="nexgtv_16"
APP_NAME="test"
MONGO_HOST="127.0.0.1"
MONGO_PORT="27017"
TIMESTAMP=`date +%F-%H%M`
MONGODUMP_PATH="/usr/bin/mongodump"
BACKUPS_DIR="/home/mongodbbackups/backups/$APP_NAME"
BACKUP_NAME="$APP_NAME-$TIMESTAMP"
$MONGODUMP_PATH -d $MONGO_DATABASE
mkdir -p $BACKUPS_DIR
mv dump $BACKUP_NAME
tar -zcvf $BACKUPS_DIR/$BACKUP_NAME.tgz $BACKUP_NAME
rm -rf $BACKUP_NAME
find /home/mongodbbackups/backups/test/ -mindepth 1 -mtime +7 -delete
There is a utility called : mongodump On the mongo command line you can type :
>./mongodump
The above will create a dump of all the databases on your localhost. To make dump of a single collection use:
./mongodump --db blog --collection posts
Have a look at : mongodump
You can also use gzip
for taking backup of one collection and compressing the backup on the fly:
mongodump --db somedb --collection somecollection --out - | gzip > collectiondump.gz
or with a date in the file name:
mongodump --db somedb --collection somecollection --out - | gzip > dump_`date "+%Y-%m-%d"`.gz
Update:
Backup all collections of a database in a date folder. The files are gziped:
mongodump --db somedb --gzip --out /backups/`date +"%Y-%m-%d"`
Or for a single archive:
mongodump --db somedb --gzip --archive > dump_`date "+%Y-%m-%d"`.gz
Or when mongodb is running inside docker:
docker exec <CONTAINER> sh -c 'exec mongodump --db somedb --gzip --archive' > dump_`date "+%Y-%m-%d"`.gz
This command will make a dump of given database in json and bson format.
mongodump -d <database name> -o <target directory>
to export
mongodump -d <database name> <backup-folder>
to import
mongorestore -d <database name> <backup-folder>