一、備份相關(guān)概念:
- 備份分為:完全備份、增量備份、差異備份
- 完全備份策略:可以備份一臺(tái)數(shù)據(jù)庫(kù)服務(wù)器上的所有數(shù)據(jù)、也可以備份一個(gè)數(shù)據(jù)庫(kù)下所有數(shù)據(jù)、還可以備份一張表里的所有數(shù)據(jù)。
- 工作中一般的備份策略:完全備份+增量備份、完全備份+差異備份
- 實(shí)現(xiàn)方式:一般會(huì)通過(guò)計(jì)劃任務(wù)進(jìn)行實(shí)現(xiàn)備份功能
二、完全備份說(shuō)明:
- 有二種方式進(jìn)行完全備份分別是:物理方式(冷備)、邏輯方式(熱備)
- 物理方式(冷備):需要斷開數(shù)據(jù)庫(kù)服務(wù),適合線下數(shù)據(jù)庫(kù)服務(wù)器
- 邏輯方式(熱備):1不需要斷開數(shù)據(jù)庫(kù)服務(wù),適合線上數(shù)據(jù)庫(kù)服務(wù)器,但缺點(diǎn)是:在備份數(shù)據(jù)和恢復(fù)數(shù)據(jù)的時(shí)候會(huì)鎖表、備份恢復(fù)時(shí),只能把數(shù)據(jù)恢復(fù)到備份時(shí)刻的數(shù)據(jù)。
三、語(yǔ)法格式:
mysqldump 備份數(shù)據(jù)格式
mysqldump -uroot -p密碼 備份語(yǔ)句 > /目錄名/備份文件名.sql
- 備份語(yǔ)句格式:
庫(kù)名 表名:備份一張的所有數(shù)據(jù)
庫(kù)名 表名1 表名2:備份多張表的所有數(shù)據(jù)
-B 庫(kù)名:備份1個(gè)庫(kù)的所數(shù)據(jù)
-B 庫(kù)名1 庫(kù)名2:備份多個(gè)庫(kù)的所數(shù)據(jù)
-A 或 --all-databases:備份服務(wù)器的所有數(shù)據(jù)mysql 恢復(fù)數(shù)據(jù)格式
mysql -uroot -p密碼 [庫(kù)名] < /目錄名/備份文件名.sql
說(shuō)明:使用表的備份文件恢復(fù)數(shù)據(jù)時(shí)必須寫庫(kù)名
四、完全備份示例:
物理方式備份:
- 備份數(shù)據(jù)
# 必須先停止mysql服務(wù)器
[root@host50 ~]# systemctl stop mysqld
# 創(chuàng)建備份文件夾
[root@host50 ~]# mkdir /bakdir
# 備份數(shù)據(jù)方式一:拷貝數(shù)據(jù)庫(kù)目錄
[root@host50 ~]# cp -rp /var/lib/mysql /bakdir/mysql.bak
# 備份數(shù)據(jù)方式二:打包壓縮數(shù)據(jù)庫(kù)目錄下文件
[root@host50 ~]# cd /var/lib/mysql
[root@host50 ~]# tar -zcf /bakdir/mysql.tar.gz ./*
- 查看備份文件,并模擬數(shù)據(jù)丟失
[root@host50 ~]# ls /bakdir/
mysql.bak mysql.tar.gz
# 模擬數(shù)據(jù)丟失
[root@host50 ~]# rm -rf /var/lib/mysql/*
- 恢復(fù)數(shù)據(jù)方式一:
# 文件拷貝回?cái)?shù)據(jù)庫(kù)目錄
[root@host50 ~]# cp -r /bakdir/mysql.bak/* /var/lib/mysql/
# 修改屬主屬組為mysql
[root@host50 ~]# chown -R mysql:mysql /var/lib/mysql
# 重新啟動(dòng)mysql服務(wù)
[root@host50 ~]# systemctl start mysqld
# 登錄mysql驗(yàn)證
[root@host50 ~]# mysql -uroot -p123qqq...A
mysql> show databases;
- 恢復(fù)數(shù)據(jù)方式二:
# 解壓數(shù)據(jù)至mysql目錄下
[root@host50 ~]#tar -zxf /bakdir/mysql.tar.gz
-C /var/lib/mysql
# 重新啟動(dòng)mysql服務(wù)
[root@host50 ~]#systemctl start mysqld
# 登錄mysql驗(yàn)證
[root@host50 ~]# mysql -uroot -p123qqq...A
mysql> show databases;
邏輯方式備份:
- 備份數(shù)據(jù)
# 備份tarena庫(kù)salary表的所有數(shù)據(jù)
[root@host50 ~]# mysqldump -uroot -p密碼 tarena salary > /bakdir/tarena_salary.sql
# 備份tarena庫(kù)所有數(shù)據(jù)
[root@host50 ~]# mysqldump -uroot -p密碼 -B tarena > /bakdir/tarena.sql
# 備份tarena庫(kù)和 db1庫(kù)的所有數(shù)據(jù)
[root@host50 ~]# mysqldump -uroot -p密碼 -B db1 tarena > /bakdir/twodb.sql
# 備份所有數(shù)據(jù)
[root@host50 ~]# mysqldump -uroot -p密碼 -A > /bakdir/allbak.sql
- 查看備份文件
[root@host50 ~]# ls /bakdir/
allbak.sql tarena.sql tarena_salary.sql
mysql.bak mysql.tar.gz twodb.sql
- 恢復(fù)表數(shù)據(jù)
# 模擬數(shù)據(jù)丟失,刪除表數(shù)據(jù)
mysql> delete from tarena.salary;
mysql> exit
# 導(dǎo)入備份恢復(fù)數(shù)據(jù)
[root@host50 ~]# mysql -uroot -p密碼 tarena < /bakdir/ tarena_salary.sql
# 查看表記錄
[root@host50 ~]# mysql -uroot -p密碼
Mysql> select count(*) from tarena.salary;
...
- 恢復(fù)庫(kù)數(shù)據(jù)
# 使用庫(kù)的備份文件恢復(fù)數(shù)據(jù)
mysql> drop database tarena;
mysql> exit;
# 使用庫(kù)的備份文件恢復(fù)數(shù)據(jù) 不需要寫庫(kù)名
[root@host50 ~]# mysql -uroot -p密碼 < /bakdir/tarena.sql
# 管理員登陸查看數(shù)據(jù)
[root@host50 ~]# mysql -uroot -p密碼
mysql> show databases;
...
mysql> use tarena;
...
mysql> show tables;
...