linux环境
全量备份脚本
#!/bin/bash
BakDir=/home/mysql/backup
LogFile=/home/mysql/backup/bak.log
Date=`date +%Y%m%d`
Begin=`date +"%Y年%m月%d日 %H:%M:%S"`
cd $BakDir
DumpFile=$Date.sql
GZDumpFile=$Date.sql.tgz
/usr/local/mysql/bin/mysqldump -uroot -p123456 --quick --all-databases --flush-logs --delete-master-logs --single-transaction > $DumpFile
/bin/tar czvf $GZDumpFile $DumpFile
/bin/rm $DumpFile
Last=`date +"%Y年%m月%d日 %H:%M:%S"`
echo 开始:$Begin 结束:$Last $GZDumpFile succ >> $LogFile
cd $BakDir/daily
rm -f *
增量备份脚本
#!/bin/bash
BakDir=/home/mysql/backup/daily
BinDir=/home/mysql/data
LogFile=/home/mysql/backup/bak.log
BinFile=/home/mysql/data/mysql-bin.index
/usr/local/mysql/bin/mysqladmin -uroot -p123456 flush-logs
Counter=`wc -l $BinFile |awk '{print $1}'`
NextNum=0
for file in `cat $BinFile`
do
base=`basename $file`
NextNum=`expr $NextNum + 1`
if [ $NextNum -eq $Counter ]
then
echo $base skip! >> $LogFile
else
dest=$BakDir/$base
if(test -e $dest)
then
echo $base exist! >> $LogFile
else
cp $BinDir/$base $BakDir
echo $base copying >> $LogFile
fi
fi
done
echo `date +"%Y年%m月%d日 %H:%M:%S"` $Next Bakup succ! >> $LogFile
设置crontab执行备份脚本
#crontab -l //内容为下
#每个星期日凌晨3:00执行完全备份脚本
0 3 * * 0 /root/DBFullyBak.sh >/dev/null 2>&1
#周一到周六凌晨3:00做增量备份
0 3 * * 1-6 /root/DBDailyBak.sh >/dev/null 2>&1
检测shell语法是否正确
sh -n /root/DBFullyBak.sh
windows环境下小库全备份
bat写法
@echo off
color 0D
MODE con: COLS=71 LINES=25
title mysql数据库自动备份脚本(任务计划)
set "Ymd=%date:~,4%%date:~5,2%%date:~8,2%"
set "bak_dir= D:\db_backup"
set MYSQL=C:\Program Files (x86)\MySQL\MySQL Server 5.0\bin
md %bak_dir%
@echo 开始备份数据库。
mysqldump -uroot -proot --single-transaction smartpay > %obj_dir%\%Ymd%smartpay.sql
@echo 删除七天前备份的文件。
forfiles /p "D:\db_backup" /s /m *.* /d -7 /c "cmd /c del @path"
@echo 数据库备份完成,3秒后程序退出。
ping /n 3 127.0.0.1 >nul
exit
xcopy新建批处理文件 backup.dat
net stop mysql
xcopy "C:\Program Files\MySQL\MySQL Server 5.0\data\piaoyi\*.*" D:\db_backup\%date:~0,10
net start mysql
windows环境下大库需要做全+增
全备份脚本
@echo off & setlocal ENABLEEXTENSIONS
set BACKUP_PATH=D:\Backup\
set DATABASES=database1 database2 database3
set USERNAME=root
set PASSWORD=123456
set MYSQL=D:\SERVER\MySQL\bin\
set WINRAR=C:\Progra~1\WinRAR\Rar.exe
set YEAR=%date:~0,4%
set MONTH=%date:~5,2%
set DAY=%date:~8,2%
set HOUR=%time:~0,2%
set MINUTE=%time:~3,2%
set SECOND=%time:~6,2%
set DIR=%BACKUP_PATH%%YEAR%\%MONTH%\%DAY%\
set ADDON=%YEAR%%MONTH%%DAY%%HOUR%%MINUTE%%SECOND%
if not exist %DIR% (
mkdir %DIR% 2>nul
)
if not exist %DIR% (
echo Backup path: %DIR% not exists, create dir failed.
goto exit
)
cd /d %DIR%
echo Start dump databases...
for %%D in (%DATABASES%) do (
echo Dumping database %%D ...
%MYSQL%mysqldump -u%USERNAME% -p%PASSWORD% %%D > %%D.%ADDON%.sql 2>nul
if exist %WINRAR% (
%WINRAR% a -k -r -s -m1 -ep1 %%D.%ADDON%.rar %%D.%ADDON%.sql 2>nul
del /F /S /Q %%D.%ADDON%.sql 2>nul
)
)
echo Done
:exit
增备脚本
@echo off
# ------- 配置项 -------
# 要执行的文件名
set FILE=D:\cron\cron_backup.bat
# 计划频率类型
set FREQUENCY=DAILY
# 频率,与上面的计划频率类型对应
set MODIFIER=1
# 该计划执行的时间(24 小时制)
set DATETIME=00:30:00
# 计划的名字
set NAME="Backup Cron Job"
# 计划执行用户,不建议修改
set USER="System"
# ------- 以下请勿修改 -------
schtasks /Create /RU %USER% /SC %FREQUENCY% /MO %MODIFIER% /ST %DATETIME% /TN %NAME% /TR %FILE%
pause