Example #1
0
        public void LoadErrorServer(Bkp_Tb_Server Bkp_Tb_Server1)
        {
            Lst_ServerError.Add(Bkp_Tb_Server1);
            ResutltMessage(Bkp_Tb_Server1.xServerIP.ToString() + "|" + "تلاش مجدد ...");
            Thread Thread1 = new Thread(() => Connect(Bkp_Tb_Server1));

            Thread1.Name += Bkp_Tb_Server1.xServerId_pk.ToString();
            Thread1.Start();
            Thread.Sleep(2000);
        }
 partial void DeleteBkp_Tb_Server(Bkp_Tb_Server instance);
 partial void UpdateBkp_Tb_Server(Bkp_Tb_Server instance);
 partial void InsertBkp_Tb_Server(Bkp_Tb_Server instance);
Example #5
0
        public void Connect(Bkp_Tb_Server item)
        {
            IntPtr                      admin_token = default(IntPtr);
            WindowsIdentity             wid_current = WindowsIdentity.GetCurrent();
            WindowsIdentity             wid_admin   = null;
            WindowsImpersonationContext wic         = null;

            ResutltMessage(item.xServerIP.ToString() + "|" + "در حال برقراری ارتباط...");
            string Str_Directory  = "";
            string Str_FolderName = "";

            try
            {
                if (LogonUser(item.xServerAdminName, item.xServerDomainName, item.xServerAdminPassword, 9, 0, ref admin_token) != 0)
                {
                    wid_admin = new WindowsIdentity(admin_token);
                    wic       = wid_admin.Impersonate();
                    string Str_Domain = item.xServerDomainName.Split('.')[0];
                    string Str_Drive  = item.xServerDriveToBackup.Trim();
                    Str_FolderName = Str_Domain + "_" + Class_ShamsiDateTime.MilladiToShamsi(DateTime.Now).ToString().Substring(8, 2);
                    Str_Directory  = "\\\\" + item.xServerIP + "\\" + Str_Drive + "$\\Backup\\";

                    //Create Directory For Backup
                    try
                    {
                        //if (System.IO.Directory.Exists(Str_Directory + Str_FolderName))
                        //{
                        //    string[] Str_Files = System.IO.Directory.GetFiles(Str_Directory + Str_FolderName);
                        //    foreach (string file in Str_Files)
                        //        System.IO.File.Delete(file);
                        //}
                        //else
                        //{
                        //    System.IO.Directory.CreateDirectory(Str_Directory + Str_FolderName);
                        //}

                        //Create Or Open Txt File
                        //StreamWriter StreamWriter1 = new StreamWriter(Str_Directory + "AutoBackup_AddressFileToZip.txt");
                        //StreamWriter1.Write(Str_FolderName);
                        //StreamWriter1.Close();

                        //Copy Consol Zip And DLL
                        if (item.xServerDriveToBackup.Trim() == "C")
                        {
                            if (!File.Exists(Str_Directory + "Console_ZipOnC.exe"))
                            {
                                System.IO.File.Copy("E:\\Sources\\Console_ZipOnC.exe", Str_Directory + "Console_ZipOnC.exe", true);
                            }
                        }
                        else if (item.xServerDriveToBackup.Trim() == "D")
                        {
                            if (!File.Exists(Str_Directory + "Console_ZipOnD.exe"))
                            {
                                System.IO.File.Copy("E:\\Sources\\Console_ZipOnD.exe", Str_Directory + "Console_ZipOnD.exe", true);
                            }
                        }
                        else if (item.xServerDriveToBackup.Trim() == "E")
                        {
                            if (!File.Exists(Str_Directory + "Console_ZipOnE.exe"))
                            {
                                System.IO.File.Copy("E:\\Sources\\Console_ZipOnE.exe", Str_Directory + "Console_ZipOnE.exe", true);
                            }
                        }
                        else if (item.xServerDriveToBackup.Trim() == "F")
                        {
                            if (!File.Exists(Str_Directory + "Console_ZipOnF.exe"))
                            {
                                System.IO.File.Copy("E:\\Sources\\Console_ZipOnF.exe", Str_Directory + "Console_ZipOnF.exe", true);
                            }
                        }
                        if (!File.Exists(Str_Directory + "ICSharpCode.SharpZipLib.dll"))
                        {
                            System.IO.File.Copy("E:\\Sources\\ICSharpCode.SharpZipLib.dll", Str_Directory + "ICSharpCode.SharpZipLib.dll", true);
                        }
                    }
                    catch (Exception ex)
                    {
                        ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا درانتقال فایل : " + ex.ToString());
                    }



                    ////Create Connection And open to backup
                    string Str_ConnectionString =
                        "Data Source=" + item.xServerIP + (item.xServerSQLInstance != null ? ("\\" + item.xServerSQLInstance) : "") +
                        ";User ID=" + item.xServerSQLAdminName +
                        ";password="******";Integrated Security=True;Connect Timeout=120";
                    SqlConnection Sql_conn = new SqlConnection(Str_ConnectionString);
                    SqlCommand    Sql_Comm = new SqlCommand();
                    Sql_Comm.Connection = Sql_conn;
                    try
                    {
                        Sql_conn.Open();
                        ResutltMessage(item.xServerIP.ToString() + "|" + "ارتباط موفق با بانک اطلاعاتی");
                    }
                    catch
                    {
                        ResutltMessage(item.xServerIP.ToString() + "|" + "ارتباط ناموفق با بانک اطلاعاتی");
                        if (!Lst_ServerError.Contains(item))
                        {
                            LoadErrorServer(item);
                        }
                        else
                        {
                            LoadServer(1);
                            return;
                        }
                    }

                    //create directory
                    string Str_CreateDirectory = " EXEC master..xp_CMDShell 'mkdir " + item.xServerDriveToBackup.Trim() +
                                                 ":\\Backup\\" + Str_FolderName + "' ";

                    Sql_Comm.CommandText    = Str_CreateDirectory;
                    Sql_Comm.CommandTimeout = 0;
                    try
                    {
                        Sql_Comm.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        Str_CreateDirectory = "EXEC sp_configure 'show advanced options', 1 " +
                                              " RECONFIGURE EXEC sp_configure 'xp_cmdshell', 1 " +
                                              " RECONFIGURE  " +
                                              " EXEC master..xp_CMDShell 'mkdir " + item.xServerDriveToBackup.Trim() +
                                              ":\\Backup\\" + Str_FolderName + "' ";

                        Sql_Comm.CommandText    = Str_CreateDirectory;
                        Sql_Comm.CommandTimeout = 0;
                        Sql_Comm.ExecuteNonQuery();
                        ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا : " + ex.ToString());
                    }

                    //delete all files in directory
                    string Str_DeleteDirectoryFiles = " EXEC master..xp_CMDShell 'del " + item.xServerDriveToBackup.Trim() +
                                                      ":\\Backup\\" + Str_FolderName + "\\*.bak' ";

                    Sql_Comm.CommandText    = Str_DeleteDirectoryFiles;
                    Sql_Comm.CommandTimeout = 0;
                    try
                    {
                        Sql_Comm.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا : " + ex.ToString());
                    }

                    Lts_BackupDataContext  Lts_Backup    = new Lts_BackupDataContext();
                    List <Bkp_Tb_DataBase> Lst_DataBases = Lts_Backup.Bkp_Tb_ServerDataBases.Where(b =>
                                                                                                   b.xServerId_fk == item.xServerId_pk).Select(b => b.Bkp_Tb_DataBase).ToList();
                    string Str_Databases = "";
                    foreach (Bkp_Tb_DataBase DatabaseName in Lst_DataBases)
                    {
                        Str_Databases += "'" + DatabaseName.xDataBaseName + "',";
                    }
                    Str_Databases = Str_Databases.Substring(0, Str_Databases.Length - 1);

                    string Str_BackupQuery = "DECLARE @name VARCHAR(50) " +
                                             "DECLARE @path VARCHAR(256) " +
                                             "DECLARE @fileName VARCHAR(256) " +
                                             "DECLARE @fileDate VARCHAR(20) " +
                                             "SET @path ='" + item.xServerDriveToBackup.Trim() + ":\\" + "Backup\\" + Str_FolderName + "\\'" +
                                             " SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112) " +
                                             "DECLARE db_cursor CURSOR FOR " +
                                             "SELECT name " +
                                             "FROM master.dbo.sysdatabases " +
                                             "WHERE name  IN (" + Str_Databases + ") " +
                                             "OPEN db_cursor " +
                                             "FETCH NEXT FROM db_cursor INTO @name " +
                                             "WHILE @@FETCH_STATUS = 0 " +
                                             "BEGIN " +
                                             "SET @fileName = @path + @name   + '.bak' " +
                                             "BACKUP DATABASE @name TO DISK = @fileName " +
                                             "FETCH NEXT FROM db_cursor INTO @name " +
                                             "END " +
                                             "CLOSE db_cursor " +
                                             "DEALLOCATE db_cursor";

                    Sql_Comm.CommandText    = Str_BackupQuery;
                    Sql_Comm.CommandTimeout = 0;

                    ResutltMessage(item.xServerIP.ToString() + "|" + "شروع عملیات پشتیبان گیری در : " + DateTime.Now.ToShortTimeString());

                    try
                    {
                        Sql_Comm.ExecuteNonQuery();
                        ResutltMessage(item.xServerIP.ToString() + "|" + "اتمام عملیات پشتیبان گیری در : " + DateTime.Now.ToShortTimeString());
                    }
                    catch (Exception ex)
                    {
                        ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا : " + ex.ToString());
                        if (!Lst_ServerError.Contains(item))
                        {
                            LoadErrorServer(item);
                        }
                        else
                        {
                            LoadServer(1);
                            return;
                        }
                    }


                    //StreamReader StreamReader1 = new StreamReader(Str_Directory + "AutoBackup_AddressFileToZip.txt");
                    //string Str_Msg = StreamReader1.ReadLine();
                    //StreamReader1.Close();
                    //StreamWriter StreamWriter2 = new StreamWriter(Str_Directory + "AutoBackup_AddressFileToZip.txt");
                    //StreamWriter2.Write(Str_Msg + "&" + "Backup Success");
                    //StreamWriter2.Close();
                    //
                    string Str_WriteFileName = " EXEC master..xp_CMDShell 'echo " + Str_FolderName + "^&" + "Backup Success > " + item.xServerDriveToBackup.Trim() +
                                               ":\\Backup\\AutoBackup_AddressFileToZip.txt' ";
                    Sql_Comm.CommandText    = Str_WriteFileName;
                    Sql_Comm.CommandTimeout = 0;
                    try
                    {
                        Sql_Comm.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا : " + ex.ToString());
                    }


                    //
                    ResutltMessage(item.xServerIP.ToString() + "|" + "شروع عملیات فشرده سازی در : " + DateTime.Now.ToShortTimeString());

                    Str_BackupQuery = " EXEC master..xp_CMDShell '" + item.xServerDriveToBackup.Trim() +
                                      ":\\Backup\\Console_ZipOn" + item.xServerDriveToBackup.Trim() + ".exe' ";
                    Sql_Comm.CommandText    = Str_BackupQuery;
                    Sql_Comm.CommandTimeout = 0;
                    try
                    {
                        Sql_Comm.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا : " + ex.ToString());
                    }


                    ResutltMessage(item.xServerIP.ToString() + "|" + "اتمام عملیات فشرده سازی در : " + DateTime.Now.ToShortTimeString());

                    //del files
                    string Str_DeleteDirectoryFiles1 = " EXEC master..xp_CMDShell 'del " + item.xServerDriveToBackup.Trim() +
                                                       ":\\Backup\\" + Str_FolderName + "\\*.bak' ";

                    Sql_Comm.CommandText    = Str_DeleteDirectoryFiles1;
                    Sql_Comm.CommandTimeout = 0;
                    try
                    {
                        Sql_Comm.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا : " + ex.ToString());
                        return;
                    }

                    //del dir
                    string Str_DeleteDirectory = " EXEC master..xp_CMDShell 'RMDIR " + item.xServerDriveToBackup.Trim() +
                                                 ":\\Backup\\" + Str_FolderName + "' ";

                    Sql_Comm.CommandText    = Str_DeleteDirectory;
                    Sql_Comm.CommandTimeout = 0;
                    try
                    {
                        Sql_Comm.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا : " + ex.ToString());
                    }

                    Sql_conn.Close();


                    //
                    ResutltMessage(item.xServerIP.ToString() + "|" + "شروع عملیات کپی در : " + DateTime.Now.ToShortTimeString());
                    if (!Directory.Exists("G:\\Bkp_" + Class_ShamsiDateTime.MilladiToShamsi(DateTime.Now).ToString().Substring(5, 2) + Class_ShamsiDateTime.MilladiToShamsi(DateTime.Now).ToString().Substring(8, 2)))
                    {
                        Directory.CreateDirectory("G:\\Bkp_" + Class_ShamsiDateTime.MilladiToShamsi(DateTime.Now).ToString().Substring(5, 2) + Class_ShamsiDateTime.MilladiToShamsi(DateTime.Now).ToString().Substring(8, 2));
                    }
                    try
                    {
                        File.Copy(Str_Directory + Str_FolderName + ".zip", "G:\\Bkp_" + Class_ShamsiDateTime.MilladiToShamsi(DateTime.Now).ToString().Substring(5, 2) + Class_ShamsiDateTime.MilladiToShamsi(DateTime.Now).ToString().Substring(8, 2) + "\\" + Str_FolderName + ".zip", true);
                        ResutltMessage(item.xServerIP.ToString() + "|" + "اتمام عملیات  کپی در : " + DateTime.Now.ToShortTimeString());
                        try
                        {
                            File.Delete(Str_Directory + Str_FolderName + ".zip");
                        }
                        catch { }
                    }
                    catch
                    {
                        ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا در عملیات  کپی : ");
                        ResutltMessage(item.xServerIP.ToString() + "|" + "!تلاش مجدد برای عملیات  کپی : ");
                        try
                        {
                            File.Copy(Str_Directory + Str_FolderName + ".zip", "G:\\Bkp_" + Class_ShamsiDateTime.MilladiToShamsi(DateTime.Now).ToString().Substring(5, 2) + Class_ShamsiDateTime.MilladiToShamsi(DateTime.Now).ToString().Substring(8, 2) + "\\" + Str_FolderName + ".zip", true);
                            ResutltMessage(item.xServerIP.ToString() + "|" + "اتمام عملیات  کپی در : " + DateTime.Now.ToShortTimeString());
                            try
                            {
                                File.Delete(Str_Directory + Str_FolderName + ".zip");
                            }
                            catch { }
                        }
                        catch
                        {
                            ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا مجدد در عملیات  کپی : ");
                        }
                    }

                    LoadServer(1);
                }
                //}
                //catch (Exception ex)
                //{
                //    ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا : " + ex.ToString());
                //    //LoadServer(1);
                //    //if (Directory.Exists(Str_Directory + Str_FolderName))
                //    //    try
                //    //    {
                //    //        Directory.Delete(Str_Directory + Str_FolderName, true);
                //    //    }
                //    //    catch { }
                //    //if (File.Exists(Str_Directory + Str_FolderName + ".zip"))
                //    //    try
                //    //    {
                //    //        File.Delete(Str_Directory + Str_FolderName + ".zip");
                //    //    }
                //    //    catch { }
                //    if (!Lst_ServerError.Contains(item))
                //    {
                //        LoadErrorServer(item);
                //    }
                //}
            }
            catch (Exception ex)
            {
                ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا : " + ex.ToString());
                LoadServer(1);
            }
        }