MySQL数据库全量备份脚本(Linux版)

MySQL数据库全量备份shell(Linux版)脚本,备份核心逻辑:

  1. mysqldump全量导出数据库文件;
  2. 压缩备份文件;
  3. 备份脚本由crontab定时任务定时触发;

crontab配置如下:

0 2 * * * root /home/backup_mysql.sh 2>&1 | tee -a /opt/backup/mysql/mysql_backup.log

详细脚本如下:


#!/bin/sh
## 获取当前日期
curDate="`date +%Y%m%d`"
#echo $curDate

pass=123456
backupDir=/opt/backup/mysql
logFile=/opt/backup/mysql/mysql_backup.log

echo "===================================="
echo "mysql数据库备份开始${curDate}..."
echo "===================================="

startDate=$(date +%s)
echo "备份执行开始时间:$(date +%F' '%T)"

cd $backupDir

## 备份mysql所有数据库
mysqldump -uhis -p$pass -f -R -E -A --single-transaction > $backupDir/mysql_his_$curDate.sql

## 压缩备份文件
zip -q -r mysql_his_$curDate.zip mysql_his_$curDate.sql -P123

## 删除sql文件,只保留压缩备份
rm -r -f /opt/backup/mysql/mysql_his_$curDate.sql


endDate=$(date +%s)
echo "备份执行结束时间:$(date +%F' '%T)"
echo "备份总耗时:$((endDate - startDate))秒"


echo "===================================="
echo "mysql数据库备份完毕${curDate}..."
echo "===================================="

添加新评论