首页 > 代码库 > mysql 自动定时备份

mysql 自动定时备份

说明:以下是数据库自动备份脚本,可以保存成.bat的方式,然后用计划任务去定时执行。


::echo 数据库开始备份!


@echo off


::定义磁盘

set disk=D

::定义单位名称 

set unit=jd_scjdj

::定义压缩文件路径

set rar=C:\Program Files\WinRAR


::定义存放路径

set filepath_ntdat=%disk%:\webserver\data_bak\ntdat

set filepath_ntcms=%disk%:\webserver\data_bak\ntcms_jd_scjdj

set filepath_ntlas=%disk%:\webserver\data_bak\ntlas_jd_sc

set filepath_ntlfs=%disk%:\webserver\data_bak\ntlfs_jd_sc

set filepath_ntpms=%disk%:\webserver\data_bak\ntpms_jd_sc

set filepath_ntrem=%disk%:\webserver\data_bak\ntrem_jd_sc

set filepath_pras=%disk%:\webserver\data_bak\pras

::定义文件类型

set filetype=MySQL

::定义时间格式

set datetime="%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%time:~6,2%"

::定义文件名

set filename_ntdat=ntdat_%unit%-%filetype%-%datetime%

set filename_ntcms_jd_scjdj=ntcms_jd_scjdj_%unit%-%filetype%-%datetime%

set filename_ntlas_jd_sc=ntlas_jd_sc_%unit%-%filetype%-%datetime%

set filename_ntlfs_jd_sc=ntlfs_jd_sc_%unit%-%filetype%-%datetime%

set filename_ntpms_jd_sc=ntpms_jd_sc_%unit%-%filetype%-%datetime%

set filename_ntrem_jd_sc=ntrem_jd_sc_%unit%-%filetype%-%datetime%

set filename_pras=pras_%unit%-%filetype%-%datetime%

::判断文件夹是否存在

If Not Exist %filepath_ntdat% MD %filepath_ntdat%

If Not Exist %filepath_ntcms% MD %filepath_ntcms%

If Not Exist %filepath_ntlas% MD %filepath_ntlas%

If Not Exist %filepath_ntlfs% MD %filepath_ntlfs%

If Not Exist %filepath_ntpms% MD %filepath_ntpms%

If Not Exist %filepath_ntrem% MD %filepath_ntrem%

If Not Exist %filepath_pras%  MD %filepath_pras%


::备份数据库

cd /d "%disk%:\webserver\tools\mysql_5.6.26\bin"

mysqldump.exe --opt -u "root" --password="root" "ntdat" > "%filepath_ntdat%\ntdat.sql"

mysqldump.exe --opt -u "root" --password="root" "ntcms" > "%filepath_ntcms%\ntcms_jd_scjdj.sql"

mysqldump.exe --opt -u "root" --password="root" "ntlas" > "%filepath_ntlas%\ntlas_jd_sc.sql"

mysqldump.exe --opt -u "root" --password="root" "ntlfs" > "%filepath_ntlfs%\ntlfs_jd_sc.sql"

mysqldump.exe --opt -u "root" --password="root" "ntpms" > "%filepath_ntpms%\ntpms_jd_sc.sql"

mysqldump.exe --opt -u "root" --password="root" "ntrem" > "%filepath_ntrem%\ntrem_jd_sc.sql"

mysqldump.exe --opt -u "root" --password="root" "pras" > "%filepath_pras%\pras.sql"


::压缩备份文件

cd /d "%filepath_ntdat%"

"%rar%\WinRAR.exe" A "%filename_ntdat%.rar" "ntdat.sql"

del /Q "ntdat.sql"

cd /d "%filepath_ntcms%"

"%rar%\WinRAR.exe" A "%filename_ntcms_jd_scjdj%.rar" "ntcms_jd_scjdj.sql"

del /Q "ntcms_jd_scjdj.sql"

cd /d "%filepath_ntlas%"

"%rar%\WinRAR.exe" A "%filename_ntlas_jd_sc%.rar" "ntlas_jd_sc.sql"

del /Q "ntlas_jd_sc.sql"

cd /d "%filepath_ntlfs%"

"%rar%\WinRAR.exe" A "%filename_ntlfs_jd_sc%.rar" "ntlfs_jd_sc.sql"

del /Q "ntlfs_jd_sc.sql"

cd /d "%filepath_ntpms%"

"%rar%\WinRAR.exe" A "%filename_ntpms_jd_sc%.rar" "ntpms_jd_sc.sql"

del /Q "ntpms_jd_sc.sql"

cd /d "%filepath_ntrem%"

"%rar%\WinRAR.exe" A "%filename_ntrem_jd_sc%.rar" "ntrem_jd_sc.sql"

del /Q "ntrem_jd_sc.sql"

cd /d "%filepath_pras%"

"%rar%\WinRAR.exe" A "%filename_pras%.rar" "pras.sql"

del /Q "pras.sql"

::echo 数据库结束备份!


本文出自 “杨海龙的博客” 博客,请务必保留此出处http://7218743.blog.51cto.com/7208743/1844513

mysql 自动定时备份