备份数据库

rem ******MySQL backup start********
@echo off
"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqldump" --opt --single-transaction=TRUE --user=xxx --password=xxx --host=127.0.0.1 --protocol=tcp --port=3306 --default-character-set=utf8 --single-transaction=TRUE --routines --events "solo" > "F:\solo_blog\mysql\backup\backup.sql"
@echo on
rem ******MySQL backup end********

代码说明:"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqldump" 调用MySQL自带的备份工具,这个路径必须是"mysqldump.exe"所在的路径,一般都是在MySQL安装路径的bin目录下。
--user=xxx 用户名
--password=xxx 密码
--host=127.0.0.1数据库所在的服务器ip地址
--events "solo" > "F:\solo_blog\mysql\backup\backup.sql" events参数即实现了将数据库备份到一个指定的文件这一操作。"solo"是需要做备份的数据库,而大于号“>”右边的就是我们的备份文件所保存的服务器目录和文件名啦。
详细资料来自这里

上传文件到git仓库

由于网络原因,不使用github进行存储,使用gitee。

一. 下载git for windows

二. 设置用户名和邮箱

$ git config --global user.name name
$ git config --global user.email email@example.com

三. 初始化git仓库

cd f:
cd F:/solo_blog/mysql/backup
git init

四. 添加远程仓库

git remote add origin git@gitee.com:name/project.git

五. 配置公钥

ssh-keygen -t rsa -C email@example.com

执行结果中包含了
Your public key has been saved in /c/Users/home/.ssh/id_rsa.pub.
将id_rsa.pub.文件的内容复制到gitee的个人公钥

六. 测试是否成功

ssh -T git@gitee.com
Hi XXX! You've successfully authenticated, but GITEE.COM does not provide she
ll access.

七. 首次pull和push

git add --all
git commit -m "first push"
git pull origin master --allow-unrelated-histories
git push origin master

八. 自动提交脚本

@echo off
@title sql备份提交
F:
cd F:/solo_blog/mysql/backup
git add --all
git commit -m "%date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,2%:%time:~3,2%:%time:~6,2%"
git push origin master

设置定时任务

参考Windows设置定时自动重启,具体不再讲述。以下是合并的脚本

@echo off
@title sql备份和提交
mshta vbscript:createobject("sapi.spvoice").speak("开始备份和上传sql")(window.close)
rem ******MySQL backup start********
"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqldump" --opt --single-transaction=TRUE --user=xxx --password=xxx --host=127.0.0.1 --protocol=tcp --port=3306 --default-character-set=utf8 --single-transaction=TRUE --routines --events "solo" > "F:\solo_blog\mysql\backup\backup.sql"
rem ******MySQL backup end********

rem ******git push start********
F:
cd F:/solo_blog/mysql/backup
git add --all
git commit -m "%date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,2%:%time:~3,2%:%time:~6,2%"
git push origin master
rem ******git push end********

恢复数据库

mysql -hhostname -uusername -ppassword databasename < backupfile.sql(未尝试)

查看详细说明和其他操作


标题:Windows下定时自动备份mysql数据库,并将备份文件同步到git仓库
作者:xingzhegu
地址:https://www.fxg.life/articles/2019/05/12/1557672100279.html