#!/bin/sh# 备份数据库 mkdir -p /data/backup/mysql mkdir -p /data/backup/dbdata# Mysql 用户名密码MYSQL_USER=rootMYSQL_PASS=vrv123456.BACKUP_DIR=/data/backup/mysqlDATA_DIR=/data/backup/dbdata# 查询mysql中所有数据库名字SQL_STRING="SELECT SCHEMA_NAME AS db FROM information_schema.SCHEMATA WHERE SCHEMA_NAME NOT IN (‘information_schema‘, ‘performance_schema‘);"DBS=$(echo $SQL_STRING | mysql -u$MYSQL_USER -p$MYSQL_PASS -Bs)# 当前日期DATE=$(date -d ‘+0 days‘ +%Y%m%d%H%M)# 打包名称ZIP_NAME="mysql_"$DATE".gz"# 删除一个月之前的数据库备份find $BACKUP_DIR -name "mysql_*.gz" -type f -mtime +30 -exec rm {} \;# 循环备份数据库for DBNAME in $DBSdo if [ ! -d ${DATA_DIR} ]; then mkdir -p ${DATA_DIR} fi SQL_FILE=$DBNAME-$DATE".sql" /usr/bin/mysqldump -u$MYSQL_USER -p$MYSQL_PASS $DBNAME > $DATA_DIR/$SQL_FILEdone# tar打包所有sql文件tar -czvPf $BACKUP_DIR/$ZIP_NAME $DATA_DIR# 打包成功后删除sql文件if [ $? = 0 ]; then rm -r $DATA_DIRfi
MySQL Database backup script