2025年tidb数据库备份(数据库备份dbs)

tidb数据库备份(数据库备份dbs)blockquote 欢迎来到我的博客 很高兴能够在这里和您见面 希望您在这里可以感受到一份轻松愉快的氛围 不仅可以获得有趣的内容和知识 也可以畅所欲言 分享您的想法和见解 推荐 Linux 运维老纪的首页 持续学习 不断总结 共同进步 活到老学到老 全面总结 IT 核心技术 系统基础 数据库 网路技术 系统安全 自动化运维 容器技术 监控工具 脚本编程 云计算 blockquote

大家好,我是讯享网,很高兴认识大家。



 <blockquote> 

讯享网

💝💝💝欢迎来到我的博客,很高兴能够在这里和您见面!希望您在这里可以感受到一份轻松愉快的氛围,不仅可以获得有趣的内容和知识,也可以畅所欲言、分享您的想法和见解。


讯享网

推荐:Linux运维老纪的首页,持续学习,不断总结,共同进步,活到老学到老
全面总结 IT核心技术:系统基础、数据库、网路技术、系统安全、自动化运维、容器技术、监控工具、脚本编程、云计算、人工智能、运维开发、算法结构、物联网、JAVA Python语言等。
不同类型针对性训练,提升编程思维,剑指大厂非常期待和您一起在这个小小的网络世界里共同探索、学习和成长。

MySQL数据库入门——备份数据库

一提到数据,大家神经都会很紧张,数据的类型有很多种,但是总归一点,数据很重要,非常重要,因此,日常的数据备份工作就成了运维工作的重点中的重点的重点……………..


mysql&gt; select * from test;

+—–+——+

| id  | name |

+—–+——+

|   1 | 1       |

|  11 | text  |

|  21 | abc  |

|   9 | bcd   |

| 111 | 1     |

| 441 | text |

|  41 | abc  |

| 999 | bcd  |

+—–+——+

8 rows in set (0.00 sec)


[root@centos6 ~]# mysqldump -uroot -p test &gt;/download/testbak_\((date &#43;%F).sql</strong></p> <p><strong>Enter password: </strong></p> <p><strong>[root&#64;centos6 ~]# ll /download/</strong></p> <p><strong>total 2</strong></p> <p><strong>-rw-r--r--.  1 root root 1888 Dec 12 20:34 testbak_2016-12-12.sql</strong></p> <hr /> <p><strong>下面我们看看这个备份文件到底是什么内容</strong></p> <hr /> <p><strong>[root&#64;centos6 ~]# egrep -v &#34;^--|*|^\)“ /download/testbak2016-12-12.sql

DROP TABLE IF EXISTS `test`;

