Advertisement
Guest User

Untitled

a guest
Jun 16th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.79 KB | None | 0 0
  1. public class Dump {
  2.  
  3.     private List<String> arquivosCriados;
  4.     private File diretorioBase;
  5.     private String host;
  6.     private String userDir = System.getProperty("user.dir");
  7.    
  8.     public Dump(String diretorio, String host) {
  9.         arquivosCriados = new ArrayList<String>();
  10.         this.diretorioBase = new File(diretorio);
  11.         this.host = host;
  12.     }
  13.  
  14.     public Boolean executar() {
  15.         if (!diretorioBase.exists()) {
  16.             return false;
  17.         }
  18.         Mapeamento mapeamento = new Mapeamento(host);
  19.         List<String> listaBancosExistentes = null;
  20.         try {
  21.             listaBancosExistentes = mapeamento.listarBancos();
  22.         } catch (Exception e) {
  23.             return false;
  24.         }
  25.         for (String banco : listaBancosExistentes) {
  26.             if (!gerarDump(banco)) {
  27.                 apagarArquivosCriados();
  28.                 return false;
  29.             }
  30.         }
  31.         return true;
  32.     }
  33.  
  34.     private Boolean gerarDump(String banco) {
  35.         String dataAtual = new SimpleDateFormat("dd/MM/yyyy HH:mm").format(new Date()).toString();
  36.         dataAtual = dataAtual.replaceAll("/", "-");
  37.         dataAtual = dataAtual.replaceAll(":", "");
  38.         String filename = this.diretorioBase.toString() + "/" + banco + "_" + dataAtual + ".backup";
  39.         File dump = new File(userDir + "/pg/dump.bat");
  40.         if (!dump.exists()) {
  41.             createBackupbat(dump);
  42.         }
  43.         try {
  44.             String pg = userDir + "/pg/dump.bat "+ConexaoBD.host+" "+ConexaoBD.PORTA+" \"" + filename + "\" "+banco+" "+ConexaoBD.SENHA+"";
  45.             Process pc = Runtime.getRuntime().exec(pg);
  46.             pc.waitFor();
  47.             arquivosCriados.add(filename);
  48.         } catch (Exception e) {
  49.             e.printStackTrace();
  50.         }
  51.  
  52.         return true;
  53.     }
  54.  
  55.     public void apagarArquivosCriados() {
  56.     }
  57.  
  58.     public List<String> getArquivosCriados() {
  59.         return arquivosCriados;
  60.     }
  61.  
  62.     private void createBackupbat(File arq) {
  63.         try {
  64.             StringBuffer conteudo = new StringBuffer();
  65.             BufferedWriter buf = new BufferedWriter(new FileWriter(arq));
  66.             conteudo.append("@echo off\n");
  67.             conteudo.append("set pghost=%1\n");
  68.             conteudo.append("set pgporta=%2\n");
  69.             conteudo.append("set pgfile=%3\n");
  70.             conteudo.append("set pgbd=%4\n");
  71.             conteudo.append("SET PGUSER=maxmil\n");
  72.             conteudo.append("SET PGPASSWORD=%5\n");
  73.             conteudo.append(userDir+ "\\pg\\pg_dump.exe -Fc -O -h %pghost% -p %pgporta% -U maxmil -f %pgfile% %pgbd%\n");
  74.             conteudo.append("exit\n");
  75.             buf.write(conteudo.toString());
  76.             buf.close();
  77.         } catch (Exception e) {
  78.             e.printStackTrace();
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement