码迷,mamicode.com
首页 > 数据库 > 详细

mysql学习之旅-数据库自动备份和手动恢复

时间:2016-09-01 23:10:45      阅读:350      评论:0      收藏:0      [点我收藏+]

标签:


1、导入测试数据库数据

建库
show databases;
CREATE DATBASE easthome;
建表
 CREATE TABLE tj(name varchar(8),tel int(8));
CREATE TABLE sex(name varchar(8),sex int(1));
插入信息
INSERT INTO easthome.tj(name,tel) VALUES(‘LIYAN‘,88888888);
INSERT INTO easthome.tj(name,tel) VALUES(‘zy‘,77777777);
insert into easthome.sex(name,sex) values(‘zy‘,1);
mysql> insert into easthome.sex(name,sex) values(‘LIYAN‘,0);
Query OK, 1 row affected (0.00 sec)
insert into easthome.sex(name,sex) values(‘yangchen‘,1);
 insert into sex(name,sex) values(‘zhangyun‘,0);
mysql> show tables;
+--------------------+
| Tables_in_easthome |
+--------------------+
| sex                |
| tj                 |
+--------------------+
2 rows in set (0.00 sec)

mysql> select * from sex;
+----------+------+
| name     | sex  |
+----------+------+
| zy       |    1 |
| LIYAN    |    0 |
| yangchen |    1 |
| zhangyun |    0 |
+----------+------+
4 rows in set (0.00 sec)

mysql> SELECT * FROM easthome.tj;
+-------+----------+
| name  | tel      |
+-------+----------+
| LIYAN | 88888888 |
| zy    | 77777777 |
+-------+----------+
2 rows in set (0.00 sec)


2、开始备份
mysqldump备份成dump文件
==============
MySQL   安装位置:D:\app\mysql57
数据库名称为:easthome
MySQL root   密码:redhat
数据库备份目的地:D:\db_backup\

脚本:

rem *******************************Code Start*****************************
@echo off
set "Ymd=%date:~,4%%date:~5,2%%date:~8,2%"
D:\app\mysql57\bin\mysqldump.exe  -uroot -predhat --default-character-set=binary -f easthome > D:\db_bakup\easthome_%Ymd%.dump
@echo on
rem *******************************Code End*****************************

将以上代码保存为backup_db.bat   调试时可以在结尾加上pause,查看是否正常执行
然后使用Windows的“计划任务”定时执行该脚本即可。(例如:每天凌晨5点执行back_db.bat)
说明:此方法可以不用关闭数据库,并且可以按每一天的时间来名称备份文件。
通过%date:~5,2%来组合得出当前日期,组合的效果为yyyymmdd,date命令得到的日期格式默认为yyyy-mm-dd(如果不是此格式可以通过pause命令来暂停命令行窗口看通过%date:~,20%得到的当前计算机日期格式),所以通过%date:~5,2%即可得到日期中的第五个字符开始的两个字符,例如今天为2009-02-05,通过%date:~5,2%则可以得到02。(日期的字符串的下标是从0开始的)

3、导入到冷备库中     #在这里卡了半天,原来mysqldump只是导出工具,导入时必须要用mysql

在cmd中登录mysql
mysql -uroot -p
create database mytest
use mytest
source d:\db_bakup\easthome_20160901.dump
show tables;    #检查是否导入成功

效果如下:
mysql> source D:\db_bakup\easthome_20160901.dump
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| sex              |
| tj               |
+------------------+
2 rows in set (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| mytest             |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql学习之旅-数据库自动备份和手动恢复

标签:

原文地址:http://www.cnblogs.com/mathprice/p/5831518.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!