Java ????????? Mysql ?????? ????????????

Source: Internet
Author: User
Java ????????? Mysql ?????? ???????????? PublicclassDatabaseBackup {*** @ paramdbdirmysql ????????????????????? * @ Paramdbname ?????????????????? * @ Parambackdir ??????????????? * Publicstaticvoidbackup (Stringdbdir, Stringdb

Java ????????? Mysql ?????? ???????????? Public class DatabaseBackup {/*** @ param dbdir mysql ????????????????????? * @ Param dbname ?????????????????? * @ Param backdir ??????????????? */Public static void backup (String dbdir, String db

Java ????????? Mysql ?????? ????????????
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 ???????????????

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.