MySQL database backup (export and import) command details mysqldump. Linux batch backup shell script for specified database

insert image description here
![Insert picture description here](https://img-blog.csdnimg.cn/174ee9e5170445b7acab94c5d97684ea.png

[options]

parameter Parameter Description
-A, --all-databases export all databases
-B, --databases Export the specified database
-h, --host=name Specify ip, the default local machine
-P, --port=# Specify port, default 3306
-u, --user=name username
-p, --password[=name] password
-t, --no-create-info Do not export the table creation statement
-d, --no-data do not export data
--ignore-table=name Table specification not to export

1 DB-level export and import (including database building, table building, and data sql statements)

1.1 Export

1.1.1 Export all databases

mysqldump -uroot -p1234 --all-databases > all-databases.sql

1.1.2 Export the specified database

mysqldump -uroot -p1234 --databases db1 db2 > custom_db.sql

1.2 import

1.2.1 System command line mode

mysql -uroot -p1234 < db_bak.sql

1.2.2 mysql command line mode

mysql -uroot -p1234
mysql > source db_bak.sql

2 table-level export and import (excluding building databases, including sql statements for building tables and data)

2.1 Export

2.1.1 Export all tables in a database

mysqldump -uroot -p1234 db1 > db1.sql

2.1.2 Export a specified table in a database

mysqldump -uroot -p1234 db1 t1 t2 > db1_custom_table.sql

2.2 import

2.2.1 System command line mode

mysql -uroot -p1234 db1 < db_bak.sql

2.2.2 mysql command line mode

mysql -uroot -p1234
mysql > use db1
mysql > source db_bak.sql

3. Linux batch backup script (library specified by sql)

#作者:李硕,日期:2023-4-15。      参考博客:https://blog.csdn.net/lishuoboy/article/details/130168865
#注意:需提前安装mysql客户端。       参考博客:https://blog.csdn.net/lishuoboy/article/details/114677600
#linux 添加定时任务编辑命令 crontab -e。使用方式通vim命令。具体crontab表达式用法自己百度
#0 * * * * /data/mysql-bak/mysql-bak-dev.sh > /data/mysql-bak/bak-dev.log
#注意:添加执行权限 chmod  +x /data/mysql-bak/mysql-bak.sh。否则定时任务不运行


#命令回显
#set -v on

bakTime=$(date +%Y%m%d-%H%M%S)
echo ========== $bakTime ==========

echo 1.定义参数
path=/data/mysql-bak/db-data
cd $path
saveDays=360
# 查询所有数据库的sql。as schema_name 是因为 schema_name可能大写、可能小写
select_schema_name_sql="SELECT schema_name as schema_name FROM information_schema.SCHEMATA WHERE schema_name not in ('mysql','information_schema','performance_schema','sys')"

echo 2.备份数据库

# 环境:dev/sit/uat/prd
profile=prd
host=127.0.0.1
port=3306
user=root
# 密码可以写入my.cnf文件,就无需mysql命令行传入。
password=你的密码

echo 2.1.$profile查询所有库。写入文件
mkdir $bakTime-$profile
mysql -h${host} -P${port} -u${user} -p${password} -e "${select_schema_name_sql}" > $bakTime-$profile/schema_name-$profile.txt

echo 2.2.$profile遍历导出所有库。每个schema_name导出一个.sql文件
IFS=$'\n'
for line in $(cat $bakTime-$profile/schema_name-$profile.txt)
do
	if [[ $line != 'schema_name' ]]; then
	  echo $line
	  # --column-statistics=0 是屏蔽版本差异报错:Unknown table 'COLUMN_STATISTICS' in information_schema (1109)
		mysqldump -h${host} -P${port} -u${user} -p${password} -B $line --column-statistics=0 > $bakTime-$profile/$(date +%Y%m%d-%H%M%S)-$profile-$line.sql
	fi
done

echo 2.3.$profile压缩并删除源文件
#tar -czvf $bakTime.tgz *.sql *.txt --remove-files
tar -cJvf $bakTime-$profile.txz $bakTime-$profile ../*$profile.log --remove-files

echo 3.滚动删除历史文件
find $path -type f -name "*.t?z" -mtime +$saveDays -exec rm -rf {
    
    } \;

Guess you like

Origin blog.csdn.net/lishuoboy/article/details/129995147