linux
#!/bin/bash
clientPath= /home/usr/local/mysql/bin/
_ip= 192.168.0.153
_port= 3306
_database= backupDb
_desDir= /home/usr/local/dbbackup/
_name= backupDbUser
_pass= backupDbUserPassword
Ymd= $( date +%Y%m%d)
H= $RANDOM
M= $RANDOM
if [ ! -d "$_desDir " ] ; then
mkdir "$_desDir "
fi
if [ ! -d "$_desDir /$Ymd " ] ; then
mkdir "$_desDir /$Ymd "
fi
backupFilePath= "${_desDir} ${Ymd} /${_database} ${Ymd} ${H} ${M} .sql"
$clientPath /mysqldump --verbose --host= $_ip --port= $_port --user= $_name --password= $_pass ${_database} > ${backupFilePath}
echo "***************backup database %_database% finish.****************"
增加保留7天及每月保留某天1份
方法1: 可以重新写一个脚本. 来进行检查删除.放到 crontab里即可. 这里不另外说. 方法2: 直接在备份脚本里检查删除. 如下.
#!/bin/bash
_keepDays= 7
_keepOneInMonth= 28
clientPath= /home/usr/local/mysql/bin/
_ip= 192.168.0.153
_port= 3306
_database= backupDb
_desDir= /home/usr/local/dbbackup/
_name= backupDbUser
_pass= backupDbUserPassword
Ymd= $( date +%Y%m%d)
H= $RANDOM
M= $RANDOM
if [ ! -d "$_desDir " ] ; then
mkdir "$_desDir "
fi
if [ ! -d "$_desDir /$Ymd " ] ; then
mkdir "$_desDir /$Ymd "
fi
backupFilePath= "${_desDir} ${Ymd} /${_database} ${Ymd} ${H} ${M} .sql"
$clientPath /mysqldump --verbose --host= $_ip --port= $_port --user= $_name --password= $_pass ${_database} > ${backupFilePath}
echo "***************backup database %_database% finish.****************"
_4dayAgo= $( date -d "${_keepDays} day ago" +"%Y%m%d" )
echo ${_4dayAgo}
_currentKeepDayInCurrentMonth= $( date +%Y%m) ${_keepOneInMonth}
if [ [ ! -d "${_desDir} /${_4dayAgo} " && "${_currentKeepDayInCurrentMonth} " != "${_4dayAgo} " ] ] ; then
rm -rf "${_desDir} /${_4dayAgo} "
fi
echo "***************backup and check file finish.****************"
增加压缩功能.
发现单纯保留备份文件有点过大,因此增加压缩的功能。压缩之后将原备份文件删除。
#!/bin/bash
_keepDays= 7
_keepOneInMonth= 28
clientPath= /home/usr/local/mysql/bin/
_ip= 192.168.0.153
_port= 3306
_database= backupDb
_desDir= /home/usr/local/dbbackup/
_name= backupDbUser
_pass= backupDbUserPassword
Ymd= $( date +%Y%m%d)
H= $RANDOM
M= $RANDOM
if [ ! -d "$_desDir " ] ; then
mkdir "$_desDir "
fi
if [ ! -d "$_desDir /$Ymd " ] ; then
mkdir "$_desDir /$Ymd "
fi
backupFileParentPath= "${_desDir} ${Ymd} "
backupFileName= "${_database} ${Ymd} ${H} ${M} .sql"
backupFilePath= "${backupFileParentPath} /${backupFileName} "
$clientPath /mysqldump --verbose --host= $_ip --port= $_port --user= $_name --password= $_pass ${_database} > ${backupFilePath}
echo "***************backup database %_database% finish.****************"
_4dayAgo= $( date -d "${_keepDays} day ago" +"%Y%m%d" )
echo ${_4dayAgo}
_currentKeepDayInCurrentMonth= $( date +%Y%m) ${_keepOneInMonth}
if [ [ -d "${_desDir} /${_4dayAgo} " && "${_currentKeepDayInCurrentMonth} " != "${_4dayAgo} " ] ] ; then
rm -rf "${_desDir} /${_4dayAgo} "
fi
if [ -f "${backupFilePath} " ] ; then
tar --remove-files -czvf "${backupFilePath} .tar.gz" -C "${backupFileParentPath} " "${backupFileName} "
fi
echo "***************backup and check file finish.****************"
window
rem *******************************Code Start*****************************
@echo off
set clientPath= d:\Mysql\bin\
set _ip= 192.168.0.153
set _port= 3306
set _database= backupDb
set _desDir= d:\Mysql\dbbackup\
set _name= backupDb
set _pass= backupDbPassword
set Ymd= %date:~,4%%date:~5,2%%date:~8,2%
set H= %random%
set M= %random%
md %_desDir%%Ymd%
set backupFilePath= %_desDir%%Ymd%\%_database%%Ymd%%H%%M%.sql
%clientPath%mysqldump --opt -h%_ip% -P%_port% -u%_name% --password= %_pass% %_database%> %backupFilePath%"
echo ***************backup database %_database% finish.*****************
rem window系统创建定时任务 控制面板-> 计划任务-> 创建任务-> 创建一个24小时重复任务运行任务,凌晨两点即可
@echo on
rem *******************************Code End*****************************
加入定时任务中, 控制面板->计划任务->创建任务->创建一个每天凌晨两点运行backup.bat的任务即可 p.s 可以先运行backup.bat 测试是否正常, 也可以在定时任务列表里右键运行,测试脚本是否能正常运行.