private void mAsyncEnviarFtp_DoWork(object sender, DoWorkEventArgs e) { worker = sender as BackgroundWorker; try { ConfigFtp config = mFilaEnviarFtp.Dequeue(); if (config.UtilizarHostFtp) { EnviarFtp(worker, e.Argument.ToString()); } } catch (Exception ex) { MetroMessageBox.Show(this, ex.Message); LogErro("LOGERRO.TXT", "backgroudBackupWorker_DoWork", ex.Message); } }
private void CallEnviarFtp(ConfigFtp _configFtp) { if (!mAsyncEnviarFtp.IsBusy) { _telaProgressBar = new frmTelaAguardeProcessoProgressBar(); _telaProgressBar.Text = "Enviando FTP..."; _telaProgressBar.SetDimensions(_telaProgressBar.Width, _telaProgressBar.Height); if (_configFtp.MostrarJanelaNotificacao) { _telaProgressBar.Notify(); } else { _telaProgressBar.Hide(); } mAsyncEnviarFtp.RunWorkerAsync(_configFtp.FilePath); } }
void FazerBackup(Propriedades item) { try { string nomeArquivo = "", constring = "", pathDestinoArquivo = "", nomeArquivoCompactado = ""; foreach (string b in item.ListaBancos) { constring = "server=" + item.IpBanco + ";port=" + item.PortaBanco + ";user id=" + item.UsuarioBanco + ";Password="******";database=" + b + ";Convert Zero Datetime=True;pooling=false; Allow User Variables=True;"; using (MySqlConnection conn = new MySqlConnection(constring)) { using (MySqlCommand cmd = new MySqlCommand()) { cmd.CommandTimeout = 7200; using (MySqlBackup mb = new MySqlBackup(cmd)) { cmd.Connection = conn; if (conn.State != ConnectionState.Open) { conn.Open(); } mb.ExportInfo.AddCreateDatabase = true; mb.ExportInfo.ExportTableStructure = true; mb.ExportInfo.ExportRows = true; mb.ExportInfo.ExportProcedures = true; mb.ExportInfo.ExportFunctions = true; mb.ExportInfo.ExportTriggers = true; mb.ExportInfo.ExportViews = true; mb.ExportProgressChanged += new MySqlBackup.exportProgressChange(mb_ExportProgressChange); if (item != null && item.Destinos.Length > 0) { string destino = item.Destinos[0]; if (!string.IsNullOrWhiteSpace(destino)) { MemoryStream ms = new MemoryStream(); nomeArquivo = item.Nome.RemoveCaracteresEspeciais().RemoveWhitespace() + "_" + b + "_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".sql"; mValorMSG = "Fazendo backup de " + b + " para \r\nPasta: " + destino + "\r\nArquivo: " + nomeArquivo; if (item.Compactar) { mb.ExportToMemoryStream(ms); } else { pathDestinoArquivo = destino + "\\" + nomeArquivo; mb.ExportToFile(pathDestinoArquivo); Log("log.txt", "FazerBackup", item.Nome, b, destino, "Backup efetuado com sucesso!"); } if (item.Compactar) { byte[] data = ms.ToArray(); using (MemoryStream stream = new MemoryStream(data)) { using (ZipFile zip = new ZipFile()) { zip.AddEntry(nomeArquivo, stream); nomeArquivoCompactado = Path.GetFileNameWithoutExtension(nomeArquivo) + "." + item.Compactador; pathDestinoArquivo = destino + "\\" + nomeArquivoCompactado; zip.Save(pathDestinoArquivo); Log("log.txt", "FazerBackup", item.Nome, b, destino, "Backup em " + item.Compactador + " efetuado com sucesso!"); } } File.Delete(destino + "\\" + nomeArquivo); } ConfigFtp host = new ConfigFtp() { UtilizarHostFtp = item.UtilizarHostFtp, FilePath = pathDestinoArquivo, MostrarJanelaNotificacao = item.MostrarJanelaNotificacao, NomeBanco = item.Nome }; mFilaEnviarFtp.Enqueue(host); foreach (string outroDestino in item.Destinos) { if (outroDestino == destino) { continue; } try { if (File.Exists(pathDestinoArquivo)) { File.Copy(pathDestinoArquivo, outroDestino + "\\" + nomeArquivoCompactado); } } catch { } } } } conn.Close(); } } } } try { ApagarBackupAntigos(item); } catch (Exception ex) { LogErro("LOGERRO.TXT", "ApagarBackupAntigo", ex.Message); } } catch (Exception ex) { LogErro("LOGERRO.TXT", "FazerBackup", ex.Message); } }
private void FazerBackup(Propriedades item) { try { string nomeArquivo = "", constring = ""; string pathDestinoArquivo = string.Empty; foreach (string b in item.ListaBancos) { constring = "server=" + item.IpBanco + ";port=" + item.PortaBanco + ";user id=" + item.UsuarioBanco + ";Password="******";database=" + b + ";Convert Zero Datetime=True;pooling=false; Allow User Variables=True;"; using (MySqlConnection conn = new MySqlConnection(constring)) { using (MySqlCommand cmd = new MySqlCommand()) { using (MySqlBackup mb = new MySqlBackup(cmd)) { cmd.Connection = conn; conn.Open(); mb.ExportProgressChanged += new MySqlBackup.exportProgressChange(mb_ExportProgressChange); foreach (string destino in item.Destinos) { MemoryStream ms = new MemoryStream(); nomeArquivo = item.Nome.RemoveCaracteresEspeciais().RemoveWhitespace() + "_" + b + "_" + DateTime.Now.ToString("ddMMyyyyHHmmssfff") + ".sql"; mValorMSG = "Fazendo backup de " + b + " para \r\nPasta: " + destino + "\r\nArquivo: " + nomeArquivo; if (item.Compactar) { mb.ExportToMemoryStream(ms); } else { pathDestinoArquivo = destino + "\\" + nomeArquivo; mb.ExportToFile(pathDestinoArquivo); Log("log.txt", "FazerBackup", item.Nome, b, destino, "Backup efetuado com sucesso!"); } if (item.Compactar) { byte[] data = ms.ToArray(); using (MemoryStream stream = new MemoryStream(data)) { using (ZipFile zip = new ZipFile()) { zip.AddEntry(nomeArquivo, stream); pathDestinoArquivo = destino + "\\" + Path.GetFileNameWithoutExtension(nomeArquivo) + "." + item.Compactador; zip.Save(pathDestinoArquivo); Log("log.txt", "FazerBackup", item.Nome, b, destino, "Backup em " + item.Compactador + " efetuado com sucesso!"); } } File.Delete(destino + "\\" + nomeArquivo); } ConfigFtp host = new ConfigFtp() { UtilizarHostFtp = item.UtilizarHostFtp, FilePath = pathDestinoArquivo, MostrarJanelaNotificacao = item.MostrarJanelaNotificacao, NomeBanco = item.Nome }; mFilaEnviarFtp.Enqueue(host); } conn.Close(); } } } } } catch (Exception ex) { LogErro("LOGERRO.TXT", "FazerBackup", ex.Message); } }