一、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();
}
}
}
三、运行效果图