您现在的位置是:首页 > 后台技术 > JavaJava
JAVA定时备份数据库(图文)
第十三双眼睛2019-07-22【Java】人已围观
简介定时备份数据库非常重要,记录一下,以后用的时候随时来找
以前在公司工作的时候,记得有个定时备份数据库的功能,觉得挺有用的,记录一下
@Component
public class TestTask {
//@Scheduled(cron="0/30 * * * * *")
public void test() throws IOException{
System.out.println("定时任务测试");
String pathSql = "d:/1.sql";
File fileSql = new File(pathSql);
//创建备份sql文件
if (!fileSql.exists()){
fileSql.createNewFile();
}
//mysqldump -hlocalhost -uroot -p123456 db > /home/back.sql
StringBuffer sb = new StringBuffer();
sb.append("mysqldump");
sb.append(" -h localhost");
sb.append(" -u"+" root");
sb.append(" -p"+"123456");
sb.append(" "+"yyg"+" >");
sb.append(pathSql);
System.out.println("cmd命令为:"+sb.toString());
Runtime runtime = Runtime.getRuntime();
System.out.println("开始备份:"+"yyg");
Process process = runtime.exec("cmd /c"+sb.toString());
System.out.println("备份成功!");
}
}
@Component
public class TestTask {
//@Scheduled(cron="0/30 * * * * *")
public void test() throws IOException{
System.out.println("定时任务测试");
String pathSql = "d:/1.sql";
File fileSql = new File(pathSql);
//创建备份sql文件
if (!fileSql.exists()){
fileSql.createNewFile();
}
//mysqldump -hlocalhost -uroot -p123456 db > /home/back.sql
StringBuffer sb = new StringBuffer();
sb.append("mysqldump");
sb.append(" -h localhost");
sb.append(" -u"+" root");
sb.append(" -p"+"123456");
sb.append(" "+"yyg"+" >");
sb.append(pathSql);
System.out.println("cmd命令为:"+sb.toString());
Runtime runtime = Runtime.getRuntime();
System.out.println("开始备份:"+"yyg");
Process process = runtime.exec("cmd /c"+sb.toString());
System.out.println("备份成功!");
}
}

很赞哦! ()
上一篇:Servlet过滤器
下一篇:JSP简要记录