-2

I am using this java class to create and restore MySql backup of 'tcs' database , what should i add or edit in this code to create and restore MySql backup in linux?

import java.io.File;

public class NastyDbJobs {

    private static String dbName = "tcs";
    private static String dbUserName = "root";
    private static String dbPassword = "password";

    public static boolean backupDB(File file) {
        String path = file.getPath();

        if (!path.contains(".sql")) {
            file = new File(path + ".sql");
        }

        String executeCmd = "mysqldump -u " + dbUserName + " -p" + dbPassword + " --add-drop-database -B " + dbName + " -r " + file.getPath();
        Process runtimeProcess;
        try {

            runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();

            if (processComplete == 0) {
                System.out.println("Backup created successfully");
                runtimeProcess.destroy();
                return true;
            } else {

                System.out.println("Could not create the backup");
            }


        } catch (Exception ex) {
            ex.printStackTrace();


        }

        return false;
    }

    public static boolean restoreDB(File file) {
        String path = file.getPath();
        if (!path.contains(".sql")) {
            file = new File(path + ".sql");
        }

        String[] executeCmd = new String[]{"mysql", "--user=" + dbUserName, "--password=" + dbPassword, "-e", "source " + file.getPath()};

        Process runtimeProcess;
        try {

            runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();

            if (processComplete == 0) {
                System.out.println("Backup restored successfully");
                return true;
            } else {
                System.out.println("Could not restore the backup");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return false;
    }
}
MoienGK
  • 4,544
  • 11
  • 58
  • 92

2 Answers2

1
import java.io.File;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.*;

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class TableBackup_1 {

private static String ip="localhost";
private static String port="3306";
private static String database="xyz";
private static String user="root";
private static String pass="pass";
private static String path="/home/Admin/abc/";
public static void export()
{
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="abc.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();
}
}

public static void main(String args[]){
export();
}
}
nil
  • 11
  • 1
0

You will have to have some kind of ini/ Properties file/ data base value that points to the full path of mysqldump unless its in your path variable. Then replace mysqldump with the full path

"/myPath/mysql5/mysqldump"

or

"c:\\myPath\\mysql5\\mysqldump"

then in the exec command first parameter pass the full path to the systems mysqldump application

tgkprog
  • 4,493
  • 4
  • 41
  • 70
  • you mean i can use exec in linux too? – MoienGK Jun 03 '13 at 10:38
  • yes but the command to execute might be different so load that from a file so you can change it without the need to recompile your code. ofcourse the ini file should be safe guarded. – tgkprog Jun 03 '13 at 12:03