技术开发 频道

DBA应用技巧:利用MySQL技能学习MongoDB

  六、备份和恢复

  在数据库表丢失或损坏的情况下,备份你的数据库是很重要的。如果发生系统崩溃,你肯定想能够将你的表尽可能丢失最少的数据恢复到崩溃发生时的状态。

  1、MySQL备份和恢复

  MySQL备份方式大体上分为以下3种:

    ● 直接拷贝数据库文件
    ● 使用mysqlhotcopy备份数据库
    ● 使用mysqldump备份数据库

  (1)、直接拷贝数据库文件

  最为直接、快速、方便,缺点是基本上不能实现增量备份。为了保证数据的一致性,需要在靠背文件前,执行以下 SQL 语句:

    FLUSH TABLES WITH READ LOCK;

  也就是把内存中的数据都刷新到磁盘中,同时锁定数据表,以保证拷贝过程中不会有新的数据写入。这种方法备份出来的数据恢复也很简单,直接拷贝回原来的数据库目录下即可。

  但对于 Innodb 类型表来说,还需要备份其日志文件,即 ib_logfile* 文件。因为当 Innodb 表损坏时,就可以依靠这些日志文件来恢复。

  (2)、使用mysqlhotcopy备份数据库

  mysqlhotcopy 是perl程序。它使用 LOCK TABLES、FLUSH TABLES 和 cp 或 scp 来快速备份数据库。对于备份数据库或单个表来说它是最快的途径,但它只能运行在本地服务器上,且mysqlhotcopy 只能备份 MyISAM表,对于Innodb表则无招可施了。

  (3)、使用mysqldump备份数据库

  mysqldump 是SQL级别的备份,它将数据表导成 SQL 脚本文件,在不同的 MySQL 版本之间升级时相对比较合适,这也是最主流的备份方法。

  2、MongoDB备份和恢复

  MongoDB提供了两个命令来备份(mongodump )和恢复(mongorestore )数据库。

  (1)、mongodump备份工具

  我们先看一下此工具的帮助信息:

[root@localhost bin]# ./mongodump --help
options:
  --help                   produce help message
  -v [ --verbose ]         be more verbose (include multiple times for more
                           verbosity e.g. -vvvvv)
  -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
  --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         if dbpath specified, each db is in a separate
                           directory
  -d [ --db ] arg          database to use
  -c [ --collection ] arg  collection to use (some commands)
  -o [ --out ] arg (=dump) output directory or "-" for stdout
  -q [ --query ] arg       json query
  --oplog                  Use oplog for point-in-time snapshotting
  --repair                 try to recover a crashed database
[root@localhost bin]#

  例如我们的系统中有一个叫做”foo”库,下面我们将演示如何将这个库备份出来:

[root@localhost bin]# ./mongodump -d foo -o /data/dump
connected to: 127.0.0.1
DATABASE: foo    to     /data/dump/foo
        foo.system.indexes to /data/dump/foo/system.indexes.bson
                 3 objects
        foo.system.users to /data/dump/foo/system.users.bson
                 1 objects
        foo.t2 to /data/dump/foo/t2.bson
                 1 objects
        foo.t1 to /data/dump/foo/t1.bson
                 2 objects
[root@localhost bin]#

  通过工具返回信息,我们可以看到foo中的数据已经被备份成bson格式的文件了, 接下来我们到备份的目录下去验证一下:

[root@localhost dump]# ll /data/dump/foo/
总计 16
-rw-r--r-- 1 root root 193 04-22 11:55 system.indexes.bson
-rw-r--r-- 1 root root  91 04-22 11:55 system.users.bson
-rw-r--r-- 1 root root  66 04-22 11:55 t1.bson
-rw-r--r-- 1 root root  49 04-22 11:55 t2.bson
[root@localhost dump]#

  结果证明foo库中的表已经被成功备份出来,接下来我们将演示如何将备份恢复回去。

  (2)、mongorestore恢复工具

  我们先看一下此工具的帮助信息:

[root@localhost bin]# ./mongorestore --help
usage: ./mongorestore [options] [directory or filename to restore from]
options:
  --help                  produce help message
  -v [ --verbose ]        be more verbose (include multiple times for more
                          verbosity e.g. -vvvvv)
  -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
  --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        if dbpath specified, each db is in a separate
                          directory
  -d [ --db ] arg         database to use
  -c [ --collection ] arg collection to use (some commands)
  --objcheck              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
[root@localhost bin]#

  例如我们先将”foo”库删除了:

[root@localhost bin]# ./mongo
MongoDB shell version: 1.8.1
connecting to: test
> use foo
switched to db foo
> db.dropDatabase();
{ "dropped" : "foo", "ok" : 1 }
> show dbs
admin   0.0625GB
local   (empty)
test    0.0625GB
>

  然后下面我们将演示如何恢复这个库:

[root@localhost bin]# ./mongorestore --directoryperdb /data/dump
connected to: 127.0.0.1
Sun Apr 22 12:01:27 /data/dump/foo/t1.bson
Sun Apr 22 12:01:27      going into namespace [foo.t1]
Sun Apr 22 12:01:27      2 objects found
Sun Apr 22 12:01:27 /data/dump/foo/t2.bson
Sun Apr 22 12:01:27      going into namespace [foo.t2]
Sun Apr 22 12:01:27      1 objects found
Sun Apr 22 12:01:27 /data/dump/foo/system.users.bson
Sun Apr 22 12:01:27      going into namespace [foo.system.users]
Sun Apr 22 12:01:27      1 objects found
Sun Apr 22 12:01:27 /data/dump/foo/system.indexes.bson
Sun Apr 22 12:01:27      going into namespace [foo.system.indexes]
Sun Apr 22 12:01:27 { name: "_id_", ns: "foo.system.users", key: { _id: 1 }, v: 0 }
Sun Apr 22 12:01:27 { name: "_id_", ns: "foo.t2", key: { _id: 1 }, v: 0 }
Sun Apr 22 12:01:27 { name: "_id_", ns: "foo.t1", key: { _id: 1 }, v: 0 }
Sun Apr 22 12:01:27      3 objects found
[root@localhost bin]#

  通过工具返回信息,我们可以看到foo中的数据已经被恢复回来了, 接下来我们到库里去验证一下:

[root@localhost bin]# ./mongo
MongoDB shell version: 1.8.1
connecting to: test
> use foo
switched to db foo
> show collections;
system.indexes
system.users
t1
t2
>

  结果证明foo库表已经被成功恢复回来了。

0
相关文章