MySQL window环境下bat脚本定时备份

原创
2017/06/15 21:04
阅读数 23

一、bat文件

@echo off
set hour=%time:~0,2%
if "%time:~0,1%"==" " set hour=0%time:~1,1%
set now=%Date:~0,4%%Date:~5,2%%Date:~8,2%%hour%%Time:~3,2%%Time:~6,2%
echo %now%
set host=127.0.0.1
set port=3306
set user=root
set pass=love
set dbname=test
set backupfile=C:\Users\dgw\Desktop\%now%.sql
G:\tool\MySQL\bin\mysqldump -h%host% -P%port% -u%user% -p%pass% -c --add-drop-table %dbname% > %backupfile%

二、java代码

package com.love.test;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;


public class Test {
	
	public static void main(String[] args) {
		
		Timer timer = new Timer();
		MyTask myTask = new MyTask();
	    timer.schedule(myTask, 0,2000);
	}
	
	
}
class MyTask extends TimerTask{

	@Override
	public void run() {
		runbat("C:/Users/dgw/Desktop/dbbackup.bat");
		System.out.println("备份了数据");
	}
	public static void runbat(String batPath) {
        try {
            Process ps = Runtime.getRuntime().exec(batPath);
            ps.waitFor();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
	
}

三、运行效果图

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部