一个分享个人学习、开发经验的Blog,http://www.joyphper.net

一个简单的mysql备份java程序

posted @ 2011-09-02 12:27 | 阅读:3445 | 评论:1 | 分类: Java

 一个简单的java备份mysql程序,可以备份远程数据库

使用的条件是:

1、本地电脑必须能执行mysqldump命令

2、本地电脑必须能运行java程序

java代码如下:

 

import java.io.*; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.Properties; 
 
import org.apache.log4j.Logger; 
 
public class MysqlBackup  
{ 
    private String Host=""; 
    private String User=""; 
    private String Pwd=""; 
    private String DBname=""; 
    private String BackupDir=""; 
    public static void main(String[] args) 
    { 
        MysqlBackup backup=new MysqlBackup(); 
        System.out.println(backup.backup()?"OK":"NO"); 
    } 
     
    public MysqlBackup() 
    { 
        Properties prop = new Properties();  
        InputStream in = Object.class.getResourceAsStream("/config.properties");  
        try { 
            prop.load(in); 
            this.Host       = prop.getProperty("db_Host").trim(); 
            this.User       = prop.getProperty("db_User").trim(); 
            this.Pwd        = prop.getProperty("db_Pwd").trim();  
            this.DBname     = prop.getProperty("db_Name").trim(); 
            this.BackupDir  = prop.getProperty("backup_Dir").trim(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
 
    } 
    public boolean backup() {//备份 
        Logger logger = Logger.getLogger(MysqlBackup.class.getName()); 
        logger.info("backup start"); 
        try { 
            Runtime rt = Runtime.getRuntime(); 
 
            String mysql = "mysqldump -h"+this.Host+" -u"+this.User+" -p"+this.Pwd+" "+this.DBname; 
 
            // 调用 mysql 的 cmd: 
            Process child = rt.exec("cmd /c "+mysql);// 设置导出编码为utf8。这里必须是utf8 
 
            // 把进程执行中的控制台输出信息写入.sql文件,即生成了备份文件。注:如果不对控制台信息进行读出,则会导致进程堵塞无法运行 
            InputStream in = child.getInputStream();// 控制台的输出信息作为输入流 
 
            InputStreamReader xx = new InputStreamReader(in, "utf8");// 设置输出流编码为utf8。这里必须是utf8,否则从流中读入的是乱码 
 
            String inStr; 
            StringBuffer sb = new StringBuffer(""); 
            String outStr; 
            // 组合控制台输出信息字符串 
            BufferedReader br = new BufferedReader(xx); 
            while ((inStr = br.readLine()) != null) { 
                sb.append(inStr + "
"); 
            } 
            outStr = sb.toString(); 
 
            // 要用来做导入用的sql目标文件: 
            DateFormat df = new SimpleDateFormat("yyyyMMdd"); 
            String backupFile = this.BackupDir+this.DBname+df.format(new Date())+".sql"; 
             
            FileOutputStream fout = new FileOutputStream(backupFile); 
            OutputStreamWriter writer = new OutputStreamWriter(fout, "utf8"); 
            writer.write(outStr); 
            // 注:这里如果用缓冲方式写入文件的话,会导致中文乱码,用flush()方法则可以避免 
            writer.flush(); 
 
            // 别忘记关闭输入输出流 
            in.close(); 
            xx.close(); 
            br.close(); 
            writer.close(); 
            fout.close(); 
 
            //System.out.println("/* Output OK! */"); 
                         
            logger.info("buckup success"); 
            logger.info("backup End"); 
            return true; 
        } catch (Exception e) { 
            e.printStackTrace(); 
            logger.info("backup failure! please try again"); 
            return false; 
        } 
         
         
    } 
} 

 

config.properties代码如下

 

db_Host=127.0.0.1 #服务器地址 
db_User=root #用户名 
db_Pwd=123456 #密码 
db_Name=mysql #数据库名 
backup_Dir=E:/ #备份文件存放位置 

TAG: java , mysql , mysql备份

共有1条评论 发表评论>>

杭州男科医院 发表于:2011-09-14 09:58
用了 谢谢
点击换一张验证码