■概要
|
MySQLデータベースの自動バックアップを行なう。 ここでは、MySQL全データベースをサーバー内の別ディレクトリへバックアップする。 |
■バックアップ設定
| (1)バックアップスクリプト作成 |
|
|
(2)バックアップ確認 |
|
|
(3)バックアップ定期自動実行設定 |
|
■バックアップ・リストア確認(削除されたデータベースの復元)
|
削除されたデータベースをバックアップからリストア(復元)できるか確認する (1)テスト用データベース作成 |
[root@centos ~]# mysql -u root -p ← MySQLへrootでログイン Enter password: ← MySQLのrootパスワード応答 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 28 to server version: 4.1.12 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> create database test; ← testデータベース作成 Query OK, 1 row affected (0.00 sec) mysql> use test ← testデータベースへ接続 Database changed mysql> create table test(num int, name varchar(50)); ← testテーブル作成 Query OK, 0 rows affected (0.00 sec) mysql> insert into test values(1,'山田太郎'); ← データ登録 Query OK, 1 row affected (0.00 sec) mysql> select * from test; ← データ確認 +------+----------+ | num | name | +------+----------+ | 1 | 山田太郎 | +------+----------+ 1 row in set (0.00 sec) mysql> exit ← ログアウト Bye |
|
(2)テスト用データベースバックアップ |
[root@centos ~]# ./mysql-backup.sh ← MySQLデータベースバックアップスクリプト実行
|
|
(3)テスト用データベース削除 |
[root@centos ~]# mysql -u root -p ← MySQLへrootでログイン Enter password: ← MySQLのrootパスワード応答 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 35 to server version: 4.1.12 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> use test ← testデータベースへ接続 Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> drop table test; ← testテーブル削除 Query OK, 0 rows affected (0.01 sec) mysql> drop database test; ← testデータベース削除 Query OK, 0 rows affected (0.00 sec) mysql> show databases; ← データベース削除確認 +----------+ | Database | +----------+ | mysql | +----------+ 1 rows in set (0.00 sec) mysql> exit ← ログアウト Bye |
|
(4)テスト用データベース復元 |
[root@centos ~]# /bin/cp -Rf /backup/mysql/test/ /var/lib/mysql/ ← バックアップよりtestデータベースをコピー [root@centos ~]# chown -R mysql:mysql /var/lib/mysql/test/ ← testデータベースの所有者をmysqlに変更 [root@centos ~]# chmod 700 /var/lib/mysql/test/ ← testデータベースのパーミッションを700に変更 [root@centos ~]# chmod 660 /var/lib/mysql/test/* ← testデータベース内データのパーミッションを660に変更 |
|
(5)テスト用データベース復元確認 |
[root@centos ~]# mysql -u root -p ← MySQLへrootでログイン Enter password: ← MySQLのrootパスワード応答 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 38 to server version: 4.1.12 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> show databases; ← testデータベース復元確認 +----------+ | Database | +----------+ | mysql | | test | +----------+ 2 rows in set (0.00 sec) mysql> use test ← testデータベースへ接続 Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; ← testテーブル復元確認 +----------------+ | Tables_in_test | +----------------+ | test | +----------------+ 1 row in set (0.00 sec) mysql> select * from test; ← データ復元確認 +------+----------+ | num | name | +------+----------+ | 1 | 山田太郎 | +------+----------+ 1 row in set (0.00 sec) mysql> drop table test; ← testテーブル削除 Query OK, 0 rows affected (0.00 sec) mysql> drop database test; ← testデータベース削除 Query OK, 0 rows affected (0.00 sec) mysql> show databases; ← testデータベース削除確認 +----------+ | Database | +----------+ | mysql | +----------+ 1 rows in set (0.00 sec) mysql> exit ← ログアウト Bye |
■バックアップ・リストア確認(変更されたデータベースの復元)
|
変更されたデータベースをバックアップからリストア(復元)できるか確認する (1)テスト用データベース作成 |
[root@centos ~]# mysql -u root -p ← MySQLへrootでログイン Enter password: ← MySQLのrootパスワード応答 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 18 to server version: 4.1.12 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> create database test; ← testデータベース作成 Query OK, 1 row affected (0.00 sec) mysql> use test ← testデータベースへ接続 Database changed mysql> create table test(num int, name varchar(50)); ← testテーブル作成 Query OK, 0 rows affected (0.00 sec) mysql> insert into test values(1,'山田太郎'); ← データ登録 Query OK, 1 row affected (0.00 sec) Query OK, 1 row affected (0.00 sec) mysql> select * from test; ← データ確認 +------+----------+ | num | name | +------+----------+ | 1 | 山田太郎 | +------+----------+ 1 row in set (0.00 sec) mysql> exit ← ログアウト Bye |
|
(2)テスト用データベースバックアップ |
[root@centos ~]# ./mysql-backup.sh ← MySQLデータベースバックアップスクリプト実行
|
|
(3)テスト用データベース変更 |
[root@centos ~]# mysql -u root -p ← MySQLへrootでログイン Enter password: ← MySQLのrootパスワード応答 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 25 to server version: 4.1.12 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> use test ← testデータベースへ接続 Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> update test set name='山田次郎'; ← データ変更 Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from test; ← データ確認 +------+----------+ | num | name | +------+----------+ | 1 | 山田次郎 | +------+----------+ 1 row in set (0.00 sec) mysql> exit ← ログアウト Bye |
|
(4)テスト用データベース復元 |
[root@centos ~]# /bin/cp -Rf /backup/mysql/test/ /var/lib/mysql/ ← バックアップよりtestデータベースをコピー
|
|
(5)テスト用データベース復元確認 |
[root@centos ~]# mysql -u root -p ← MySQLへrootでログイン Enter password: ← MySQLのrootパスワード応答 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 26 to server version: 4.1.12 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> use test ← testデータベースへ接続 Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from test; ← データ復元確認 +------+----------+ | num | name | +------+----------+ | 1 | 山田太郎 | +------+----------+ 1 row in set (0.00 sec) mysql> drop table test; ← testテーブル削除 Query OK, 0 rows affected (0.00 sec) mysql> drop database test; ← testデータベース削除 Query OK, 0 rows affected (0.00 sec) mysql> show databases; ← testデータベース削除確認 +----------+ | Database | +----------+ | mysql | +----------+ 1 rows in set (0.00 sec) mysql> exit ← ログアウト Bye |