private void SalvarConfiguracaoBackup_Click(object sender, EventArgs e)
        {
            Backup backup = Firebird.RetornaBackup();

            if (backup == null && chkAtivarBackup.Checked)
            {
                backup = new Backup();
                //backup.Ativo = 1;
                backup.Periodo       = cmbPeriodo.SelectedIndex;
                backup.CaminhoBackup = CaminhoBackup.Text;
                bool retorno = Firebird.SaveBackup(backup);
                if (retorno)
                {
                    MessageBox.Show("Backup Salvo com Sucesso.", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.None);
                }
                else
                {
                    MessageBox.Show("Erro ao tentar salvar o backup. Verifique o log de erro para obter mais informações.", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else if (chkAtivarBackup.Checked)
            {
                //backup.Ativo = 1;
                backup.Periodo       = cmbPeriodo.SelectedIndex;
                backup.CaminhoBackup = CaminhoBackup.Text;
                string data = DateTime.Now.ToString();
                backup.DataUltimoBackup = data.Substring(0, 10);
                bool retorno = Firebird.AlterarDataUltimoBackup(backup.DataUltimoBackup);
                if (retorno)
                {
                    MessageBox.Show("Alterado as configurações do Backup", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.None);
                }
                else
                {
                    MessageBox.Show("Erro ao tentar alterar o backup!", "Erro!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else if (backup != null && chkAtivarBackup.Checked == false)
            {
                if (MessageBox.Show("Você desativou o backup automático.Deseja confirmar?", "Confirmar", MessageBoxButtons.YesNo,
                                    MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Firebird.DeletaBackup();
                }
            }
        }
        private void btnRestaurarCopiaDeSeguranca_Click(object sender, EventArgs e)
        {
            Backup         backup    = Firebird.RetornaBackup();
            OpenFileDialog oldBackup = new OpenFileDialog();

            this.Cursor = Cursors.WaitCursor;

            string NomeArquivo = string.Empty;

            FbConnection.ClearAllPools();

            try
            {
                {
                    oldBackup.InitialDirectory = Environment.CurrentDirectory + "\\BackUp";
                    oldBackup.Title            = "Abrir arquivo de BackUp";
                    oldBackup.Filter           = "Arquivo de Backup DL 2020|*.dl";
                    if (oldBackup.ShowDialog(this) == DialogResult.OK)
                    {
                        if (oldBackup.FileName != string.Empty)
                        {
                            NomeArquivo = oldBackup.FileName;
                        }
                        else
                        {
                            MessageBox.Show("Selecione o backup para restaurar os dados", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                    else
                    {
                        return;
                    }
                }

                this.Cursor = Cursors.WaitCursor;


                string RestoreBanco = Environment.CurrentDirectory + "\\DL2020.fdb";
                //string conexao = @"User=SYSDBA;Password=masterkey;database = " + RestoreBanco + ";DataSource=localhost;Port=3050;Dialect=3;";

                string    conexao    = Firebird.Conn;
                FbRestore RestoreSvc = new FbRestore();
                RestoreSvc.ConnectionString = conexao;

                RestoreSvc.BackupFiles.Add(new FbBackupFile(NomeArquivo, 2048));
                RestoreSvc.Verbose  = true;
                RestoreSvc.PageSize = 4096;
                RestoreSvc.Options  = FbRestoreFlags.Create | FbRestoreFlags.Replace;

                RestoreSvc.Execute();

                MessageBox.Show("Backup restaurado com sucesso!", "Sucesso!", MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                if (backup != null)
                {
                    Firebird.SaveBackup(backup);
                }
                Application.Restart();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Erro ao restaurar Cópia de Segurança" + ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
Esempio n. 3
0
        private void CheckBackUp()
        {
            try
            {
                Backup backup = Firebird.RetornaBackup();
                if (backup != null)
                {
                    if (Directory.Exists(backup.CaminhoBackup))
                    {
                        switch (backup.Periodo)
                        {
                        case 0:
                            if (backup.DataUltimoBackup == "")
                            {
                                FbBackup backupSvc   = new FbBackup();
                                string   NomeArquivo = backup.CaminhoBackup + "/BackUp DL 2020 - Dia " +
                                                       DateTime.Today.Day.ToString("0,0") + "_" +
                                                       DateTime.Today.Month.ToString("0,0") + ".dl";
                                if (File.Exists(NomeArquivo))
                                {
                                    File.Delete(NomeArquivo);
                                }
                                backupSvc.ConnectionString = Firebird.Conn;
                                backupSvc.Verbose          = true;
                                backupSvc.BackupFiles.Add(new FbBackupFile(NomeArquivo, -1));
                                backupSvc.Execute();
                                string date = DateTime.Today.ToString();
                                backup.DataUltimoBackup = date;
                                Firebird.AlterarDataUltimoBackup(date);
                                Firebird.SaveBackup(backup);
                            }
                            else
                            {
                                DateTime data = Convert.ToDateTime(backup.DataUltimoBackup);
                                data = data.AddDays(+1);
                                if (DateTime.Today == data || DateTime.Today > data)
                                {
                                    FbBackup backupSvc   = new FbBackup();
                                    string   NomeArquivo = backup.CaminhoBackup + "/BackUp DL 2020 - Dia " +
                                                           DateTime.Today.Day.ToString("0,0") + "_" +
                                                           DateTime.Today.Month.ToString("0,0") + ".dl";
                                    if (File.Exists(NomeArquivo))
                                    {
                                        File.Delete(NomeArquivo);
                                    }
                                    backupSvc.ConnectionString = Firebird.Conn;
                                    backupSvc.Verbose          = true;
                                    backupSvc.BackupFiles.Add(new FbBackupFile(NomeArquivo, -1));
                                    backupSvc.Execute();
                                    string date = DateTime.Today.ToString();
                                    backup.DataUltimoBackup = date;
                                    Firebird.AlterarDataUltimoBackup(date);
                                    Firebird.SaveBackup(backup);
                                }
                            }
                            break;

                        case 1:
                            if (backup.DataUltimoBackup == "")
                            {
                                FbBackup backupSvc   = new FbBackup();
                                string   NomeArquivo = backup.CaminhoBackup + "/BackUp DL 2020 - Dia " +
                                                       DateTime.Today.Day.ToString("0,0") + "_" +
                                                       DateTime.Today.Month.ToString("0,0") + ".dl";
                                if (File.Exists(NomeArquivo))
                                {
                                    File.Delete(NomeArquivo);
                                }
                                backupSvc.ConnectionString = Firebird.Conn;
                                backupSvc.Verbose          = true;
                                backupSvc.BackupFiles.Add(new FbBackupFile(NomeArquivo, -1));
                                backupSvc.Execute();
                                string date = DateTime.Today.ToString();
                                backup.DataUltimoBackup = date;
                                Firebird.AlterarDataUltimoBackup(date);
                                Firebird.SaveBackup(backup);
                            }
                            else
                            {
                                DateTime data = Convert.ToDateTime(backup.DataUltimoBackup);
                                data = data.AddDays(+7);
                                if (DateTime.Today == data || DateTime.Today > data)
                                {
                                    FbBackup backupSvc   = new FbBackup();
                                    string   NomeArquivo = backup.CaminhoBackup + "/BackUp DL 2020 - Dia " +
                                                           DateTime.Today.Day.ToString("0,0") + "_" +
                                                           DateTime.Today.Month.ToString("0,0") + ".dl";
                                    if (File.Exists(NomeArquivo))
                                    {
                                        File.Delete(NomeArquivo);
                                    }
                                    backupSvc.ConnectionString = Firebird.Conn;
                                    backupSvc.Verbose          = true;
                                    backupSvc.BackupFiles.Add(new FbBackupFile(NomeArquivo, -1));
                                    backupSvc.Execute();
                                    string date = DateTime.Today.ToString();
                                    backup.DataUltimoBackup = date;
                                    Firebird.AlterarDataUltimoBackup(date);
                                    Firebird.SaveBackup(backup);
                                }
                            }
                            break;

                        case 2:
                            if (backup.DataUltimoBackup == "")
                            {
                                FbBackup backupSvc   = new FbBackup();
                                string   NomeArquivo = backup.CaminhoBackup + "/BackUp DL 2020 - Dia " +
                                                       DateTime.Today.Day.ToString("0,0") + "_" +
                                                       DateTime.Today.Month.ToString("0,0") + ".dl";
                                if (File.Exists(NomeArquivo))
                                {
                                    File.Delete(NomeArquivo);
                                }
                                backupSvc.ConnectionString = Firebird.Conn;
                                backupSvc.Verbose          = true;
                                backupSvc.BackupFiles.Add(new FbBackupFile(NomeArquivo, -1));
                                backupSvc.Execute();
                                string date = DateTime.Today.ToString();
                                backup.DataUltimoBackup = date;
                                Firebird.AlterarDataUltimoBackup(date);
                                Firebird.SaveBackup(backup);
                            }
                            else
                            {
                                DateTime data = Convert.ToDateTime(backup.DataUltimoBackup);
                                data = data.AddDays(+30);
                                if (DateTime.Today == data || DateTime.Today > data)
                                {
                                    FbBackup backupSvc   = new FbBackup();
                                    string   NomeArquivo = backup.CaminhoBackup + "/BackUp DL 2020 - Dia " +
                                                           DateTime.Today.Day.ToString("0,0") + "_" +
                                                           DateTime.Today.Month.ToString("0,0") + ".dl";
                                    if (File.Exists(NomeArquivo))
                                    {
                                        File.Delete(NomeArquivo);
                                    }
                                    backupSvc.ConnectionString = Firebird.Conn;
                                    backupSvc.Verbose          = true;
                                    backupSvc.BackupFiles.Add(new FbBackupFile(NomeArquivo, -1));
                                    backupSvc.Execute();
                                    string date = DateTime.Today.ToString();
                                    backup.DataUltimoBackup = date;
                                    Firebird.AlterarDataUltimoBackup(date);
                                    Firebird.SaveBackup(backup);
                                }
                            }
                            break;
                        }
                    }
                    else
                    {
                        DialogResult result = MessageBox.Show(
                            "O caminho para o backup automático não está disponivel, deseja continuar?",
                            "Erro", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (result == DialogResult.No)
                        {
                            Process.GetCurrentProcess().Kill();
                        }
                        else
                        {
                        }
                    }
                }
            }
            catch
            {
            }
        }