自定义一个Listener,每天在特定时间备份数据库:

Java代码  收藏代码
  1. public class AutoBackUpListener implements ServletContextListener{  
  2.       
  3.     private Timer timer;  
  4.       
  5.     public void contextDestroyed(ServletContextEvent arg0) {  
  6.          System.out.println("shut down task.");  
  7.          timer.cancel(); //Terminate the timer thread  
  8.     }  
  9.   
  10.     public void contextInitialized(ServletContextEvent arg0) {  
  11.         timer = new Timer();   
  12.         System.out.println("begin task.");  
  13.            
  14.         Calendar calendar = Calendar.getInstance();  
  15.         calendar.set(Calendar.HOUR_OF_DAY, 03);//   3:30 am 备份数据库  
  16.         calendar.set(Calendar.MINUTE, 30);  
  17.         calendar.set(Calendar.SECOND, 0);  
  18.         Date time = calendar.getTime();  
  19.         timer = new Timer();  
  20.         //timer.schedule(new RemindTask(), time);  
  21.         timer.scheduleAtFixedRate(new Beifen(), time, 1000*60*60*24);//每天执行一次  
  22.          
  23.     }  
  24.   
  25.     class Beifen extends TimerTask {  
  26.         private String user_name;//数据库用户名     
  27.         private String user_psw;//数据库密码     
  28.         private String db_name;// 需要备份的数据库名     
  29.         private String host_ip;     
  30.         private String user_charset;     
  31.         private String backup_path; //存放备份文件的路径     
  32.         private String stmt;     
  33.         public Beifen(){}  
  34.           
  35.         public Beifen(String user_name,String user_psw,String db_name,String host_ip,String user_charset,String backup_path){     
  36.             this.user_name=user_name;     
  37.             this.user_psw=user_psw;     
  38.             this.db_name=db_name;     
  39.             //主机IP;     
  40.             if(host_ip==null||host_ip.equals(""))     
  41.                 this.host_ip="localhost";//默认为本机     
  42.             else    
  43.                 this.host_ip=host_ip;     
  44.             //字符集     
  45.             if(user_charset==null||user_charset.equals(""))     
  46.                 this.user_charset=" "//默认为安装时设置的字符集     
  47.             else    
  48.                 this.user_charset=" --default-character-set="+user_charset;     
  49.             this.backup_path=backup_path;     
  50.         
  51.             /*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    
  52.                     +this.user_charset+" --result-file="+this.backup_path; */    
  53.              this.stmt="D:\\AppServ\\MySQL\\bin\\mysqldump "+this.db_name+" -h "+this.host_ip+" -u"+this.user_name+" -p"+this.user_psw     
  54.             +this.user_charset+" --result-file="+this.backup_path;  
  55.         }   
  56.          
  57.         public boolean backup_run(){     
  58.             boolean run_result=false;     
  59.             try{     
  60.                 Runtime.getRuntime().exec(this.stmt);     
  61.                 run_result=true;     
  62.             }catch(Exception e){     
  63.                 e.printStackTrace();     
  64.             }finally{     
  65.                 return run_result;     
  66.             }     
  67.         }   
  68.           
  69.         @Override  
  70.         public void run() {  
  71.             Beifen backup=new Beifen("root","root","tansuo_mobile",null,"utf8","d:\\tansuo_moblie.sql");     
  72.             boolean result=backup.backup_run();     
  73.             if(result)     
  74.                 System.out.println("备份成功");  
  75.         }  
  76.           
  77.     }  
  78.       
  79.   
  80. }  

 

 使用jsp 流从服务器下载该sql文件:

Java代码  收藏代码
  1.    
  2. <%@ page language="java" pageEncoding="utf-8"%>  
  3. <%@ page import="java.io.OutputStream,java.io.File,java.io.FileInputStream"%>  
  4. <%@page import="java.io.PrintWriter"%>  
  5. <html>  
  6.   <head>  
  7.     
  8.     <title>JSP流文件下载</title>  
  9.   
  10.     <meta http-equiv="pragma" content="no-cache">  
  11.     <meta http-equiv="cache-control" content="no-cache">  
  12.     <meta http-equiv="expires" content="0">      
  13.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  14.     <meta http-equiv="description" content="This is my page">  
  15.   
  16.   </head>  
  17.     
  18.   <body>  
  19.    <%        
  20.           response.reset();  
  21.           OutputStream o =response.getOutputStream();       
  22.           byte   b[]=new   byte[500];    
  23.           String path = "d:\\tansuo_moblie.sql"//要下载的文件路径  
  24.           String myName = "数据库备份.sql";        //默认保存的名字  
  25.           //String path = request.getSession().getServletContext().getRealPath("/");  
  26.          // if(path.endsWith("\\"))  
  27.           //{  
  28.            //   path+="upload\\excelfiles\\TransInfo.xls";  
  29.           //}  
  30.           //else  
  31.           //{  
  32.             //  path+="upload\\excelfiles\\TransInfo.xls";  
  33.           //}  
  34.           File fileLoad=new File(path);    
  35.           response.reset();    
  36.           response.setCharacterEncoding("GBK");   
  37.           request.setCharacterEncoding("GBK");   
  38.             
  39.           response.setContentType("application/octet-stream; charset=GBK");       
  40.           response.addHeader("Content-Disposition""attachment; filename=\"" + new String(myName.getBytes("gb2312"),"iso8859-1") + "\"");    
  41.           
  42.           long   fileLength=fileLoad.length();     
  43.           String   length=String.valueOf(fileLength);     
  44.           response.setHeader("Content_Length",length);     
  45.           FileInputStream   in=new   FileInputStream(fileLoad);     
  46.           int   n=0;     
  47.           while((n=in.read(b))!=-1){   
  48.              o.write(b,0,n);  
  49.           }    
  50.              
  51.           in.close();  
  52.           o.close();  
  53.            
  54.   %>  
  55.   </body>  
  56. </html>  

 

 

posted on 2013-12-03 17:40  ymlove7  阅读(783)  评论(0)    收藏  举报