Created
July 13, 2018 17:18
-
-
Save leog1992/9dc4b03f8fdd5a61e5f71b63d01172fc to your computer and use it in GitHub Desktop.
Codigo java para generar copia de seguridad de base de datos localhost o externa.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package clases; | |
import java.io.BufferedReader; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.FileWriter; | |
import java.io.PrintWriter; | |
import javax.swing.JOptionPane; | |
/** | |
* | |
* @author CALIDAD | |
*/ | |
public class Backup_Mysql { | |
private int BUFFER = 10485760; | |
//para guardar en memmoria | |
private StringBuffer temp = null; | |
//para guardar el archivo SQL | |
private FileWriter fichero = null; | |
private PrintWriter pw = null; | |
public boolean CrearBackup(String host, String port, String user, String password, String db, String file_backup) { | |
boolean ok = false; | |
try { | |
//sentencia para crear el BackUp | |
Process run = Runtime.getRuntime().exec( | |
"C:\\xampp\\mysql\\bin\\mysqldump --host=" + host + " --port=" + port | |
+ " --user=" + user + " --password=" + password | |
+ " --compact --complete-insert --extended-insert --skip-quote-names" | |
+ " --skip-comments --skip-triggers " + db); | |
//se guarda en memoria el backup | |
InputStream in = run.getInputStream(); | |
BufferedReader br = new BufferedReader(new InputStreamReader(in)); | |
temp = new StringBuffer(); | |
int count; | |
char[] cbuf = new char[BUFFER]; | |
while ((count = br.read(cbuf, 0, BUFFER)) != -1) { | |
temp.append(cbuf, 0, count); | |
} | |
br.close(); | |
in.close(); | |
/* se crea y escribe el archivo SQL */ | |
fichero = new FileWriter(file_backup); | |
pw = new PrintWriter(fichero); | |
pw.println(temp.toString()); | |
ok = true; | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
JOptionPane.showMessageDialog(null, "Error no se genero el archivo por el siguiente motivo:"+ex.getMessage(), "Verificar",JOptionPane.ERROR_MESSAGE); | |
} finally { | |
try { | |
if (null != fichero) { | |
fichero.close(); | |
} | |
} catch (Exception e2) { | |
e2.printStackTrace(); | |
} | |
} | |
return ok; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment