예제 #1
0
        void DB_Backup_Running(Info_MySQL_DB new_DB, Info_MySQL_Instance instansce)
        {
            Compression_File Comp = new Compression_File();

            string    Mess = "";
            Exception er   = null; //gán cờ er bằng null chỉ thị ko có lỗi

            try                    // bắt đầu quá trình backup
            {
                //phương thức trả về 1 exception (er == null thì ko có lỗi), nếu có lỗi thì ném ra exception và dừng công việc backup
                er = new_DB.Excute_Backup_DB(instansce);
                if (er != null)
                {
                    throw new Exception("Error Backup Database", er);
                }
                else
                {
                    File_Read_Write.Write_File(AppDomain.CurrentDomain.BaseDirectory + @"Log\Service_Log.txt", DateTime.Now + ": Backup " + instansce.Server_Name + @" - " + new_DB.DBName + " Successfully!", true);
                }


                // đóng nén file
                er = Comp.Run_DongNen_DB(new_DB.DBName);
                if (er != null)
                {
                    throw new Exception("Error Compression file", er);
                }
                else
                {
                    File_Read_Write.Write_File(AppDomain.CurrentDomain.BaseDirectory + @"Log\Service_Log.txt", DateTime.Now + ": Compression " + instansce.Server_Name + @" - " + new_DB.DBName + ".zip Successfully!", true);
                }

                // tạo folder trong FTP server



                string path_save_inFTP = "BackupDB_zip/" + instansce.DisplayName + "/" + DateTime.Now.ToString("yyyy-MM-dd");
                er = FTPClient.CreateFolder(ConfigurationManager.AppSettings["FTP_SERVER_URI"].ToString(), ConfigurationManager.AppSettings["FTP_USER"].ToString(), ConfigurationManager.AppSettings["FTP_PASSWORD"].ToString(), path_save_inFTP);
                if (er != null)
                {
                    throw new Exception("Error Create folder FTP Server", er);
                }
                //else
                //{
                //    try
                //    {
                //        File_Read_Write.Write_File(AppDomain.CurrentDomain.BaseDirectory + @"Log\Service_Log.txt", DateTime.Now + ": Create folder " +path_save_inFTP+ " in " + ConfigurationManager.AppSettings["FTP_SERVER_URI"].ToString() + " Successfully!", true);
                //    }
                //    catch { }
                //}



                // upload file
                string file = AppDomain.CurrentDomain.BaseDirectory + @"Backup_zip\" + new_DB.DBName + ".zip";

                er = FTPClient.Upload(ConfigurationManager.AppSettings["FTP_SERVER_URI"].ToString() + path_save_inFTP, ConfigurationManager.AppSettings["FTP_USER"].ToString(), ConfigurationManager.AppSettings["FTP_PASSWORD"].ToString(), file);
                if (er != null)
                {
                    throw new Exception("Error Upload file to FTP Server", er);
                }
                else
                {
                    File_Read_Write.Write_File(AppDomain.CurrentDomain.BaseDirectory + @"Log\Service_Log.txt", DateTime.Now + ": Upload " + instansce.Server_Name + @" - " + new_DB.DBName + ".zip to " + ConfigurationManager.AppSettings["FTP_SERVER_URI"].ToString() + path_save_inFTP + " Successfully!", true);
                }

                //gửi mail
                SendEmail.Send_Email(new_DB.Email, null, "[Dashboard - RTS] Backup Database succufully!", "Backup Database " + instansce.Server_Name + @" - " + new_DB.DBName + " succufully!", false);


                //quá trình hoàn tất
            }
            catch (Exception io)
            {
                try
                {
                    if (io.InnerException.Message == "" || io.InnerException.Message == null)
                    {
                        Mess = io.Message;
                    }
                    else
                    {
                        Mess = io.Message + ": " + io.InnerException.Message;
                    }
                }
                catch { }

                //gửi mail
                SendEmail.Send_Email(new_DB.Email, null, "[Dashboard - RTS] Backup Database error!", "Server: " + instansce.Server_Name + "\nDatabase name: " + new_DB.DBName + "\n" + Mess, false);


                File_Read_Write.Write_File(AppDomain.CurrentDomain.BaseDirectory + @"Log\Service_Log.txt", DateTime.Now + ": ----------------------------------------- ", true);
                File_Read_Write.Write_File(AppDomain.CurrentDomain.BaseDirectory + @"Log\Service_Log.txt", DateTime.Now + ": Error - " + new_DB.DBName + ": " + Mess, true);
                File_Read_Write.Write_File(AppDomain.CurrentDomain.BaseDirectory + @"Log\Service_Log.txt", DateTime.Now + ": ----------------------------------------- ", true);
            }
        }