#!/bin/bash # 把mysql所有数据库都备份 全库全量备份 # 定义用于备份数据库的用户名和密码 user=root userPWD=s2 # 定义备份数据库名称 # Get all database list first dbNames="$(mysql -uroot -p$userPWD -hlocalhost -Bse 'show databases')" # 定义备份目录 dataBackupDir=/media/2a76a963-92b1-4f74-a2c8-b7dc7a898331/mysqlbackup # 定义邮件正文文件 eMailFile=$dataBackupDir/log/email.txt # 定义邮件地址 eMail=admin@geektown.cn # 定义备份日志文件 logFile=$dataBackupDir/log/mysqlbackup.log # DATE=`date -I` DATE=`date -d "now" +%Y%m%d` echo `date -d "now" "+%Y-%m-%d %H:%M:%S"` ; $eMailFile for dbName in ${dbNames[*]} do # 定义备份文件名 dumpFile=$dataBackupDir/db/$dbName-$DATE.sql.gz echo $dumpFile >> $eMailFile # 使用mysqldump备份数据库,请根据具体情况设置参数 mysqldump -u$user -p$userPWD $dbName --skip-lock-tables | gzip > $dumpFile done if [[ $? == 0 ]]; then echo "DataBase Backup Success!" >> $eMailFile else echo "DataBase Backup Fail!" >> $emailFile fi # 写日志文件 echo "================================" >> $logFile cat $eMailFile >> $logFile echo $dumpFile >> $logFile # 发送邮件通知 #cat $eMailFile | mail -s "MySQL Backup" $eMail