certo chapa descupa a demora mas la vai, esse primeiro codigo declaro alguns metodos utilitarios para geração dos scripts
de backup e restore.
a esses codigos foram implementados e testados em SGBD`s MySQL, PostgreSQL.
não estam adequados para derby.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package db.admin.backup.sql.auto;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author antoniel
*/
public class ManageBackup {
public static void createScriptBackup() {
File file = null;
StringBuffer buffer = null;
if(File.separator.compareTo("/") == 0 ) {
file = new File( "backup.sh" );
buffer = new StringBuffer("#!/bin/bash\n\n");
} else {
file = new File( "backup.bat" );
buffer = new StringBuffer();
}
buffer.append("");//CODIGO DO BACKUP
try {
BufferedWriter saida = new BufferedWriter(new PrintWriter(file));
saida.write(buffer.toString());
saida.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(ManageBackup.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex) {
Logger.getLogger(ManageBackup.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void createScriptRestore() {
File file = null;
StringBuffer buffer = null;
if(File.separator.compareTo("/") == 0 ) {
file = new File( "restore.sh" );
buffer = new StringBuffer("#!/bin/bash\n\n");
} else {
file = new File( "restore.bat" );
buffer = new StringBuffer();
}
buffer.append("");//CODIGO DO RESTORE DO BANCO DE DADOS
try {
BufferedWriter saida = new BufferedWriter(new PrintWriter(file));
saida.write(buffer.toString());
saida.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(ManageBackup.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex) {
Logger.getLogger(ManageBackup.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**Retorna o tempo em milisegundos*/
public static int getTime(String time) {
int ponto = time.indexOf(":");//captura o indice do caractere delimitador
if (ponto == -1) {
throw new IllegalArgumentException("Tempo invalido forneça o tempo no formato \"hh:mm\"");
}
int hora = Integer.parseInt(time.substring(0, ponto));//recupera o numero de horas
int min = Integer.parseInt(time.substring(ponto + 1,time.length()));//
return ( ( ( (hora * 60 ) + min ) * 60 ) * 1000 );//retorna o tempo em milisegundos
}
/*Retorna o path de onde o script de restore estar salvo*/
public static String getScriptBackup() {
if ( File.separator.compareTo("/") == 0 ) {
return "backup.sh";
}
return "backup.bat";
}
/*Retorna o path de onde o script de restore estar salvo*/
public static String getScriptRestore() {
if ( File.separator.compareTo("/") == 0 ) {
return "restore.sh";
}
return "restore.bat";
}
}
nesse codigo aqui e que entra a ação do backup.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package db.admin.backup.sql.auto;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author antoniel
*/
public class Backup {
public Backup(long time) {
this.time = time;
}
/*Executa o backup em concorrencia com a aplicação.*/
public void createBackup(String shell) {
thread = new Thread(new RunBackup(shell));
}
/**Executa restore do banco de dados apartir de um backup.*/
public void createRestore(String shell) {
try {
processo = Runtime.getRuntime().exec(shell);
printErrorExecucao();
} catch (IOException ex) {
Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void stop() {
thread.stop();
}
public void start() {
thread.start();
}
public long getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
public class RunBackup implements Runnable {
private RunBackup(String shell) {
this.shell = shell;
}
@Override
public void run() {
try {
while(true) {
Thread.sleep(time);
processo = Runtime.getRuntime().exec(shell);
printErrorExecucao();
}
} catch (InterruptedException ex) {
Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
}
}
private String shell;
}
private void printErrorExecucao() {
BufferedReader entrada = new BufferedReader(new InputStreamReader(processo.getErrorStream()));
String linha = null;
try {
while ((linha = entrada.readLine()) != null) {
System.out.println(linha);
}
entrada.close();
} catch (IOException ex) {
Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
}
}
private long time;
private Thread thread;
private Process processo;
}
aqui e apenas exemplo de utilização.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package db.admin.backup.sql.auto;
/**
*
* @author antoniel
*/
public class RunBackup {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ManageBackup.createScriptBackup();
Backup backup = new Backup(ManageBackup.getTime("01:00"));//executa o backup de hora em hora
backup.createBackup(ManageBackup.getScriptBackup());
backup.start();//inicia a thread
}
}