• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Java?????????mysql??????23:00????????????

mysql 搞代码 4年前 (2022-01-09) 17次浏览 已收录 0个评论

Java?????????mysql??????23:00???????????? public class DatabaseBackup { /** * * @param dbdir mysql????????????????????? * @param dbname ?????????????????? * @param backdir ??????????????? */ public static void backup(String dbdir, String db

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);            // ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????<i>本文来源gaodai$ma#com搞$$代**码)网@</i>??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???????????????


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Java?????????mysql??????23:00????????????
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址