mkdir /usr/local/mysqlDataBackup
cd /usr/local/mysqlDataBackup
2、创建sh文件并编写shell脚本:
backup.sh
单数据库脚本:
#!/bin/bash
#This scripts is for auto backup databases
#create by weijb at 2021-07-28
#delete data from 7 day before
DATAdelete=‘date +%F -d "-7 day"‘
rm -rf /usr/local/mysqlDataBackup/*_${DATAdelete}.sql.gz
MYSQL_CMD=/usr/bin/mysqldump
MYSQL_USER=账号
MYSQL_PWD=密码
DATA=‘date +%F‘
DBname=test_db
${MYSQL_CMD} -u${MYSQL_USER} -p${MYSQL_PWD} --compact -B ${DBname} | gzip >/usr/local/mysqlDataBackup/${DBname}_${DATA}.sql.gz
#!/bin/bash
#This scripts is for auto backup databases
#create by weijb at 2021-07-28
#delete data from 7 day before
DATAdelete=‘date +%F -d "-7 day"‘
rm -rf /usr/local/mysqlDataBackup/*_${DATAdelete}.sql.gz
MYSQL_CMD=/usr/bin/mysqldump
MYSQL_USER=账号
MYSQL_PWD=密码
DATA=‘date +%F‘
DBname=‘ mysql -u${MYSQL_USER} -p${MYSQL_PWD} -e "show databases;" | sed '1,5d'‘
for DBname in ${DBname}
do
${MYSQL_CMD} -u${MYSQL_USER} -p${MYSQL_PWD} --compact -B ${DBname} | gzip >/usr/local/mysqlDataBackup/${DBname}_${DATA}.sql.gz
done
解析:
(1) 截取需要备份的数据库
-e "show databases;" | sed '1,5d'‘ 显示所有的数据库,并截取mysql自带的information_schema、mysql、performance_schema、sys意外的所有数据库。