MySQL database sub-database sub-table backup

Sub-database backup

Create a script and write

[root@localhost scripts]# vim bak_db_v1.sh

#!/bin/bash

backup path

bak_path=/backup/db

account password

mysql_cmd='-uroot -pRedHat@123'

Databases to exclude

exclude_db='information_schema|mysql|performance_schema|sys'

Check whether the backup path exists, create it if it does not exist

[ -d ${bak_path} ] || mkdir -p ${bak_path}

Extract the database that needs to be backed up and write it to the file (dbname)

mysql ${mysql_cmd} -e 'show databases' -N | egrep -v "${exclude_db}" > dbname

Loop files, backing up for each library

while read line

do

 mysqldump ${mysql_cmd} -B $line | gzip > ${bak_path}/${line}_$(date +%F).sql.gz

done < dbname

delete temporary files

rm -f dbname

Table backup

#!/bin/bash
backup path
bak_path=/backup/db
account, password
mysql_cmd='-uroot -pRedHat@123'
database to be excluded
exclude_db='information_schema|mysql|performance_schema|sys'
to extract the data table to be backed up, And write it into the file (tbname)
mysql -uroot -pRedHat@123 -N -e'show tables from abc' > tbname
loop file, backup for each table
while read line
do
put the data table in the corresponding database Below
 [ -d ${bak_path}/abc ] || mkdir -p ${bak_path}/abc
 mysqldump ${mysql_cmd} abc $line | gzip > ${bak_path}/abc/abc_${line}_$(date + %F).sql.gz
done < tbname
delete temporary file
rm -f tbname

Guess you like

Origin blog.csdn.net/m0_70940822/article/details/132014086