Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.lang.*;
- import java.util.*;
- class Backup
- {
- public static void main(String[] args)
- {
- String command = "D:\MySQL\bin\mysqldump.exe -u root -proot yatin -r D:/backup.sql";
- System.out.println(command);
- Process runtimeProcess;
- try
- {
- runtimeProcess = Runtime.getRuntime().exec(command);
- int processComplete = runtimeProcess.waitFor();
- if (processComplete == 0)
- {
- System.out.println("Backup created successfully");
- //return true;
- }
- else
- {
- System.out.println("Could not create the backup");
- }
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- }
- import java.io.*;
- import java.lang.*;
- import java.util.*;
- class Restore
- {
- public static void main(String[] args)
- {
- String command = "D:\MySQL\bin\mysql.exe -u root -proot mybackup< D:/backup.sql";
- //String [] command = new String[]{"mysql",mybackup,"D:\MySQL\bin\mysql.exe -u root -proot < D:/backup.sql";
- //String[] command = new String[]{"mysql", [database], "--user=" + [username], "--password=" + [password], "-e", " source " + [absolute path to the sql file]};
- //String[] command = new String[]{"mysql",mybackup, "-u=" +root, "-p="+root, "-e", " source " +D:/backup.sql};
- System.out.println(command);
- Process runtimeProcess;
- try
- {
- runtimeProcess = Runtime.getRuntime().exec(command);
- int processComplete = runtimeProcess.waitFor();
- if (processComplete == 0)
- {
- System.out.println("Restore created successfully");
- //return true;
- }
- else
- {
- System.out.println("Could not restore");
- }
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- }
- mysql> mysql -u root -proot mybackup < D:/backup.sql;
- ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
- corresponds to your MySQL server version for the right syntax to use near 'mysql
- -u root -proot mybackup < D:/backup.sql' at line 1
- public static void mysqlExport(String host, String port, String user, String password, String dbname, String table, String folder, String query) {
- System.out.println("------ Exporting "+dbname+"."+table+" at "+folder+"---------------------------");
- try {
- String command = "mysqldump --host=" + host + " --port=" + port + " --user=" + user + " --password=" + password + " " + dbname + " " + table + " --where="" + query + "" > " + folder + table + ".sql";
- System.out.println(command);
- int returnValue = executeCommand(Arrays.asList("mysqldump", "--host="+host, "--port="+port, "--user="+user, "--password="+password, dbname, table, "--where="+query), folder + table + ".sql");
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- }
- public static void mysqlImport(String host, String port, String user, String password, String dbname, String table, String folder) {
- System.out.println("------ Importing "+dbname+"."+table+" at "+folder+"---------------------------");
- try {
- String command = "mysql --host=" + host + " --port=" + port + " --user=" + user + " --password=" + password + " " + dbname + " " + table + " < " + folder + table + ".sql";
- System.out.println(command);
- int returnValue = executeCommand(new String[]{"mysql", "--host="+host, "--port="+port, "--user=" + user, "--password=" + password, dbname, "-e", "source " + folder + table + ".sql"});
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- }
- public static int executeCommand(String[] commands) throws IOException
- {
- System.out.println(commands.toString());
- Process process = Runtime.getRuntime().exec(commands);
- return dumpProcess(process);
- }
- public static int executeCommand(List<String> commands, String folder) throws IOException
- {
- ProcessBuilder builder = new ProcessBuilder(commands);
- System.out.println(builder.command());
- builder.redirectOutput(new File(folder));
- Process process = builder.start();
- return dumpProcess(process);
- }
- public static int dumpProcess(Process process) throws IOException
- {
- int returnValue = -1;
- try {
- String s = null;
- process.waitFor();
- returnValue = process.exitValue();
- if (returnValue == 0) {
- System.out.println("Command successful !!");
- BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
- System.out.println("Here is the standard output of the command:n");
- while ((s = stdInput.readLine()) != null) {
- System.out.println(s);
- }
- } else {
- System.out.println("Command failed. Exist Status code = "+returnValue);
- BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
- System.out.println("Here is the standard error of the command (if any):n");
- while ((s = stdError.readLine()) != null) {
- System.out.println(s);
- }
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- return returnValue;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement