fr-itaya / training

0 stars 0 forks source link

データベース - MySQL - バックアップ・リストア #30

Closed fr-sato closed 10 years ago

fr-sato commented 10 years ago

■目的

MySQLコマンドを用いてDBのバックアップ、リストアについて学びます。
課題毎に発行したコマンドをIssueに記述して下さい。

■課題1

MySQLコマンドを用いて作成したDBのバックアップファイルを作成して下さい。

■課題2

MySQLコマンドを用いて課題1で作成したバックアップファイルからDBをリストアして下さい。

fr-itaya commented 10 years ago

本課題は12:40開始、工数見積3時間で行います! よろしくお願いいたします。

fr-itaya commented 10 years ago

■課題1

作成したDBのバックアップファイルを作成

$ mysqldump -u [username] -p [password] -h [hosytname] [DBname] [tableName] [option] > [BackupDestination]
$ mysqldump -u root -p mysql_test > mydb.sql
$ ls -al
total 116
drwx------ 8 ec2-user ec2-user  4096 Jun  5 04:12 .
drwxr-xr-x 3 root     root      4096 Dec 10 23:47 ..
-rw------- 1 ec2-user ec2-user 20051 Jun  5 04:01 .bash_history
-rw-r--r-- 1 ec2-user ec2-user    18 Sep  4  2013 .bash_logout
-rw-r--r-- 1 ec2-user ec2-user  2949 May 29 02:25 .bash_profile
-rw-r--r-- 1 ec2-user ec2-user   124 Sep  4  2013 .bashrc
drwxrwxr-x 3 ec2-user ec2-user  4096 Mar 19 09:49 .composer
drwxrwxr-x 4 ec2-user ec2-user  4096 Jun  5 00:51 .gem
-rw-rw-r-- 1 ec2-user ec2-user    53 Apr 25 07:03 .gitconfig
-rw-rw-r-- 1 ec2-user ec2-user  2874 Jun  5 04:12 mydb.sql
-rw------- 1 ec2-user ec2-user 12559 Jun  5 04:11 .mysql_history
drwxrw---- 3 ec2-user ec2-user  4096 Apr  7 07:05 .pki
drwxrwxr-x 6 ec2-user ec2-user  4096 May  1 02:10 public_html
-rw-rw-r-- 1 ec2-user ec2-user   135 May 29 02:18 .screenrc
drwx------ 2 ec2-user ec2-user  4096 Apr 21 05:17 .ssh
-rwxrwxr-x 1 ec2-user ec2-user  1487 Jun  4 01:17 .tmux.conf
drwxrwxr-x 5 ec2-user ec2-user  4096 Apr 15 06:09 .vim
-rw------- 1 ec2-user ec2-user 14747 Jun  4 09:19 .viminfo
-rwxrwxr-x 1 ec2-user ec2-user  1632 Jun  4 01:51 .vimrc

$ cat mydb.sql
-- MySQL dump 10.13  Distrib 5.6.15, for Linux (x86_64)
--
-- Host: localhost    Database: mysql_test
-- ------------------------------------------------------
-- Server version       5.6.15

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `test02`
--

DROP TABLE IF EXISTS `test02`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `test02` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` text NOT NULL,
  `editor` varchar(16) DEFAULT NULL,
  `created_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `test02`
--

LOCK TABLES `test02` WRITE;
/*!40000 ALTER TABLE `test02` DISABLE KEYS */;
INSERT INTO `test02` VALUES (2,'Permiculation City','Greg Egan','1994-01-01 00:00:00'),(3,'Nighteen Eighty-Four','George Orwell','1972-12-20 00:00:00');
/*!40000 ALTER TABLE `test02` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `test03`
--

DROP TABLE IF EXISTS `test03`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `test03` (
  `id` int(11) DEFAULT NULL,
  `detail` text,
  KEY `id` (`id`),
  CONSTRAINT `test03_ibfk_1` FOREIGN KEY (`id`) REFERENCES `test02` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `test03`
--

LOCK TABLES `test03` WRITE;
/*!40000 ALTER TABLE `test03` DISABLE KEYS */;
INSERT INTO `test03` VALUES (2,'SF novel featured in artificial life and simulated reality'),(3,'This novel portrays a society omnipresent government surveillance and public manipulation');
/*!40000 ALTER TABLE `test03` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2014-06-05  4:12:44

■課題2

バックアップファイルからDBをリストア

mysql> SHOW TABLES;
+----------------------+
| Tables_in_mysql_test |
+----------------------+
| test02               |
| test03               |
+----------------------+
2 rows in set (0.00 sec)

mysql> DROP TABLE test03;
Query OK, 0 rows affected (0.21 sec)

mysql> SHOW TABLES;
+----------------------+
| Tables_in_mysql_test |
+----------------------+
| test02               |
+----------------------+
1 row in set (0.00 sec)
$ mysql -u root -p mysql_test < mydb.sql
mysql> USE mysql_test;
mysql> SHOW TABLES;
+----------------------+
| Tables_in_mysql_test |
+----------------------+
| test02               |
| test03               |
+----------------------+
2 rows in set (0.00 sec)
fr-sato commented 10 years ago

確認しました。バックアップとリストアそれぞれのコマンドはOKです。

リストアの確認ですが、テーブルがあるかだけの確認ではなく、1つテーブルをピックアップしてテーブル構成やデータが復元できているかまで確認してみましょう。

fr-itaya commented 10 years ago

+ リストアの確認

テーブル構成の確認

mysql> DESC test03;
+--------+---------+------+-----+---------+-------+
| Field  | Type    | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+-------+
| id     | int(11) | YES  | MUL | NULL    |       |
| detail | text    | YES  |     | NULL    |       |
+--------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> SHOW CREATE TABLE test03;
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table  | Create Table                                                                                                                                                                                             |
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test03 | CREATE TABLE `test03` (
  `id` int(11) DEFAULT NULL,
  `detail` text,
  KEY `id` (`id`),
  CONSTRAINT `test03_ibfk_1` FOREIGN KEY (`id`) REFERENCES `test02` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

データの確認

mysql> SELECT * FROM test03;
+------+-------------------------------------------------------------------------------------------+
| id   | detail                                                                                    |
+------+-------------------------------------------------------------------------------------------+
|    2 | SF novel featured in artificial life and simulated reality                                |
|    3 | This novel portrays a society omnipresent government surveillance and public manipulation |
+------+-------------------------------------------------------------------------------------------+
2 rows in set (0.01 sec)
fr-sato commented 10 years ago

確認しました、OKです。リストアした場合はテーブル構成、データ復元まで確認しておくと良いかなと思います。