-2

我正在使用这个java类来创建和恢复'tcs'数据库的MySql备份,我应该在这段代码中添加或编辑什么来在linux中创建和恢复MySql备份?

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;
    }
}
4

2 回答 2

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();
}
}
于 2016-01-12T05:49:30.850 回答
0

您将必须有某种指向 mysqldump 完整路径的 ini/属性文件/数据库值,除非它在您的路径变量中。然后用完整路径替换mysqldump

"/myPath/mysql5/mysqldump"

或者

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

然后在 exec 命令的第一个参数中将完整路径传递给系统 mysqldump 应用程序

于 2013-06-03T09:23:55.760 回答