mysqldump備份及恢復

 

 
  1. mysqldump增量備份: 
  2. 基於時間點備份恢復方式 
  3. [root@centos 3306]# mysql -uroot -p -S /data/3306/mysql.sock  
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 5.0.56-log Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
 
  1. mysql> use oldboy; 
Database changed
 
  1. mysql> set names gbk; 
Query OK, 0 rows affected (0.00 sec)
mysql> create table student(
    -> Sno int(10) NOT NULL COMMENT '學號',
    -> Sname varchar(16) NOT NULL COMMENT '姓名',
    -> Ssex char(2) NOT NULL COMMENT '性別',
    -> Sage tinyint(2)  NOT NULL default '0' COMMENT '學生年齡',
    -> Sdept varchar(16)  default NULL  COMMENT '學生所在系別', 
    -> PRIMARY KEY  (Sno) 
    -> ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=gbk;
Query OK, 0 rows affected (0.01 sec)
 
  1. mysql> INSERT INTO student values(0001,'陸亞','男',24,'計算機網絡'); 
Query OK, 1 row affected (0.00 sec)
 
  1. mysql> INSERT INTO student values(0002,'kqzj ','男',26,'computer application'); 
Query OK, 1 row affected, 1 warning (0.00 sec)
 
  1. mysql> INSERT INTO student values(0003,'xiaozhang','男',28,'物流管理'); 
Query OK, 1 row affected (0.00 sec)
 
  1. mysql> INSERT INTO student values(0004,'脈動','男',29,'computer application'); 
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> \q  
Bye
 
  1. [root@centos 3306]# mysqldump -u root -p'oldboy' -S /data/3306/mysql.sock --lock-all-tables --default-character-set=gbk -F -B  oldboy|gzip >/tmp/oldboy_$(date +%F).sql.gz   ##執行一次全備 
[root@centos bak]# mysql -uroot -p -S /data/3306/mysql.sock 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 5.0.56-log Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use oldboy
Database changed
mysql> set names gbk;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO student values(0005,'ydds','男',26,'計算機科學與技術');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO student values(0090,'yazhe','男',22,'計算機科學與技術');
ERROR 1146 (42S02): Table 'test.student' doesn't exist
mysql> INSERT INTO student values(0090,'yazhe','男',22,'計算機科學與技術');     
Query OK, 1 row affected (0.00 sec)
mysql> \q
Bye
[root@centos 3306]# mkdir /bak/
[root@centos 3306]# ll
total 172
drwxr-xr-x 6 mysql mysql 4096 Apr 15 02:33 data
-rw-rw---- 1 mysql mysql    0 Apr 15 02:46 error.log
-rw-rw---- 1 mysql mysql    0 Apr 15 02:34 error.log-old
-rw-r--r-- 1 mysql mysql 1882 Apr 15 01:50 my.cnf
-rwx------ 1 mysql mysql  836 Apr 14 21:13 mysql
-rw-rw---- 1 mysql mysql  117 Apr 14 21:14 mysql-bin.000001
-rw-rw---- 1 mysql mysql  391 Apr 14 22:03 mysql-bin.000002
-rw-rw---- 1 mysql mysql  254 Apr 14 22:34 mysql-bin.000003
-rw-rw---- 1 mysql mysql  117 Apr 14 22:43 mysql-bin.000004
-rw-rw---- 1 mysql mysql 1877 Apr 15 00:09 mysql-bin.000005
-rw-rw---- 1 mysql mysql  117 Apr 15 01:51 mysql-bin.000006
-rw-rw---- 1 mysql mysql  364 Apr 15 02:46 mysql-bin.index
-rw-rw---- 1 mysql mysql    5 Apr 15 01:55 mysqld.pid
srwxrwxrwx 1 mysql mysql    0 Apr 15 01:55 mysql.sock
-rw-rw---- 1 mysql root   473 Apr 15 01:55 ryan_mysql.err
-rw-rw---- 1 mysql mysql 2405 Apr 15 02:46 slow.log
 
  1. [root@centos 3306]# mysqldump -u root -p'oldboy' -S /data/3306/mysql.sock --lock-all-tables --default-character-set=gbk -F -B  oldboy|gzip >/tmp/oldboy_$(date +%F).sql.gz    
[root@centos 3306]# ll
total 180
drwxr-xr-x 6 mysql mysql 4096 Apr 15 02:33 data
-rw-rw---- 1 mysql mysql    0 Apr 15 02:47 error.log
-rw-rw---- 1 mysql mysql    0 Apr 15 02:46 error.log-old
-rw-r--r-- 1 mysql mysql 1882 Apr 15 01:50 my.cnf
-rwx------ 1 mysql mysql  836 Apr 14 21:13 mysql
-rw-rw---- 1 mysql mysql  117 Apr 14 21:14 mysql-bin.000001
-rw-rw---- 1 mysql mysql  391 Apr 14 22:03 mysql-bin.000002
-rw-rw---- 1 mysql mysql  254 Apr 14 22:34 mysql-bin.000003
-rw-rw---- 1 mysql mysql  117 Apr 14 22:43 mysql-bin.000004
-rw-rw---- 1 mysql mysql 1877 Apr 15 00:09 mysql-bin.000005
-rw-rw---- 1 mysql mysql  117 Apr 15 01:51 mysql-bin.000006
-rw-rw---- 1 mysql mysql  117 Apr 15 01:54 mysql-bin.000007
-rw-rw---- 1 mysql mysql  392 Apr 15 02:47 mysql-bin.index
-rw-rw---- 1 mysql mysql    5 Apr 15 01:55 mysqld.pid
srwxrwxrwx 1 mysql mysql    0 Apr 15 01:55 mysql.sock
-rw-rw---- 1 mysql root   473 Apr 15 01:55 ryan_mysql.err
-rw-rw---- 1 mysql mysql 2590 Apr 15 02:47 slow.log
[root@centos 3306]# mysql -uroot -p -S /data/3306/mysql.sock 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 5.0.56-log Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use oldboy
Database changed
mysql> set names gbk;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO student values(0190,'yazhe','男',22,'計算機科學與技術');      
Query OK, 1 row affected (0.00 sec)
mysql> drop database oldboy;
Query OK, 1 row affected (0.00 sec)
mysql> \q
Bye
[root@centos 3306]# ll
total 180
drwxr-xr-x 5 mysql mysql 4096 Apr 15 02:49 data
-rw-rw---- 1 mysql mysql    0 Apr 15 02:47 error.log
-rw-rw---- 1 mysql mysql    0 Apr 15 02:46 error.log-old
-rw-r--r-- 1 mysql mysql 1882 Apr 15 01:50 my.cnf
-rwx------ 1 mysql mysql  836 Apr 14 21:13 mysql
-rw-rw---- 1 mysql mysql  117 Apr 14 21:14 mysql-bin.000001
-rw-rw---- 1 mysql mysql  391 Apr 14 22:03 mysql-bin.000002
-rw-rw---- 1 mysql mysql  254 Apr 14 22:34 mysql-bin.000003
-rw-rw---- 1 mysql mysql  117 Apr 14 22:43 mysql-bin.000004
-rw-rw---- 1 mysql mysql 1877 Apr 15 00:09 mysql-bin.000005
-rw-rw---- 1 mysql mysql  117 Apr 15 01:51 mysql-bin.000006
-rw-rw---- 1 mysql mysql  117 Apr 15 01:54 mysql-bin.000007
-rw-rw---- 1 mysql mysql  392 Apr 15 02:47 mysql-bin.index
-rw-rw---- 1 mysql mysql    5 Apr 15 01:55 mysqld.pid
srwxrwxrwx 1 mysql mysql    0 Apr 15 01:55 mysql.sock
-rw-rw---- 1 mysql root   473 Apr 15 01:55 ryan_mysql.err
-rw-rw---- 1 mysql mysql 2590 Apr 15 02:47 slow.log
 
  1. [root@centos 3306]# cp mysql-bin.000007 /bak/    ##拷貝文件到/bak目錄下 
  2. [root@centos 3306]# cp /tmp/oldboy_2012-04-15.sql.gz /bak    ##拷貝全備文件到/bak目錄下 
[root@centos 3306]# cd /bak/
[root@centos bak]# ls
mysql-bin.000011  mysql-bin.000012  mysql-bin.000014  mysql-bin.sql  oldboy_2012-04-15.sql  oldboy_2012-04-15.sql.gz
 
  1. [root@centos bak]# gzip -d oldboy_2012-04-16.sql.gz      ##解壓全備份的文件 
  2. [root@centos bak]# mysql -uroot -p -S /data/3306/mysql.sock     ##登陸3306庫 
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@centos bak]# mysql -uroot -p -S /data/3306/mysql.sock </bak/oldboy_2012-04-15.sql 
Enter password: 
 
  1. [root@centos bak]# vi oldboy_2012-04-16.sql      ##這個是我們進行全備份的mysql-bin轉化過sql的文件 
