原本使用操作系统的快照进行备份还原,备份成功后,还原没有成功(参考:Backup and Restore with Filesystem Snapshots)
所以这个方法就先不记录到这里了。
当前测试以下?种备份还原方法(个人初学理解):
1. 使用拷贝和替换数据库文件进行备份还原
2. 使用mongodump和mongorestore
3. 使用mongoimport 和 mongoexport
1. 使用拷贝和替换数据库文件进行备份还原(有些危险又不好)
1.1 备份
a. 在mongodb中执行db.fsyncLock(),刷新数据写入磁盘并锁住整个实例:
?
>db.fsyncLock()b. 打包压缩整个数据库目录(也可以打包部分数据库),作为备份:
[root@localhost ~]# tar -cvzf /root/mongodb_20150505.tar.gz /var/lib/mongoc. 在mongodb中执行db.fsyncUnlock()解锁,备份步骤完成!~
?
?
>db.fsyncUnlock()
?
1.1 还原a. 在mongodb中关闭服务(或者在操作系统层面关闭mongod服务):
?
>use admin >db.shutdownServer()
?
b. 将mongo数据文件删除!注意确认备份存在且正常!~否则回天无力!
[root@localhost ~]# rm -rf /var/lib/mongo/*c. 解压备份的文件到根目录下,相当于还原:
[root@localhost ~]# tar -xvzf /root/mongodb_20150505.tar.gz -C /d. 如果启动不了服务,先删除文件mongod.lock:
?
?
[root@localhost ~]# rm -f /var/lib/mongo/mongod.locke. 启动服务,正常访问。
?
?
[root@localhost ~]# service mongod start
2. 使用mongodump和mongorestore
?
a. 简单备份还原本地数据库的的方法,备份所有及还原所有:
?
mongodump mongorestore /root/dump
?
备份完成后在当前目录将生成一个文件夹”dump“:

?
b. 还原单个数据库时,指定要还原的数据库及其备份目录:
?
mongorestore --db test /root/dump/test还可以指定输出路径,使用计算机名:
?
?
mongodump --host localhost.localdomain --port 27017 --out /root/mongodump-2015-05-05 mongorestore --port 27017 --db test /root/mongodump-2015-05-05/test远程备份时指定用户名密码(这个没尝试,参考官方例子):
?
?
mongodump --host mongodb1.example.net --port 3017 --username user --password pass --out /opt/backup/mongodump-2013-10-24 mongorestore --host mongodb1.example.net --port 3017 --username user --password pass /opt/backup/mongodump-2013-10-24
c. 更多参考:执行 #mongorestore --help
?
?
# mongorestore --help
general options:
--help print usage
--version print the tool version and exit
verbosity options:
-v, --verbose more detailed log output (include multiple times for more verbosity, e.g. -vvvvv)
--quiet hide all log output
connection options:
-h, --host= mongodb host to connect to (setname/host1,host2 for replica sets)
--port= server port (can also use --host hostname:port)
ssl options:
--ssl connect to a mongod or mongos that has ssl enabled
--sslCAFile= the .pem file containing the root certificate chain from
the certificate authority
--sslPEMKeyFile= the .pem file containing the certificate and key
--sslPEMKeyPassword= the password to decrypt the sslPEMKeyFile, if necessary
--sslCRLFile= the .pem file containing the certificate revocation list
--sslAllowInvalidCertificates bypass the validation for server certificates
--sslAllowInvalidHostnames bypass the validation for server name
--sslFIPSMode use FIPS mode of the installed openssl library
authentication options:
-u, --username= username for authentication
-p, --password= password for authentication
--authenticationDatabase= database that holds the user's credentials
--authenticationMechanism= authentication mechanism to use
namespace options:
-d, --db= database to use
-c, --collection= collection to use
input options:
--objcheck validate all objects before inserting
--oplogReplay replay oplog for point-in-time restore
--oplogLimit= only include oplog entries before the provided Timestamp(seconds[:ordinal])
--restoreDbUsersAndRoles restore user and role definitions for the given database
--dir= input directory, use '-'