中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

java 定時備份數(shù)據(jù)庫

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用

/**

     操作數(shù)據(jù)庫

*/

public class BackupDb {
     public String backup() throws IOException{
      String user = "root"; //數(shù)據(jù)庫的用戶名
      String password = "admin";//數(shù)據(jù)庫的密碼
      String database = "hrtweb";//要備份的數(shù)據(jù)庫名
      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("已經(jīng)保存到 " + filepath + " 中");
      } catch (IOException e) {
       e.printStackTrace();
      }
      return filepath;
     }
    }

/**

    創(chuàng)建定時器

*/

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();
          // 設(shè)定備份時間
       if (beginTime.equals("17:09")) {
        try {
         bdb.backup(); // 執(zhí)行文件備份
         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);
     }
}



/**

    測試類,執(zhí)行定時備份指令

*/

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

標(biāo)簽: Mysql 定時備份 數(shù)據(jù)庫

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:高效的MySQL的批插入 BULK INSERT

下一篇:基于FTP4J組件的FTP操作客戶端