Dump
Meta Data(JSON)와 Data(BSON)를 동시에 받는다.
전체를 받을 수 있고, 데이터 베이스, 콜렉션 단위로 받을 수도 있다.
백업 경로만 지정해 주면 데이터베이스 명으로 디렉터리를 만들고 Dump를 수행 함
Dump
Dump ALL Databases (dumpall.sh) |
#!/bin/bash
server='localhost' port=9999 user='root' pwd='root1122'
backupdir=/tmp/backup/all/dump
/usr/bin/mongodump -h $server:$port -u $user -p $pwd --authenticationDatabase admin -o $backupdir
|
OR
Dump ALL Databases (dumpall.sh) |
#!/bin/bash
server='localhost' port=9999 user='root' pwd='root1122'
backupdir=/tmp/backup/all/dump
DBS=$(mongo $server:$port -u $user -p $pwd --authenticationDatabase admin --quiet --eval "db.adminCommand('listDatabases').databases.forEach(function (d) { mdb = db.getSiblingDB(d.name); print(mdb); })")
for DB in $DBS; do if [ $DB != "local" ]; then echo "Exporting $DB ..." /usr/bin/mongodump -h $server:$port -u $user -p $pwd --authenticationDatabase admin -d $DB -o $backupdir fi done |
Restore
Restore all Database (restoreall.sh) |
#!/bin/bash
server='localhost' port=9999 user='root' pwd='root1122'
backupdir=/tmp/backup/all/dump
/usr/bin/mongorestore -h $server:$port -u $user -p $pwd --authenticationDatabase admin $backupdir |
특정 데이터베이스만 Restore 하려면
/usr/bin/mongorestore -h $server:$port -u $user -p $pwd --authenticationDatabase admin -d 3db /tmp/backup/all/dump/3db
특정 컬렉션만 Restore 하려면
/usr/bin/mongorestore -h $server:$port -u $user -p $pwd --authenticationDatabase admin -d tpcc /tmp/backup/all/dump/tpcc/WAREHOUSE.bson
Export
데이터만 받으면서 Collection 레벨로만 받을 수 있다.
그래서 인덱스 생성 구문을 따로 추출해서 작업해 줘야 한다.
Export
Export All Database (exportall.sh) |
#!/bin/bash
server='localhost' port=9999 user='root' pwd='root1122'
backupdir=/tmp/backup/all/export
DBS=$(mongo $server:$port -u $user -p $pwd --authenticationDatabase admin --quiet --eval "db.adminCommand('listDatabases').databases.forEach(function (d) { mdb = db.getSiblingDB(d.name); print(mdb); })")
for DB in $DBS; do COLLECTIONS=$(mongo $server:$port/$DB -u $user -p $pwd --authenticationDatabase admin --quiet --eval "db.getCollectionNames().join('\n')" ) for collection in $COLLECTIONS; do if [ $DB != "admin" ]; then echo "Exporting $DB/$collection ..." /usr/bin/mongoexport -h $server:$port -u $user -p $pwd --authenticationDatabase admin -d $DB -c $collection -o $backupdir/$DB.$collection.json fi done done |
Import
Import All Database (importall.sh) |
#!/bin/bash
server='localhost' port=8888 user='root' pwd='root1122'
backupdir=/tmp/backup/all/export
FILES=$(ls $backupdir)
for file in $FILES; do DB=`echo $file | cut -d'.' -f1` coll=`echo $file | cut -d'.' -f2` ext=`echo $file | cut -d'.' -f3` if [ $ext != "json" ]; then
coll=`echo $file | cut -d'.' -f2`.`echo $file | cut -d'.' -f3` ext=`echo $file | cut -d'.' -f4`
if [ $ext != "json" ]; then
coll=`echo $file | cut -d'.' -f2`.`echo $file | cut -d'.' -f3`.`echo $file | cut -d'.' -f4` ext=`echo "$file" | cut -d'.' -f5`
if [ $ext != "json" ]; then coll=`echo $file | cut -d'.' -f2`.`echo $file | cut -d'.' -f3`.`echo $file | cut -d'.' -f4`.`echo $file | cut -d'.' -f5` fi
fi
fi echo "$DB $coll" /usr/bin/mongoimport --host $server:$port -u $user -p $pwd --authenticationDatabase=admin --db $DB --collection $coll --file $backupdir/$file
done |