gpt4 book ai didi

java - 在 Linux 中使用 Java 创建 MySql 备份

转载 作者:太空宇宙 更新时间:2023-11-04 05:45:09 25 4
gpt4 key购买 nike

我正在使用这个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;
}
}

最佳答案

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();
}
}

关于java - 在 Linux 中使用 Java 创建 MySql 备份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16884375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com