CREATE TABLE `test` (

  `id` int(4) NOT NULL,

  `name` char(20) NOT NULL

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

LOCK TABLES `test` WRITE;

INSERT INTO `test` VALUES (1,‘1’),(11,‘text’),(21,‘abc’),(9,‘bcd’),(111,‘1’),(441,‘text’),(41,‘abc’),(999,‘bcd’);

UNLOCK TABLES;

由上的文件内容,可以看出,这个备份实际的过程就是将创建数据库、建表、插入数据的sql语句备份出来,也可以说是将sql语句导出


-B参数

[root@centos6 ~]# mysqldump -uroot -p -B test &gt;/download/testbak\((date &#43;%F)_b.sql</strong></p> <p><strong>Enter password: </strong></p> <p><strong>[root&#64;centos6 ~]# egrep -v &#34;^--|^\)” /download/testbak_2016-12-12_b.sql   

/*!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 utf8 /;

/!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 /;

CREATE DATABASE /!32312 IF NOT EXISTS/ `test` /!40100 DEFAULT CHARACTER SET latin1 /;

USE `test`;

DROP TABLE IF EXISTS `test`;

/!40101 SET @saved_cs_client     = @@character_set_client /;

/!40101 SET character_set_client = utf8 /;

CREATE TABLE `test` (

  `id` int(4) NOT NULL,

  `name` char(20) NOT NULL

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

/!40101 SET character_set_client = @saved_cs_client /;

LOCK TABLES `test` WRITE;

/!40000 ALTER TABLE `test` DISABLE KEYS /;

INSERT INTO `test` VALUES (1,‘1’),(11,‘text’),(21,‘abc’),(9,‘bcd’),(111,‘1’),(441,‘text’),(41,‘abc’),(999,‘bcd’);

/!40000 ALTER TABLE `test` ENABLE KEYS /;

UNLOCK TABLES;

/!40103 SET TIME_ZONE=@OLD_TIME_ZONE /;

/!40101 SET SQL_MODE=@OLD_SQL_MODE /;

/!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS /;

/!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS /;

/!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT /;

/!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS /;

/!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION /;

/!40111 SET SQL_NOTES=@OLD_SQLNOTES */;


-B参数的作用一目了然,就是当我们的数据库丢失时,可以直接用此备份文件进行恢复,无需再重新建库、建表,然后再进行数据恢复的操作


2、压缩备份


有时候,数据库的数据比较大,可能会用到压缩后进行备份,节省备份时间与磁盘空间的使用


[root@centos6 ~]# mysqldump -uroot -p -B test|gzip &gt;/download/testbak\((date &#43;%F).sql.gz</strong></p> <p><strong>Enter password: </strong></p> <p><strong>[root&#64;centos6 ~]# ll /download/testbak_2016-12-12.sql.gz</strong></p> <p><strong>-rw-r--r--. 1 root root 753 Dec 12 20:49 /download/testbak_2016-12-12.sql.gz</strong></p> <p><strong>[root&#64;centos6 ~]# ll /download/</strong></p> <p><strong>total 14</strong></p> <p><strong>-rw-r--r--.  1 root root 2027 Dec 12 20:41 testbak_2016-12-12_b.sql</strong></p> <p><strong>-rw-r--r--.  1 root root 1888 Dec 12 20:34 testbak_2016-12-12.sql</strong></p> <p><strong>-rw-r--r--.  1 root root  753 Dec 12 20:49 testbak_2016-12-12.sql.gz</strong></p> <hr /> <p><strong>同时也可以看的压缩后的效果</strong></p> <hr /> <blockquote> <h5><span style="color:#fe2c24;"><strong>3、多库备份</strong></span></h5> </blockquote> <p><strong>[root&#64;centos6 ~]# mysqldump -uroot -p -B test mysql|gzip &gt;/download/testbak_\)(date +%F).sql01.gz

Enter password: 

– Warning: Skipping the data of table mysql.event. Specify the –events option explicitly.

[root@centos6 ~]# ll /download/testbak_2016-12-12.sql01.gz 

-rw-r–r–. 1 root root Dec 12 20:52 /download/testbak2016-12-12.sql01.gz


此处有个警告信息,可以忽略也可以备份时加上参数,备份语句如下

[root@centos6 ~]# mysqldump -uroot -p -B –events test mysql|gzip &gt;/download/testbak\((date &#43;%F).sql02.gz</strong></p> <p><strong>Enter password: </strong></p> <p><strong>[root&#64;centos6 ~]# ll /download/testbak_2016-12-12.sql02.gz                                   </strong></p> <p><strong>-rw-r--r--. 1 root root Dec 12 20:54 /download/testbak_2016-12-12.sql02.gz</strong></p> <p><strong>这样就不会有这为警告信息了</strong></p> <hr /> <p><strong>但是这种多库一起备份&#xff0c;就会产生一个问题&#xff0c;如果只是其中一个数据库有问题了&#xff0c;就不好进行单库恢复了&#xff0c;故此备份方法不常用&#xff0c;也不符合实际需求&#xff0c;因此多库备份时就需要进行多次单库备份的操作</strong></p> <hr /> <p><strong>[root&#64;centos6 ~]# mysqldump -uroot -p -B test|gzip &gt;/download/testbackup_\)(date +%F).sql.gz                 

Enter password: 

[root@centos6 ~]# mysqldump -uroot -p -B –events mysql|gzip &gt;/download/mysqlbak_\((date &#43;%F).sql.gz           </strong></p> <p><strong>Enter password: </strong></p> <p><strong>[root&#64;centos6 ~]# ll /download/</strong></p> <p><strong>total 80</strong></p> <p><strong>-rw-r--r--.  1 root root Dec 12 20:58 mysqlbak_2016-12-12.sql.gz</strong></p> <p><strong>-rw-r--r--.  1 root root    754 Dec 12 20:58 testbackup_2016-12-12.sql.gz</strong></p> <p><strong>-rw-r--r--.  1 root root   2027 Dec 12 20:41 testbak_2016-12-12_b.sql</strong></p> <p><strong>-rw-r--r--.  1 root root   1888 Dec 12 20:34 testbak_2016-12-12.sql</strong></p> <p><strong>-rw-r--r--.  1 root root Dec 12 20:52 testbak_2016-12-12.sql01.gz</strong></p> <p><strong>-rw-r--r--.  1 root root Dec 12 20:54 testbak_2016-12-12.sql02.gz</strong></p> <p><strong>-rw-r--r--.  1 root root    753 Dec 12 20:49 testbak_2016-12-12.sql.gz</strong></p> <hr /> <blockquote> <h6><span style="color:#fe2c24;"><strong>4、单表备份</strong></span></h6> </blockquote> <hr /> <p><strong>分库备份是为了恢复数据库时方便操作&#xff0c;但是同样面临问题&#xff0c;如果是某个库中的某一个表有损坏&#xff0c;但又不有全库进行恢复&#xff0c;所以实际生产中常用的是分库、分表进行备份&#xff0c;这样数据也备份了&#xff0c;恢复时也好操作</strong></p> <hr /> <p><strong>[root&#64;centos6 ~]# mysqldump -uroot -p -B test test &gt;/download/test_testbak_\)(date +%F).sql      

Enter password: 

[root@centos6 ~]# egrep -v “#|^\(|*&#34; /download/test_testbak_2016-12-12.sql</strong></p> <p><strong>-- MySQL dump 10.13  Distrib 5.5.52, for linux2.6 (x86_64)</strong></p> <p><strong>--</strong></p> <p><strong>-- Host: localhost    Database: test</strong></p> <p><strong>-- ------------------------------------------------------</strong></p> <p><strong>-- Server version       5.5.53-log</strong></p> <p><strong>--</strong></p> <p><strong>-- Current Database: &#96;test&#96;</strong></p> <p><strong>--</strong></p> <p><strong>USE &#96;test&#96;;</strong></p> <p><strong>--</strong></p> <p><strong>-- Table structure for table &#96;test&#96;</strong></p> <p><strong>--</strong></p> <p><strong>DROP TABLE IF EXISTS &#96;test&#96;;</strong></p> <p><strong>CREATE TABLE &#96;test&#96; (</strong></p> <p><strong>  &#96;id&#96; int(4) NOT NULL,</strong></p> <p><strong>  &#96;name&#96; char(20) NOT NULL</strong></p> <p><strong>) ENGINE&#61;MyISAM DEFAULT CHARSET&#61;latin1;</strong></p> <p><strong>--</strong></p> <p><strong>-- Dumping data for table &#96;test&#96;</strong></p> <p><strong>--</strong></p> <p><strong>LOCK TABLES &#96;test&#96; WRITE;</strong></p> <p><strong>INSERT INTO &#96;test&#96; VALUES (1,&#39;1&#39;),(11,&#39;text&#39;),(21,&#39;abc&#39;),(9,&#39;bcd&#39;),(111,&#39;1&#39;),(441,&#39;text&#39;),(41,&#39;abc&#39;),(999,&#39;bcd&#39;);</strong></p> <p><strong>UNLOCK TABLES;</strong></p> <p><strong>--</strong></p> <p><strong>-- Current Database: &#96;test&#96;</strong></p> <p><strong>--</strong></p> <p><strong>USE &#96;test&#96;;</strong></p> <p><strong>--</strong></p> <p><strong>-- Table structure for table &#96;test&#96;</strong></p> <p><strong>--</strong></p> <p><strong>DROP TABLE IF EXISTS &#96;test&#96;;</strong></p> <p><strong>CREATE TABLE &#96;test&#96; (</strong></p> <p><strong>  &#96;id&#96; int(4) NOT NULL,</strong></p> <p><strong>  &#96;name&#96; char(20) NOT NULL</strong></p> <p><strong>) ENGINE&#61;MyISAM DEFAULT CHARSET&#61;latin1;</strong></p> <p><strong>--</strong></p> <p><strong>-- Dumping data for table &#96;test&#96;</strong></p> <p><strong>--</strong></p> <p><strong>LOCK TABLES &#96;test&#96; WRITE;</strong></p> <p><strong>INSERT INTO &#96;test&#96; VALUES (1,&#39;1&#39;),(11,&#39;text&#39;),(21,&#39;abc&#39;),(9,&#39;bcd&#39;),(111,&#39;1&#39;),(441,&#39;text&#39;),(41,&#39;abc&#39;),(999,&#39;bcd&#39;);</strong></p> <p><strong>UNLOCK TABLES;</strong></p> <p><strong>-- Dump completed on 2016-12-12 21:13:16</strong></p> <hr /> <p><strong>因此分表备份同分库备份一样&#xff0c;只需要进行多次单表备份的操作&#xff0c;但是有的小伙伴肯定会提出问题了&#xff0c;如果一个库里几千张表&#xff0c;几万张表&#xff0c;这种备份要备到猴年马月吧&#xff1f;&#xff1f;&#xff1f;&#xff1f;&#xff0c;数据量比较大的备份可以使用专业的备份工具&#xff0c;数据量不大或者表不是很多的情况&#xff0c;可以将备份操作写成脚本 纳入定时任务&#xff0c;定时执行&#xff0c;只需要检查备份是否成功即可</strong></p> <p><strong>实际生产环境中一个简单的备份脚本&#xff0c;仅供参考</strong></p> <p><strong>[root&#64;centos6 scripts]# vi bak.sh </strong></p> <p><strong>#!/bin/sh</strong></p> <p><strong></strong></p> <p><strong>#this scripts create by root of mingongge</strong></p> <p><strong>#create at 2016-11-11</strong></p> <p><strong></strong></p> <p><strong>ip&#61;&#96;grep &#39;IPADDR&#39; /etc/sysconfig/network-scripts/ifcfg-eth0|awk -F &#34;&#61;&#34; &#39;{print \)2}‘`

  #定义服务器IP变量

BAKDIR=/backup  

  #定义备份路径

[ ! -d \(BAKDIR/\){ip} ] && mkdir -p \(BAKDIR/\){ip}

 #判断如果不存在这个路径就创建一个,为了服务器多的时候方便看

DB_PWD=”mingongge“

DB_USER=”root“

MYSQL=”/application/mysql/bin/mysql“

MYSQL_DUMP=”/application/mysql/bin/mysqldump“

DATA=`date +%F`

bak data of test’s databses

DB_NAME=`\(MYSQL -u\)DB_USER -p\(DB_PWD -e &#34;show databases;&#34;|sed &#39;1,5d&#39;&#96;</strong></p> <p><strong>  #定义数据库变量</strong></p> <p><strong>for name in \)DB_NAME

#for循环语句取库名

do

  \(MYSQL_DUMP -u\)DB_USER -p\(DB_PWD -B \){name} |gzip &gt;\(BAKDIR/\){ip}/\({name}_\)DATA.sql.gz  

 #全库备份

  [ ! -d \(BAKDIR/\){ip}/\({name} ] &amp;&amp; mkdir -p  \)BAKDIR/\({ip}/\){name}

#判断这个路径,为了区别哪个库的备份文件

  for tablename in `\(MYSQL -u\)DB_USER -p\(DB_PWD -e &#34;show tables from \){name};”|sed ‘1d’`

#for循环语句取表名

  do

   \(MYSQL_DUMP -u\)DB_USER -p\(DB_PWD \){name} \({tablename} |gzip &gt;\)BAKDIR/\({ip}/\){name}/\({tablename}_\)DATA.sql.gz

#分表备份

  done

done


执行的结果如下

[root@ranzhioa ~]# tree /backup/

/backup/

10.1xx.1xx.1xx   #服务器IP

  xxxxxxx           #其实是库名

      cash_balance_2016-12-15.sql.gz

      cash_depositor_2016-12-15.sql.gz

      cash_trade_2016-12-15.sql.gz

        crm_customer_2016-12-15.sql.gz

         crm_delivery_2016-12-15.sql.gz

        crm_order_2016-12-15.sql.gz

        crm_orderAction_2016-12-15.sql.gz

         crm_orderField_2016-12-15.sql.gz

       crm_plan_2016-12-15.sql.gz


小讯
上一篇 2025-05-12 22:57
下一篇 2025-04-29 12:02

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/193929.html