Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Dump {
- private List<String> arquivosCriados;
- private File diretorioBase;
- private String host;
- private String userDir = System.getProperty("user.dir");
- public Dump(String diretorio, String host) {
- arquivosCriados = new ArrayList<String>();
- this.diretorioBase = new File(diretorio);
- this.host = host;
- }
- public Boolean executar() {
- if (!diretorioBase.exists()) {
- return false;
- }
- Mapeamento mapeamento = new Mapeamento(host);
- List<String> listaBancosExistentes = null;
- try {
- listaBancosExistentes = mapeamento.listarBancos();
- } catch (Exception e) {
- return false;
- }
- for (String banco : listaBancosExistentes) {
- if (!gerarDump(banco)) {
- apagarArquivosCriados();
- return false;
- }
- }
- return true;
- }
- private Boolean gerarDump(String banco) {
- String dataAtual = new SimpleDateFormat("dd/MM/yyyy HH:mm").format(new Date()).toString();
- dataAtual = dataAtual.replaceAll("/", "-");
- dataAtual = dataAtual.replaceAll(":", "");
- String filename = this.diretorioBase.toString() + "/" + banco + "_" + dataAtual + ".backup";
- File dump = new File(userDir + "/pg/dump.bat");
- if (!dump.exists()) {
- createBackupbat(dump);
- }
- try {
- String pg = userDir + "/pg/dump.bat "+ConexaoBD.host+" "+ConexaoBD.PORTA+" \"" + filename + "\" "+banco+" "+ConexaoBD.SENHA+"";
- Process pc = Runtime.getRuntime().exec(pg);
- pc.waitFor();
- arquivosCriados.add(filename);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return true;
- }
- public void apagarArquivosCriados() {
- }
- public List<String> getArquivosCriados() {
- return arquivosCriados;
- }
- private void createBackupbat(File arq) {
- try {
- StringBuffer conteudo = new StringBuffer();
- BufferedWriter buf = new BufferedWriter(new FileWriter(arq));
- conteudo.append("@echo off\n");
- conteudo.append("set pghost=%1\n");
- conteudo.append("set pgporta=%2\n");
- conteudo.append("set pgfile=%3\n");
- conteudo.append("set pgbd=%4\n");
- conteudo.append("SET PGUSER=maxmil\n");
- conteudo.append("SET PGPASSWORD=%5\n");
- conteudo.append(userDir+ "\\pg\\pg_dump.exe -Fc -O -h %pghost% -p %pgporta% -U maxmil -f %pgfile% %pgbd%\n");
- conteudo.append("exit\n");
- buf.write(conteudo.toString());
- buf.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement