Java版本的mysql每天23:00自動備份

public class DatabaseBackup {
    /**
     * 
     * @param dbdir mysql數據庫安裝路徑
     * @param dbname  數據庫的名稱
     * @param backdir 備份的目錄
     */
    public static void backup(String dbdir, String dbname, String backdir) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HHmmss");
        String currentTime = dateFormat.format(calendar.getTime());
        try {
            long startTime = System.currentTimeMillis();
            Runtime rt = Runtime.getRuntime();
            Process child = rt
                    .exec(dbdir + "/bin/mysqldump --default-character-set=utf8 -uroot -p123456 " + dbname);
            InputStream in = child.getInputStream();
            InputStreamReader xx = new InputStreamReader(in, "utf8");

            FileOutputStream fout = new FileOutputStream(new File(backdir, dbname + "_" + currentTime + ".bak"));
            OutputStreamWriter writer = new OutputStreamWriter(fout, "utf8");

            dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            writer.write("-- Dump by Microsoul at " + dateFormat.format(calendar.getTime()) + "\r\n");

            String inStr;
            BufferedReader br = new BufferedReader(xx);
            // 這樣實時寫入文件很重要,網上有很多是將讀取的存入字符串中,最後再寫成文件,這樣會導致Java的堆棧內存溢出。
            while ((inStr = br.readLine()) != null) {
                writer.write(inStr);
                writer.write("\r\n");
            }

            writer.write("\r\n-- Use " + (System.currentTimeMillis() - startTime) + "ms\r\n");

            writer.flush();
            in.close();
            xx.close();
            br.close();
            writer.close();
            fout.close();
        } catch (Exception e) {
            PrintStream print = null;
            try {
                print = new PrintStream(new File(backdir, currentTime + "_backup_err.log"));
                dateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");
                currentTime = dateFormat.format(calendar.getTime());
                print.println(currentTime + "  backup failed.");
                e.printStackTrace(print);
                print.flush();
            } catch (IOException e2) {

            } finally {
                if (print != null) {
                    print.close();
                }
            }
        }

    }
}

 以上是備份的代碼;

public class Test {
    public static void main(String[] args) {
        Calendar twentyOne = Calendar.getInstance();
        twentyOne.set(Calendar.HOUR_OF_DAY, 23);
        twentyOne.set(Calendar.MINUTE, 0);
        twentyOne.set(Calendar.SECOND, 0);

        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                DatabaseBackup.backup("/usr/local/mysql", "test", "/home/xtiger/db/");
            }
        }, twentyOne.getTime(), 24 * 3600 * 1000);
    }
}

 以上是調用的代碼。通過 Timer定時調用。

發佈了37 篇原創文章 · 獲贊 1 · 訪問量 3506
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章