今天我有个朋友让我帮他在windowServer服务器上备份一下mysql的数据库,于是花了一天的时间完成了一个每天定时备份数据库的功能,小编在这里为大家记录一下:
首先对于mysql命令行的导入导出作一个简单的梳理
一、导入
①mysql -h主机 -u账号 -p密码 db < db.sql[存放路径,下同] --为db数据库导入数据 / mysqljump -h地址 -u账号 -p密码 db < db.sql --为db数据库导入数据
②mysql -h主机 -u账号 -p密码 db table1 < db.table1.sql --为db数据库table1表导入数据 / mysqljump -h主机 -u账号 -p密码 db table1 < db.table1.sql --为db数据库table1表导入数据
③source 数据库文件路径(.sql)
二、导出
①mysqldump 工具导出
mysqldump -h主机 -u账号 -p密码 数据库 > 存放路径(db.sql-->默认是在mysql安装路径下bin目录下)------------导出数据库
mysqldump -h主机 -u账号 -p密码 数据库 数据表 > 存放路径 ------------导出数据表下面的某张表
rem 表示在批处理文件执行过程中,只显示结果,而不显示执行的命令 echo on 的作用很明显与echo off相反@echo off rem 开启延迟变量扩展 SETLOCAL ENABLEDELAYEDEXPANSION rem set是定义赋值变量 赋值变量 = 前后不能有空格 rem 和 :: 都是起到注释的作用 set mysql_data_path=D:\mysqldata2\dataset back_path=D:\DATA_BAK\[email protected] yy=%date:~0,4% @set mm=%date:~5,2% @set dd=%date:~8,2% @set datedir=%yy%-%mm%-%dd% if not exist %back_path%%datedir%( echo mkdir%back_path%%datedir% md %back_path%%datedir% ) set "filename1=%date:~0,4%-%date:~5,2%-%date:~8,2%\contractunit.sql" set "filename2=%date:~0,4%-%date:~5,2%-%date:~8,2%\esbdb.sql" set "filename3=%date:~0,4%-%date:~5,2%-%date:~8,2%\g_centerbookdb.sql" set "filename4=%date:~0,4%-%date:~5,2%-%date:~8,2%\g_contractunit.sql" set "filename5=%date:~0,4%-%date:~5,2%-%date:~8,2%\g_customerdb.sql" set "filename6=%date:~0,4%-%date:~5,2%-%date:~8,2%\g_esbdb.sql" set "filename7=%date:~0,4%-%date:~5,2%-%date:~8,2%\g_membercomponentdb.sql" set "filename8=%date:~0,4%-%date:~5,2%-%date:~8,2%\g_stockdb.sql" set "filename9=%date:~0,4%-%date:~5,2%-%date:~8,2%\g_systemcomponentdb.sql" set "filename10=%date:~0,4%-%date:~5,2%-%date:~8,2%\jccrmdb.sql" set "filename11=%date:~0,4%-%date:~5,2%-%date:~8,2%\jddasynreqdb.sql" set "filename12=%date:~0,4%-%date:~5,2%-%date:~8,2%\mysql.sql" set "filename13=%date:~0,4%-%date:~5,2%-%date:~8,2%\shopdb.sql" set "filename14=%date:~0,4%-%date:~5,2%-%date:~8,2%\stockdb.sql" set "filename15=%date:~0,4%-%date:~5,2%-%date:~8,2%\tcontractunit.sql" set "filename16=%date:~0,4%-%date:~5,2%-%date:~8,2%\tjccrmdb.sql" set "filename17=%date:~0,4%-%date:~5,2%-%date:~8,2%\tjddasynreqdb.sql" set "filename18=%date:~0,4%-%date:~5,2%-%date:~8,2%\tshopdb.sql" set "filename19=%date:~0,4%-%date:~5,2%-%date:~8,2%\tstockdb.sql" "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" -uqtsa [email protected] --opt --default-character-set=utf8 -e --triggers -R --hex-blob --flush-logs -x contractunit > %back_path%!filename1! "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" -uqtsa [email protected] --opt --default-character-set=utf8 -e --triggers -R --hex-blob --flush-logs -x esbdb > %back_path%!filename2! "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" -uqtsa [email protected] --opt --default-character-set=utf8 -e --triggers -R --hex-blob --flush-logs -x g_centerbookdb > %back_path%!filename3! "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" -uqtsa [email protected] --opt --default-character-set=utf8 -e --triggers -R --hex-blob --flush-logs -x g_contractunit > %back_path%!filename4! "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" -uqtsa [email protected] --opt --default-character-set=utf8 -e --triggers -R --hex-blob --flush-logs -x g_customerdb > %back_path%!filename5! "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" -uqtsa [email protected] --opt --default-character-set=utf8 -e --triggers -R --hex-blob --flush-logs -x g_esbdb > %back_path%!filename6! "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" -uqtsa [email protected] --opt --default-character-set=utf8 -e --triggers -R --hex-blob --flush-logs -x g_membercomponentdb > %back_path%!filename7! "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" -uqtsa [email protected] --opt --default-character-set=utf8 -e --triggers -R --hex-blob --flush-logs -x g_stockdb > %back_path%!filename8! "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" -uqtsa [email protected] --opt --default-character-set=utf8 -e --triggers -R --hex-blob --flush-logs -x g_systemcomponentdb > %back_path%!filename9! "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" -uqtsa [email protected] --opt --default-character-set=utf8 -e --triggers -R --hex-blob --flush-logs -x jccrmdb > %back_path%!filename10! "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" -uqtsa [email protected] --opt --default-character-set=utf8 -e --triggers -R --hex-blob --flush-logs -x jddasynreqdb > %back_path%!filename11! "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" -uqtsa [email protected] --opt --default-character-set=utf8 -e --triggers -R --hex-blob --flush-logs -x mysql > %back_path%!filename12! "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" -uqtsa [email protected] --opt --default-character-set=utf8 -e --triggers -R --hex-blob --flush-logs -x shopdb > %back_path%!filename13! "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" -uqtsa [email protected] --opt --default-character-set=utf8 -e --triggers -R --hex-blob --flush-logs -x stockdb > %back_path%!filename14! "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" -uqtsa [email protected] --opt --default-character-set=utf8 -e --triggers -R --hex-blob --flush-logs -x tcontractunit > %back_path%!filename15! "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" -uqtsa [email protected] --opt --default-character-set=utf8 -e --triggers -R --hex-blob --flush-logs -x tjccrmdb > %back_path%!filename16! "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" -uqtsa [email protected] --opt --default-character-set=utf8 -e --triggers -R --hex-blob --flush-logs -x tjddasynreqdb > %back_path%!filename17! "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" -uqtsa [email protected] --opt --default-character-set=utf8 -e --triggers -R --hex-blob --flush-logs -x tshopdb > %back_path%!filename18! "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" -uqtsa [email protected] --opt --default-character-set=utf8 -e --triggers -R --hex-blob --flush-logs -x tstockdb > %back_path%!filename19! REM D: REM cd %mysql_data_path% REM for /R %%s in (.,*) do ( REM if exist %%s\nul ( REM if "%%s" NEQ "%mysql_data_path%." ( REM set a=%%~nxs REM set "filename=%date:~0,4%-%date:~5,2%-%date:~8,2%\!a!.sql" REM "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" -uqtsa [email protected] --opt --default-character-set=utf8 -e --triggers -R --hex-blob --flush-logs -x !dbname! > %back_path%!filename! REM ) REM ) REM ) REM 不知道为什么这个for循环导出数据有问题,哎!知道的朋友请留下的你美言 echo backup success #pause
-----------------这个bat编程是今天现学的,懂不了太多
②复制data文件夹备份
操作之前一定得关闭mysql服务 net stop mysql(如果加入系统服务的话)
*******************************Code Start**************************** net stop mysql xcopy c:\mysql\data\bbs\*.* c:\db_bak\bbs\%date:~0,10%\ /S /I net start mysql *******************************Code End*****************************
③利用WinRAR对MySQL数据库进行定时备份
对于MySQL的备份,最好的方法就是直接备份MySQL数据库的Data目录。下面提供了一个利用WinRAR来对Data目录进行定时备份的方法。
*******************************Code Start***************************** net stop mysql c:\progra~1\winrar\winrar a -ag -k -r -s d:\mysql.rar d:\mysql\data net start mysql *******************************Code End*****************************
小编提示下 :以上所有的路径都是小编所使用环境的路径,如果大家使用请根据自己的路径来改写程序,同时对于bat编程请大家百度学习,因为我也是今天学习的,略懂一二!
最后的定时操作,对于window我们可以使用任务计划程序 设置每天定时任务 ;对于linux我们可以使用Crontab来处理。还有就是linux下面也可以使用mysqldump source 来导出导入数据库
原文地址:https://www.cnblogs.com/YangJieCheng/p/8178472.html