1.备份单个数据库
普通备份:
[sql]
- [root@A_server ~]#mysqldump -u root -p'111111' newjueqi_default>/tmp/newjueqi_default.sql
- [root@A_server ~]# ll /tmp/newjueqi_default.sql
- -rw-r--r-- 1 root root 1908 Apr 19 10:13 /tmp/newjueqi_default.sql
[sql]
- [root@A_server ~]# mysqldump -u root -p'111111' newjueqi_default|gzip>/tmp/newjueqi_default.sql.gz
- [root@A_server ~]# ll /tmp/newjueqi_default.sql.gz
- -rw-r--r-- 1 root root 745 Apr 19 10:13 /tmp/newjueqi_default.sql.gz
[sql]
- mysqldump -u root -p'111111' newjueqi --default-character-set=gbk |gzip >/server/bak/newjueqi.sql.gz
- [root@A_server ~]# mysqldump -u root -p'111111' --default-character-set=gbk newjueqi_default|gzip>/tmp/newjueqi_default.sql.gz
- [root@A_server ~]# ll /tmp/newjueqi_default.sql.gz
- -rw-r--r-- 1 root root 725 Apr 19 10:15 /tmp/newjueqi_default.sql.gz
[sql]
- egrep -v '\*|--|^$' /server/bak/newjueqi.sql
[sql]
- [root@A_server ~]# mysqldump -u root -p'111111' -B newjueqi_default mysql>/tmp/newjueqi_default.sql
[sql]
- [root@A_server ~]# mysqldump -u root -p'111111' newjueqi_default test>/tmp/newjueqi_default.sql
[sql]
- [root@A_server ~]# mysqldump -u root -p'111111' newjueqi_default test test2>/tmp/newjueqi.sql
- [root@A_server ~]# egrep -v '\*|--|^$' /tmp/newjueqi.sql
- DROP TABLE IF EXISTS `test`;
- CREATE TABLE `test` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `name` varchar(200) NOT NULL DEFAULT '',
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
- LOCK TABLES `test` WRITE;
- INSERT INTO `test` VALUES (1,'tom'),(2,'newjueqi');
- UNLOCK TABLES;
- DROP TABLE IF EXISTS `test2`;
- CREATE TABLE `test2` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
- LOCK TABLES `test2` WRITE;
- INSERT INTO `test2` VALUES (1);
- UNLOCK TABLES
[sql]
- [root@A_server ~]# mysqldump -u root -p'111111' -d newjueqi_default test test2>/tmp/newjueqi.sql
- [root@A_server ~]# egrep -v '\*|--|^$' /tmp/newjueqi.sql
- DROP TABLE IF EXISTS `test`;
- CREATE TABLE `test` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `name` varchar(200) NOT NULL DEFAULT '',
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
- DROP TABLE IF EXISTS `test2`;
- CREATE TABLE `test2` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
6. 打包备份
[sql]
- mysqldump -uroot -p'111111' -S /data/3306/mysql.sock -A -B|gzip>/server/backup/mysql_bak.$(date +%F).sql.gz
-A:备份全部数据库
-B: 使用use,drop