定时备份数据库

1public 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代码  
81   
82<%@ page language="java" pageEncoding="utf-8"%>  
83<%@ page import="java.io.OutputStream,java.io.File,java.io.FileInputStream"%>  
84<%@page import="java.io.PrintWriter"%>  
85<html>  
86  <head>  
87    
88    <title>JSP流文件下载</title>  
89  
90    <meta http-equiv="pragma" content="no-cache">  
91    <meta http-equiv="cache-control" content="no-cache">  
92    <meta http-equiv="expires" content="0">      
93    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
94    <meta http-equiv="description" content="This is my page">  
95  
96  </head>  
97    
98  <body>  
99   <%        
100          response.reset();  
101          OutputStream o =response.getOutputStream();       
102          byte   b[]=new   byte[500];    
103          String path = "d:\\tansuo_moblie.sql"; //要下载的文件路径  
104          String myName = "数据库备份.sql";        //默认保存的名字  
105          //String path = request.getSession().getServletContext().getRealPath("/");  
106         // if(path.endsWith("\\"))  
107          //{  
108           //   path+="upload\\excelfiles\\TransInfo.xls";  
109          //}  
110          //else  
111          //{  
112            //  path+="upload\\excelfiles\\TransInfo.xls";  
113          //}  
114          File fileLoad=new File(path);    
115          response.reset();    
116          response.setCharacterEncoding("GBK");   
117          request.setCharacterEncoding("GBK");   
118            
119          response.setContentType("application/octet-stream; charset=GBK");       
120          response.addHeader("Content-Disposition", "attachment; filename=\"" + new String(myName.getBytes("gb2312"),"iso8859-1") + "\"");    
121          
122          long   fileLength=fileLoad.length();     
123          String   length=String.valueOf(fileLength);     
124          response.setHeader("Content_Length",length);     
125          FileInputStream   in=new   FileInputStream(fileLoad);     
126          int   n=0;     
127          while((n=in.read(b))!=-1){   
128             o.write(b,0,n);  
129          }    
130             
131          in.close();  
132          o.close();  
133           
134  %>  
135  </body>  
136</html>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值