windows下面mongodb數據庫備份和恢復
我可以講數據備份到c:\data\dump目錄下面,首先創建這個路徑。然後進入到mongodb的bin目錄下面
我的是:
C:\Program Files\mongodb\bin
備份腳本是:
//備份 mongodump -h 127.0.0.1:27017 -d test -o c:\data\dump
恢復腳本是:
//恢復 mongorestore -h 127.0.0.1:27017 -d test --directoryperdb c:\data\dump\test
解釋一下用到的命令
原始解釋:
-v [ --verbose ] be more verbose (include multiple times for more verbosity e.g. -vvvvv) --version print the program's version and exit -h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for sets) --port arg server port. Can also use --host hostname:port --ipv6 enable IPv6 support (disabled by default) -u [ --username ] arg username -p [ --password ] arg password --authenticationDatabase arg user source (defaults to dbname) --authenticationMechanism arg (=MONGODB-CR) authentication mechanism --dbpath arg directly access mongod database files in the given path, instead of connecting to a mongod server - needs to lock the data directory, so cannot be used if a mongod is currently accessing the same path --directoryperdb each db is in a separate directly (relevant only if dbpath specified) --journal enable journaling (relevant only if dbpath specified) -d [ --db ] arg database to use -c [ --collection ] arg collection to use (some commands) --objcheck validate object before inserting (default) --noobjcheck don't validate object before inserting --filter arg filter to apply before inserting --drop drop each collection before import --oplogReplay replay oplog for point-in-time restore --oplogLimit arg include oplog entries before the provided Timestamp (seconds[:ordinal]) during the oplog replay; the ordinal value is optional --keepIndexVersion don't upgrade indexes to newest version --noOptionsRestore don't restore collection options --noIndexRestore don't restore indexes --w arg (=0) minimum number of replicas per write
linux下面mongodb數據庫備份和恢復
linux下面我們可以創建一個自動備份腳本,可以設置定時任務,也可以手動備份。我是手動備份的。
首先創建一個sh命令,我是放在home下面的。
vim /home/mongoBeiFen.sh
輸入如下內容:
#!/bin/bash shijie=`date +%Y%m%d%H` backmongodbFile=mongodb$shijie.tar.gz cd /home/mongoDbback/ /usr/local/mongo/bin/mongodump -h 127.0.0.1 --port 27017 -u mongo -p 123456 -d my_mongodb -o my_mongodb_dump/ tar czf $backmongodbFile my_mongodb_dump/ rm my_mongodb_dump -rf
解釋:
存放備份的文件夾是/home/mongoDbback/
-u是數據庫名用戶名 -p是密碼 -d是數據庫名 具體和window差不多,大家可以看下上面windows的解釋。
備份的時候只要運行一下
./mongoBeiFen.sh
就可以了。
數據庫恢復:
/usr/local/mongo/bin/mongorestore -d my_mongodb my_mongodb_dump/my_mongodb/*
指向每個文件
復制代碼 代碼如下:
/usr/local/mongo/bin/mongorestore -h 127.0.0.1 --port 27017 -- drop --directoryperdb my_mongodb_dump/my_mongodb
指向一個目錄
有問題的話可以嘗試window的寫法:
復制代碼 代碼如下:
/usr/local/mongo/bin/mongorestore -h 127.0.0.1:27017 -d test --drop --directoryperdb my_mongodb_dump/my_mongodb