MySQL Backup and Restore

Overview

Key point:

  • Use SELECT … INTO OUTFILE to backup data.
  • Use LOAD DATA INFILE to restore data.
  • A new table can be created and load data to it.

Example

Preparation: Table and data

MariaDB [testdate]> DESCRIBE ttt;
+--------------+-------------+------+-----+-------------------+-------+
| Field        | Type        | Null | Key | Default           | Extra |
+--------------+-------------+------+-----+-------------------+-------+
| others       | varchar(20) | YES  |     | NULL              |       |
| the_datetime | datetime    | YES  |     | CURRENT_TIMESTAMP |       |
+--------------+-------------+------+-----+-------------------+-------+
2 rows in set (0.00 sec)

MariaDB [testdate]> SELECT * FROM ttt;
+--------+---------------------+
| others | the_datetime        |
+--------+---------------------+
| a      | 2016-05-24 14:33:20 |
| a      | 2016-05-24 14:33:30 |
| b      | 2016-05-24 15:31:42 |
| c      | 2016-05-24 15:31:43 |
| d      | 2016-05-24 15:31:44 |
+--------+---------------------+
5 rows in set (0.00 sec)

MariaDB [testdate]>

Backup data to file

MariaDB [testdate]> SELECT * FROM ttt WHERE the_datetime < TimeStamp('2016-05-24 15:31:43');
+--------+---------------------+
| others | the_datetime        |
+--------+---------------------+
| a      | 2016-05-24 14:33:20 |
| a      | 2016-05-24 14:33:30 |
| b      | 2016-05-24 15:31:42 |
+--------+---------------------+
3 rows in set (0.00 sec)

MariaDB [testdate]> SELECT * INTO OUTFILE './all_data.txt' FROM ttt;
Query OK, 5 rows affected (0.00 sec)

MariaDB [testdate]> SELECT * INTO OUTFILE './some_data.txt' FROM ttt WHERE the_datetime < TimeStamp('2016-05-24 15:31:43');
Query OK, 3 rows affected (0.00 sec)

MariaDB [testdate]> SELECT others, the_datetime INTO OUTFILE './some_data2.txt' FROM ttt WHERE the_datetime < TimeStamp('2016-05-24 15:31:43');
Query OK, 3 rows affected (0.00 sec)
MariaDB [testdate]> 

Delete the data

Once the data has been backup successfully, the data can be deleted from the table.

MariaDB [testdate]> DELETE FROM ttt WHERE the_datetime < TimeStamp('2016-05-24 15:31:43');
Query OK, 3 rows affected (0.03 sec)

MariaDB [testdate]> SELECT * FROM ttt;
+--------+---------------------+
| others | the_datetime        |
+--------+---------------------+
| c      | 2016-05-24 15:31:43 |
| d      | 2016-05-24 15:31:44 |
+--------+---------------------+
2 rows in set (0.00 sec)

MariaDB [testdate]>

Restore data to the existing table

MariaDB [testdate]> LOAD DATA INFILE './some_data.txt' INTO TABLE ttt;
Query OK, 3 rows affected (0.08 sec)
Records: 3  Deleted: 0  Skipped: 0  Warnings: 0

MariaDB [testdate]> SELECT * FROM ttt;
+--------+---------------------+
| others | the_datetime        |
+--------+---------------------+
| c      | 2016-05-24 15:31:43 |
| d      | 2016-05-24 15:31:44 |
| a      | 2016-05-24 14:33:20 |
| a      | 2016-05-24 14:33:30 |
| b      | 2016-05-24 15:31:42 |
+--------+---------------------+
5 rows in set (0.04 sec)

MariaDB [testdate]>

Restore to new table

Create a new table with the same structure, and restore data to this new table.

MariaDB [testdate]> CREATE TABLE another_ttt (others VARCHAR(20), the_datetime DATETIME NOT NULL DEFAULT NOW());
Query OK, 0 rows affected (0.25 sec)

MariaDB [testdate]> DESCRIBE another_ttt;
+--------------+-------------+------+-----+-------------------+-------+
| Field        | Type        | Null | Key | Default           | Extra |
+--------------+-------------+------+-----+-------------------+-------+
| others       | varchar(20) | YES  |     | NULL              |       |
| the_datetime | datetime    | NO   |     | CURRENT_TIMESTAMP |       |
+--------------+-------------+------+-----+-------------------+-------+
2 rows in set (0.02 sec)

MariaDB [testdate]> LOAD DATA INFILE './some_data.txt' INTO TABLE another_ttt;
Query OK, 3 rows affected (0.04 sec)
Records: 3  Deleted: 0  Skipped: 0  Warnings: 0

MariaDB [testdate]> SELECT * FROM another_ttt;
+--------+---------------------+
| others | the_datetime        |
+--------+---------------------+
| a      | 2016-05-24 14:33:20 |
| a      | 2016-05-24 14:33:30 |
| b      | 2016-05-24 15:31:42 |
+--------+---------------------+
3 rows in set (0.00 sec)

MariaDB [testdate]>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值