自定义一个Listener,每天在特定时间备份数据库:
- public class AutoBackUpListener implements ServletContextListener{
- private Timer timer;
- public void contextDestroyed(ServletContextEvent arg0) {
- System.out.println("shut down task.");
- timer.cancel(); //Terminate the timer thread
- }
- public void contextInitialized(ServletContextEvent arg0) {
- timer = new Timer();
- System.out.println("begin task.");
- Calendar calendar = Calendar.getInstance();
- calendar.set(Calendar.HOUR_OF_DAY, 03);// 3:30 am 备份数据库
- calendar.set(Calendar.MINUTE, 30);
- calendar.set(Calendar.SECOND, 0);
- Date time = calendar.getTime();
- timer = new Timer();
- //timer.schedule(new RemindTask(), time);
- timer.scheduleAtFixedRate(new Beifen(), time, 1000*60*60*24);//每天执行一次
- }
- class Beifen extends TimerTask {
- private String user_name;//数据库用户名
- private String user_psw;//数据库密码
- private String db_name;// 需要备份的数据库名
- private String host_ip;
- private String user_charset;
- private String backup_path; //存放备份文件的路径
- private String stmt;
- public Beifen(){}
- public Beifen(String user_name,String user_psw,String db_name,String host_ip,String user_charset,String backup_path){
- this.user_name=user_name;
- this.user_psw=user_psw;
- this.db_name=db_name;
- //主机IP;
- if(host_ip==null||host_ip.equals(""))
- this.host_ip="localhost";//默认为本机
- else
- this.host_ip=host_ip;
- //字符集
- if(user_charset==null||user_charset.equals(""))
- this.user_charset=" "; //默认为安装时设置的字符集
- else
- this.user_charset=" --default-character-set="+user_charset;
- this.backup_path=backup_path;
- /*this.stmt="C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\mysqldump "+this.db_name+" -h "+this.host_ip+" -u"+this.user_name+" -p"+this.user_psw
- +this.user_charset+" --result-file="+this.backup_path; */
- this.stmt="D:\\AppServ\\MySQL\\bin\\mysqldump "+this.db_name+" -h "+this.host_ip+" -u"+this.user_name+" -p"+this.user_psw
- +this.user_charset+" --result-file="+this.backup_path;
- }
- public boolean backup_run(){
- boolean run_result=false;
- try{
- Runtime.getRuntime().exec(this.stmt);
- run_result=true;
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- return run_result;
- }
- }
- @Override
- public void run() {
- Beifen backup=new Beifen("root","root","tansuo_mobile",null,"utf8","d:\\tansuo_moblie.sql");
- boolean result=backup.backup_run();
- if(result)
- System.out.println("备份成功");
- }
- }
- }
使用jsp 流从服务器下载该sql文件:
- <%@ page language="java" pageEncoding="utf-8"%>
- <%@ page import="java.io.OutputStream,java.io.File,java.io.FileInputStream"%>
- <%@page import="java.io.PrintWriter"%>
- <html>
- <head>
- <title>JSP流文件下载</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- </head>
- <body>
- <%
- response.reset();
- OutputStream o =response.getOutputStream();
- byte b[]=new byte[500];
- String path = "d:\\tansuo_moblie.sql"; //要下载的文件路径
- String myName = "数据库备份.sql"; //默认保存的名字
- //String path = request.getSession().getServletContext().getRealPath("/");
- // if(path.endsWith("\\"))
- //{
- // path+="upload\\excelfiles\\TransInfo.xls";
- //}
- //else
- //{
- // path+="upload\\excelfiles\\TransInfo.xls";
- //}
- File fileLoad=new File(path);
- response.reset();
- response.setCharacterEncoding("GBK");
- request.setCharacterEncoding("GBK");
- response.setContentType("application/octet-stream; charset=GBK");
- response.addHeader("Content-Disposition", "attachment; filename=\"" + new String(myName.getBytes("gb2312"),"iso8859-1") + "\"");
- long fileLength=fileLoad.length();
- String length=String.valueOf(fileLength);
- response.setHeader("Content_Length",length);
- FileInputStream in=new FileInputStream(fileLoad);
- int n=0;
- while((n=in.read(b))!=-1){
- o.write(b,0,n);
- }
- in.close();
- o.close();
- %>
- </body>
- </html>
浙公网安备 33010602011771号