java 定時備份資料庫

來源:互聯網
上載者:User

標籤:

/**

     操作資料庫

*/

public class BackupDb {
     public String backup() throws IOException{
      String user = "root"; //資料庫的使用者名稱
      String password = "admin";//資料庫的密碼
      String database = "hrtweb";//要備份的資料庫名
      Date date = new Date();
      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
      String filepath = "d:\\"+sdf.format(date)+".sql";
      File file = new File("d:\\",sdf.format(date)+".sql");
      if(!file.exists()){
       file.createNewFile();  
      }
      String stmt1 = "mysqldump " + database +" -h 127.0.0.1 "+ " -u " + user + " -p" +
      password + " --default-character-set=gbk --result-file=" + filepath;
      try {
        Runtime.getRuntime().exec(stmt1);
        System.out.println("已經儲存到 " + filepath + " 中");
      } catch (IOException e) {
       e.printStackTrace();
      }
      return filepath;
     }
    }

/**

    建立定時器

*/

public class PickTask {
    private Timer timer = new Timer();
     private  TimerTask task = new TimerTask() {
      public void run() {
       Date date = new Date();
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       String beginDate = sdf.format(date);
       String beginTime = beginDate.substring(11, 16);
       System.out.println("開始時間:"+beginDate);

       BackupDb bdb = new BackupDb();
          // 設定備份時間
       if (beginTime.equals("17:09")) {
        try {
         bdb.backup(); // 執行檔案備份
         String dbName = bdb.backup().toString(); // 取出備份的檔案名稱字
         String path = "d:\\";
         int nameNo = dbName.lastIndexOf("\\");
         //判斷檔案是否存在,如果存在,則備份成功,如果不存在則備份不成功需要重新備份
         File file = new File(path, dbName.substring(nameNo + 1,dbName.length()));
         if (file.exists()){
               System.out.println("備份成功");

         }else{

                System.out.println("備份失敗,重新備份");
               //在備份未成功的情況下重新備份
          new PickTask().start(1, 1);
         }

        } catch (FileNotFoundException e) {
         System.out.println("can not find the file");
        } catch (IOException e) {
         e.printStackTrace();
        }
       }else{
        System.out.println("時間還不到呢,不要著急哦!");
       }
      }
     };

          //start 方法不能少,主要是schedule方法
     public void start(int delay, int internal) {
      timer.schedule(task, delay * 1000, internal * 1000);
     }
}



/**

    測試類別,執行定時備份指令

*/

public class TimerUse {
    public static void main(String[] args) {
          PickTask picktask = new PickTask();
          picktask.start(1, 60); // 每60秒執行一次
         }
}

java 定時備份資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.