java定期备份mysql脚本

判断当前系统是win还是linux,进行数据的备份,返回sql文件

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author 陈飞龙
 */
public class MysqlBackUp {
    /**
     * @param host 数据库服务器主机地址,可以是ip,也可以是域名
     * @param port 数据库服务器端口
     * @param dbName 数据库名字
     * @param username 数据库用户名
     * @param password 数据库密码(明文)
     * @param filePath 存到哪个文件,形如:"F:/db/2019-08-12_00_00_00.sql"
     * @return
     */
    public static File backup(String host, int port, String dbName, String username, String password, String filePath) {
        Long starttime = System.currentTimeMillis();
        try {
            File file = new File(filePath);
            String[] commands = new String[3];
            String os = System.getProperties().getProperty("os.name");
            if (os.startsWith("Win")) {
                commands[0] = "cmd.exe";
                commands[1] = "/c";
            } else {
                commands[0] = "/bin/sh";
                commands[1] = "-c";
            }
            StringBuilder mysqldump = new StringBuilder();
            mysqldump.append("mysqldump");
            mysqldump.append(" --opt");
            mysqldump.append(" --user=").append(username);
            mysqldump.append(" --password=").append(password);
            mysqldump.append(" --host=").append(host);
            mysqldump.append(" --protocol=tcp");
            mysqldump.append(" --port=").append(port);
            mysqldump.append(" --default-character-set=utf8");
            mysqldump.append(" --single-transaction=TRUE");
            mysqldump.append(" --routines");
            mysqldump.append(" --events");
            mysqldump.append(" ").append(dbName);
            mysqldump.append(" > ");
            mysqldump.append("").append(filePath).append("");
            String command = mysqldump.toString();
            System.out.println(command);
            commands[2] = command;
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec(commands);
            if (process.waitFor() == 0) {
                Long endtime = System.currentTimeMillis();
                Long distance = endtime - starttime;
                System.out.println("【" + dbName + "】备份成功,耗时:" + distance + "ms");
                return file;
            } else {
                InputStream is = process.getErrorStream();
                if (is != null) {
                    BufferedReader in = new BufferedReader(new InputStreamReader(is, "utf-8"));
                    String line;
                    StringBuilder sb = new StringBuilder();
                    while ((line = in.readLine()) != null) {
                        sb.append(line);
                    }
                    System.out.println("数据库备【" + dbName + "】份失败\r\n" + sb.toString());
                }
            }
        } catch (Exception e) {
            System.err.println("数据库备【" + dbName + "】份失败");
            return null;
        }
        return null;
    }
}

设置定时任务,只做大概的思路,定时备份

/**
 * Created by 陈飞龙 on 2019/08/12 15:59
 */
@Component
@Configuration      //1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class ScheduledUtil {
    @Scheduled(cron = "0/5 * * * * ?")  //每隔5秒执行一次定时任务
    public void consoleInfo(){
//        System.out.println("定时任务");
    }
}

把脚本发送到指定的邮箱

不做详细说明

猜你喜欢

转载自blog.csdn.net/xljx_1/article/details/99301613
今日推荐