-- MySQL dump 10.11
--
-- Host: localhost    Database: oldboy
-- ------------------------------------------------------
-- Server version       5.0.56-log
/*!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 gbk */;
/*!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 */;
--
-- Current Database: `oldboy`
--
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `oldboy` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `oldboy`;
--
-- Table structure for table `student`
--
DROP TABLE IF EXISTS `student`;
SET @saved_cs_client     = @@character_set_client;
SET character_set_client = utf8;
CREATE TABLE `student` (
 
  1. [root@centos bak]# vi mysql-bin.000014   ##看這裏面都是亂碼,所以我們要把它轉化成可讀的sql文件 
鎂binW脝<89>O^OQ^@^@^@^^@^@^@b^@^@^@^A^@^D^@5.0.56-log^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^S8^M^@^H^@^R^@^D^D^D^D^R^@K^@^D^Z樓脝<89>O^BQ^@^@^@F^@^@^@簍^@^@^@^@^@^X^@^@^@^@^@^@^@^F^@^@^Z^@^@^@@^@^@^A^@^@^@^@^@^@^@^@^F^Cstd^D^\^@^\^@^H^@oldboy^@BEGIN樓脝<89>O^BQ^@^@^@<84>^@^@^@,^A^@^@^^@^@^@^@^@^@^@^F^@^@^Z^@^@^@@^@^@^A^@^@^@^@^@^@^@^@^F^Cstd^D^\^@^\^@^H^@oldboy^@INSERT INTO student values(0190,'yazhe','脛脨',22,'錄脝脣茫祿煤驢脝脩摟脫毛彔彔脢玫')樓@^@G^A^@^@^@^@陋^@^@^@^@^@^@^@戮脝<89>O^BQ^@^@^@U^@^@^@<9c>^A^@^@^H^@^X^@^@^@^@^@^@^@^F^@^@^Z^@^@^@@^@^@^A^@^@^@^@^@^@^@^@^F^Cstd^D^\^@^\^@^H^@oldboy^@drop database old~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                                                                                                                                                                                           
 
  1. [root@centos bak]# mysqlbinlog mysql-bin.000007 >mysql-bin.sql  ##把bin文件轉化爲sql文件 
[root@centos bak]# ls
mysql-bin.000007  mysql-bin.sql  oldboy_2012-04-16.sql                                                                                                                                                                     
 
  1. [root@centos bak]# vi mysql-bin.sql     ##編輯這個文件 
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#120416 20:05:39 server id 11  end_log_pos 98   Start: binlog v 4, server v 5.0.
56-log created 120416 20:05:39
# Warning: this binlog was not closed properly. Most probably mysqld crashed wri
ting it.
# at 98
#120416 20:07:12 server id 11  end_log_pos 168  Query   thread_id=7     exec_tim
e=0     error_code=0
use oldboy/*!*/;
SET TIMESTAMP=1334578032/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.uniq
ue_checks=1/*!*/;
SET @@session.sql_mode=0/*!*/;
/*!\C latin1 *//*!*/;
SET @@session.character_set_client=8,@@session.collation_connection=8,@@session.
collation_server=8/*!*/;
BEGIN
/*!*/;
# at 168
@                                                                               
"mysql-bin.sql" 46L, 1777C                                    1,1           Top
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#120416 20:05:39 server id 11  end_log_pos 98   Start: binlog v 4, server v 5.0.56-log created 120416 20:05:39
# Warning: this binlog was not closed properly. Most probably mysqld crashed writing it.
# at 98
#120416 20:07:12 server id 11  end_log_pos 168  Query   thread_id=7     exec_time=0     error_code=0
use oldboy/*!*/;
SET TIMESTAMP=1334578032/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1/*!*/;
SET @@session.sql_mode=0/*!*/;
/*!\C latin1 *//*!*/;
SET @@session.character_set_client=8,@@session.collation_connection=8,@@session.collation_server=8/*!*/;
BEGIN
/*!*/;
# at 168
#120416 20:07:12 server id 11  end_log_pos 308  Query   thread_id=7     exec_time=0     error_code=0
SET TIMESTAMP=1334578032/*!*/;
INSERT INTO student values(0005,'ydds','男',26,'計算機科學與技術')
/*!*/;
# at 308
#120416 20:07:12 server id 11  end_log_pos 335  Xid = 55
COMMIT/*!*/;
# at 335
#120416 20:10:25 server id 11  end_log_pos 405  Query   thread_id=8     exec_time=0     error_code=0
SET TIMESTAMP=1334578225/*!*/;
BEGIN
/*!*/;
# at 405
#120416 20:10:25 server id 11  end_log_pos 546  Query   thread_id=8     exec_time=0     error_code=0
SET TIMESTAMP=1334578225/*!*/;
INSERT INTO student values(0090,'yazhe','男',22,'計算機科學與技術')
/*!*/;
# at 546
#120416 20:10:25 server id 11  end_log_pos 573  Xid = 67
COMMIT/*!*/;
# at 573
#120416 20:11:45 server id 11  end_log_pos 658  Query   thread_id=9     exec_time=0     error_code=0
SET TIMESTAMP=1334578305/*!*/;
 
  1. drop database oldboy     ##找到這句話,要不每次恢復後都執行drop就相當於每有恢復 
/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
 
  1. [root@centos bak]# mysql -uroot -p -S /data/3306/mysql.sock </bak/mysql-bin.sql  ##這裏進行恢復 
Enter password: 
 
  1. [root@centos bak]# mysql -uroot -p -S /data/3306/mysql.sock      ##登陸3306庫 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 28
Server version: 5.0.56-log Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use oldboy;
Database changed
mysql> select * from student;
+-----+-----------+------+------+------------------+
| Sno | Sname     | Ssex | Sage | Sdept            |
+-----+-----------+------+------+------------------+
|   1 | ??        | ?    |   24 | ?????            | 
|   2 | kqzj      | ?    |   26 | computer applica | 
|   3 | xiaozhang | ?    |   28 | ????             | 
|   4 | ??        | ?    |   29 | computer applica | 
|   5 | ydds      | ?    |   26 | ????????         | 
|  90 | yazhe     | ?    |   22 | ????????         | 
| 190 | yazhe     | ?    |   22 | ????????         | 
+-----+-----------+------+------+------------------+
7 rows in set (0.00 sec)
mysql> set names gbk;
Query OK, 0 rows affected (0.00 sec)
 
  1. mysql> select * from student;    ##看看是不是都備份回來啦,至此增量備份結束。 
+-----+-----------+------+------+------------------+
| Sno | Sname     | Ssex | Sage | Sdept            |
+-----+-----------+------+------+------------------+
|   1 | 陸亞      | 男   |   24 | 計算機網絡       | 
|   2 | kqzj      | 男   |   26 | computer applica | 
|   3 | xiaozhang | 男   |   28 | 物流管理         | 
|   4 | 脈動      | 男   |   29 | computer applica | 
|   5 | ydds      | 男   |   26 | 計算機科學與技術 | 
|  90 | yazhe     | 男   |   22 | 計算機科學與技術 | 
| 190 | yazhe     | 男   |   22 | 計算機科學與技術 | 
+-----+-----------+------+------+------------------+
7 rows in set (0.00 sec)
 
 
  1. -------------------------------------------------------------------------------------------------------------------- 
  2. 如果有多個庫: 
  3. mysqlbinlog mysqlbin.000021 -d oldboy -r oldboy.sql 
  4. -d ##基於具體庫的恢復 
  5. 其他方法: 
  6. 常見命令組合: 
  7. (a) 
  8. mysqlbinlog mysqlbin.000021 --start-datetime='2011-03-19 02:58:54' --stop-datetime='2011-03-19 03:22:44' -r time.sql 
  9. 上面語句將顯示2011-03-19 02:58:54---2011-03-19 03:22:44時間段的binlog,並輸出到time.sql. 
  10. (b) 
  11. mysqlbinlog mysqlbin.000021 --start-datetime='2011-03-19 02:58:54' -d test -r time.sql 
  12. 這個語句只有開始時間,那麼就是從2011-03-19 02:58:54時刻到日誌結尾,test數據庫的binlog輸出到time.sql 
  13. (c) 
  14. mysqlbinlog mysqlbin.000021 --stop-datetime='2011-03-19 03:22:44' -d oldboy -s  
  15. 這個語句只有結束時間,那麼就是從日誌開頭到2011-03-19 03:22:44時刻截止,oldboy數據庫的binlog輸出. 
  16. -------------------------------------------------------------------------------------------------------------------- 
 
 
  1. 基於位置點的恢復: 
  2. ※※※※※ 
  3.   --start-position=#  Start reading the binlog at position N. Applies to the 
  4.                       first binlog passed on the command line. 
  5.   --stop-position=#   Stop reading the binlog at position N. Applies to the 
  6.                       last binlog passed on the command line. 
  7. 參數說明:以上兩個參數顯示指定position點內的binlog日誌內容,是非常重要的選項。 
  8. 這個兩個選項比時間選項恢復要更精確 
  9. (a) 
  10. mysqlbinlog mysqlbin.000021 --start-position=510 --stop-position=1312 -r pos.sql 
  11. 輸出 初始位置510,結束位置1312的所有binlog日誌到pos.sql 
  12. 注意:結尾的日誌點比較特殊 不會被包含。即輸出1312pos以前的binlog。 
  13. (b) 
  14. mysqlbinlog mysqlbin.000021 --start-position=510 -r pos510-end.sql 
  15. 輸出 初始位置510,結束位置到文件結尾的所有binlog到pos510-end.sql,當然,你也可以指定庫名輸出binlog,如: 
  16. mysqlbinlog mysqlbin.000021 --start-position=510 -r pos510-end-oldboy.sql -d oldboy 
  17. (c) 
  18. mysqlbinlog mysqlbin.000021 --stop-position=954 -r start-954.sql 
  19. 輸出從文件開始位置,到954位置截止的所有binlog. 
  20. 起始點:# at 510 
  21. # at 510 
  22. #110319  2:58:54 server id 1  end_log_pos 578   Query   thread_id=7     exec_time=0     error_code=0 
  23. 結束點:# at 1312 
  24. # at 1312 
  25. #110319  3:22:44 server id 1  end_log_pos 1339  Xid = 81 
  26. mysqlbinlog mysqlbin.000021 --start-position=510 --stop-position=1312 -r pos.sql 
 
  1. ※※※※※※※※※※※※※※ 
  2. ※1 基於時間點 ※ 
  3. ※2 把中間的一兩分鐘去掉
  4. ※3 基於位置點 ※ 
  5. ※※※※※※※※※※※※※※ 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章