Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static String ip = "localhost";
- private static String port = "3306";
- private static String database = "IMS";
- private static String user = "root";
- private static String pass = "pass";
- private static String path = "/home/user/Desktop/project/";
- public static void backup() {
- Date dateNow = new Date();
- SimpleDateFormat dateformatyyyyMMdd = new SimpleDateFormat("yyyyMMdd");
- String date_to_string = dateformatyyyyMMdd.format(dateNow);
- System.out.println("date into yyyyMMdd format: " + date_to_string);
- String ss = "IMS.sql";
- String fullName = path + " " + date_to_string + " " + ss;
- String dumpCommand = "mysqldump " + database + " -h " + ip + " -u " + user + " -p" + pass;
- Runtime rt = Runtime.getRuntime();
- File test = new File(fullName);
- PrintStream ps;
- try {
- Process child = rt.exec(dumpCommand);
- ps = new PrintStream(test);
- InputStream in = child.getInputStream();
- int ch;
- while ((ch = in.read()) != -1) {
- ps.write(ch);
- //System.out.write(ch); //to view it by console
- }
- InputStream err = child.getErrorStream();
- while ((ch = err.read()) != -1) {
- System.out.write(ch);
- }
- } catch (Exception exc) {
- exc.printStackTrace();
- }
- }
- String user = "root";
- String pass = "1234";
- String[] restoreCmd = new String[]{"mysql", "--user=" + user, "--password=" + pass, "-e", "source " + path};
- Process process;
- try {
- process = Runtime.getRuntime().exec(restoreCmd);
- int procCom = process.waitFor();
- if (procCom == 0) {
- lbl_restore.setText("Restore Succes!");
- } else {
- lbl_restore.setText("Can't Restore!");
- }
- } catch (Exception e) {
- JOptionPane.showMessageDialog(null, e);
- }
- public boolean restoreDatabase(String dbUserName, String dbPassword, String source) {
- String[] executeCmd = new String[]{"mysql", "--user=" + dbUserName, "--password=" + dbPassword, "-e", "source " + source};
- Process runtimeProcess;
- try {
- runtimeProcess = Runtime.getRuntime().exec(executeCmd);
- int processComplete = runtimeProcess.waitFor();
- if (processComplete == 0) {
- log.info("Backup restored successfully with " + source);
- return true;
- } else {
- log.info("Could not restore the backup " + source);
- }
- } catch (Exception ex) {
- log.error(ex, ex.getCause());
- }
- return false;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement