akshintlakalyan 0 Newbie Poster

I have written to take Backup of any database automatically, at any time given by the user in config file. It's working fine, now i wish to take the Backup of a certain database which was already taken Backup previously for the next time. Any help.

private void FrmRestoreBackup_Load(object sender, EventArgs e)
         {
             lstDetails.Items.Clear();
             lstDetails.FullRowSelect = true;
             CheckForIllegalCrossThreadCalls = false;
             txtusr.ReadOnly = true;
             txtpwd.ReadOnly = true;
             radio_tab1_Sql.Enabled = false;
             radio_tab1_Window.Enabled = false;
             drplist_Servername2.Enabled = false;
             drplist_Database2.Enabled = false;
             btnRefresh1.Enabled = false;
             chkBack.Enabled = false;
             WriteToListView("Backup Restore Page Loaded");
            drpllist_ServerName1.Items.Clear();
            this.Show();
            listView1.Items.Clear();
            DataTable dt = GetDetails();
            WriteToListView("Loading the XML File Values to the ListView");
            listView1.BeginUpdate();
            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem listItem = new ListViewItem(dr[0].ToString());
                listItem.SubItems.Add(dr[1].ToString());
                listItem.SubItems.Add(dr[2].ToString());
                listItem.SubItems.Add(dr[3].ToString());
                listItem.SubItems.Add(dr[4].ToString());
                listItem.SubItems.Add(dr[5].ToString());
                listView1.Items.Add(listItem);
            }
            if (listView1.Columns.Count == 0)
            {
                listView1.Columns.Add("ServerName", 80, HorizontalAlignment.Left);
                listView1.Columns.Add("DataBase", 70, HorizontalAlignment.Left);
                listView1.Columns.Add("Authentication_Type", 100, HorizontalAlignment.Left);
                listView1.Columns.Add("User_Id", 70, HorizontalAlignment.Left);
                listView1.Columns.Add("Password", 80, HorizontalAlignment.Left);
                listView1.Columns.Add("Backup_Enabled", 90, HorizontalAlignment.Left);
            }
            listView1.EndUpdate();
            WriteToListView("ListView is Loaded with existing DataBase Details");
            listView1.FullRowSelect = true;
            if (radiobtn_SQL_Server1.Checked == true)
            {
                txtUsrName.Enabled = true;
                txtPasword.Enabled = true;
                lbl_Username.Enabled = true;
                lbl_password.Enabled = true;
            }
            else
            {
                txtUsrName.Enabled = false;
                txtPasword.Enabled = false;
                lbl_Username.Enabled = false;
                lbl_password.Enabled = false;
            }
          
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = 5000;
            aTimer.Enabled = true;
         }
private void OnTimedEvent(object source, ElapsedEventArgs e)
         {
             try
             {
                DataTable tb = GetDetails();
                int RCnt = tb.Rows.Count;
                string[] SName = new string[RCnt];
                string[] DName = new string[RCnt];
                string[] UID = new string[RCnt];
                string[] PWD = new string[RCnt];
                string[] BEnabled = new string[RCnt];
                string[] AuthType = new string[RCnt];
                string[] FilePath = new string[RCnt];
                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    SName[i] =(string)tb.Rows[i][0]; 
                    DName[i] = (string)tb.Rows[i][1]; 
                    UID[i] = (string)tb.Rows[i][3]; 
                    PWD[i] = (string)tb.Rows[i][4]; 
                    BEnabled[i] = (string)tb.Rows[i][5];
                    AuthType[i] = (string)tb.Rows[i][2];
                }
                if (DateTime.Now.ToString("HH:mm") == Time_Config)
                {
                    aTimer.Enabled = false;
                    WriteToListView("Time is Matched to take Backup");
                    //DirectoriesChecked = false;
                     for (int i = 0; i < tb.Rows.Count; i++)
                     {
                        if (BEnabled[i] == "true")
                        {
                            if (AuthType[i] == "SQL Server Authentication")
                            {
                                DirectoriesChecked = false;
                                Thread.Sleep(3000);
                                DB = DName[i];
                                BackupDatabase(DName[i], SName[i], DPath , UID[i], PWD[i],Time_Config);
                                Thread.Sleep(100);
                               
                            }
                            else if (AuthType[i] == "Windows Authentication")
                            {
                                DirectoriesChecked = false;
                                Thread.Sleep(3000);
                                DB = DName[i];
                                BackupDatabase1(DName[i], SName[i], DPath,Time_Config);
                                Thread.Sleep(100);
                                
                            }
                        }
                    }
                     WriteToListView("Backup Check is Completed for all the Databases");
                }
                Thread.Sleep(40000);
                //if (DirectoriesChecked == false)
                //{
                //    WriteToListView("Backup failed for Database " + DB.ToString());
                //    writetoLog("Backup failed for Database " + DB.ToString());
                //}
                //else
                //{
                //    WriteToListView("Success");
                //}
                ProcessDir(DPath);
                Thread.Sleep(80000);
                aTimer.Enabled = true;
             }
             catch (Exception ex)
             {
                 writetoLog(ex.Message.ToString());
                 WriteToListView(ex.Message.ToString());
             }
         }
public void BackupDatabase1(string databasename, string Servername, string DestinationPath,string ConfigTIme)
        {
            SqlConnection con = new SqlConnection();
            try
            {
                WriteToListView("Taking the Backup for " + databasename );
                string backUpDatabase = databasename;
                srv = new Server(Servername);
                srv.ConnectionContext.LoginSecure = true;
                Backup bd = new Backup();
                bd.Action = BackupActionType.Database;
                bd.Database = backUpDatabase;
                if (!(Directory.Exists(DestinationPath + "\\" + databasename + "bak")))
                {
                    newPath = Path.Combine(DestinationPath, databasename + "bak");
                    Directory.CreateDirectory(newPath);
                }
                else
                {
                    newPath = Path.Combine(DestinationPath, databasename + "bak");
                }
                string FilePath = newPath + "\\" + databasename + DateTime.Now.ToString("dd-MM-yy HH-mm") + ".bak";
                if (DestinationPath != "" && FilePath.EndsWith(".bak"))
                {
                        bkpDevice = new BackupDeviceItem(FilePath , DeviceType.File);
                }
                else
                {
                    writetoLog("Invalid File Selection");
                    return;
                }
                bd.Devices.Add(bkpDevice);
                bd.PercentComplete += new PercentCompleteEventHandler(Auto_PercentComplete);
                //Thread.Sleep(200);
                //bd.Complete += new ServerMessageEventHandler(Auto_Complete);
                bd.SqlBackupAsync(srv);
            }
            catch (Exception ex)
            {
                if (srv.ConnectionContext.IsOpen)
                    srv.ConnectionContext.Disconnect();
                writetoLog(ex.Message.ToString());
                WriteToListView(ex.Message.ToString());
            }
        }