Example #1
0
 public static void SendBackupStatusMail(MailConfig objMailConfig, BackupSuccessLog objBackupSuccessLog)
 {
     foreach (var EmailTo in objMailConfig.Receipients.Receiver)
     {
         var MessageBody = FormMessageBody(objBackupSuccessLog);
         ExecuteBackupStatusMail(EmailTo, objMailConfig, MessageBody);
     }
 }
Example #2
0
 public static void DatabaseBackupLog(BackupSuccessLog objBackupSuccessLog)
 {
     try
     {
         var CurrentDirectory = Helper.AppConstants.ServiceConfigSavedPath + DateTime.Now.ToString("MMMM") + "\\";
         if (!Directory.Exists(CurrentDirectory))
         {
             Directory.CreateDirectory(CurrentDirectory);
         }
         var XmlString    = Helper.ToXml(objBackupSuccessLog);
         var EncryptedXml = CipherUtil.Encrypt(XmlString, Helper.AppConstants.CipherKey);
         File.WriteAllText(CurrentDirectory + Helper.TimeStamp + ".dat", EncryptedXml);
     }
     catch (Exception ex)
     {
         log.Error(ex);
     }
 }
Example #3
0
        private static string FormMessageBody(BackupSuccessLog objBackupSuccessLog)
        {
            string StatusTable = "";

            StatusTable += "<style type='text/css'>#StatusTable{ border-collapse: collapse !important;} #StatusTable td " +
                           "{border: 1px solid #cccccc !important; padding:5px;} #StatusTable thead{font-weight:bold; background-color:#fafafa;}</style>" +
                           "<table id='StatusTable' style='font-family: Arial !important; font-size: 13px !important'><thead>" +
                           "<tr><td>Database Name</td><td>Backup Date/Time</td><td style='width:200px'>Backup Location</td><td>Backup Type</td>" +
                           "<td>Backup Status</td></tr></thead>";

            for (var i = 0; i < objBackupSuccessLog.DatabaseName.Count; i++)
            {
                StatusTable += "<tr><td>" + objBackupSuccessLog.DatabaseName[i] + "</td><td> " + objBackupSuccessLog.DateTime[i] + "</td><td style='width:200px'>" + objBackupSuccessLog.Location[i] +
                               "</td><td>" + objBackupSuccessLog.BackupType[i] + "</td><td>" + objBackupSuccessLog.Status[i] + "</td></tr>";
            }
            StatusTable += "</table>";
            return(StatusTable);
        }
Example #4
0
        public static void BackupDatabase(BackupConfig objBackupConfig)
        {
            try
            {
                var    StatusObject     = new BackupSuccessLog();
                string filename         = "Database Backup Utility";
                string ConnectionString = "server=" + objBackupConfig.ServerCredentials.ServerName + ";uid=" +
                                          objBackupConfig.ServerCredentials.UserId + ";password="******";";

                string BackUpType = GetBackupType(objBackupConfig);
                string zipPath    = objBackupConfig.BackupLocation + @"\" + DateTime.Now.ToString("yyyyMMdd") + "_" + BackUpType;;
                foreach (var databaseName in objBackupConfig.BackupDatabase.DatabaseName)
                {
                    try
                    {
                        StatusObject.DatabaseName.Add(databaseName);
                        StatusObject.DateTime.Add(DateTime.Now.ToString(Helper.AppConstants.DateTimeFormat));
                        StatusObject.BackupType.Add(BackUpType);
                        string BackUpPath = objBackupConfig.BackupLocation + @"\" + DateTime.Now.ToString("yyyyMMdd") + "_" + BackUpType;

                        if (!Directory.Exists(BackUpPath))
                        {
                            Directory.CreateDirectory(BackUpPath);
                        }
                        BackUpPath += @"\" + databaseName + ".bak";
                        StatusObject.Location.Add(BackUpPath);
                        using (var connection = new SqlConnection(ConnectionString))
                        {
                            var query = "";
                            if (BackUpType == Helper.AppConstants.Full)
                            {
                                query = String.Format("BACKUP DATABASE {0} TO DISK = '{1}' WITH INIT, NAME= '[{2}]', NOSKIP, NOFORMAT ", databaseName, BackUpPath, filename);
                            }
                            else if (BackUpType == Helper.AppConstants.Differential)
                            {
                                query = String.Format(" select  database_name,type,backup_finish_date from  msdb.dbo.backupset where database_name='{0}' and type ='D'" +
                                                      "  if  @@ROWCOUNT > 0 " +
                                                      "BACKUP DATABASE {0} TO DISK = '{1}' WITH DIFFERENTIAL, NAME= '[{2}]', NOSKIP, NOFORMAT " +
                                                      " else " +
                                                      "BACKUP DATABASE {0} TO DISK = '{3}' WITH INIT, NAME= '[{2}]', NOSKIP, NOFORMAT ", databaseName, BackUpPath, filename, BackUpPath.Replace(".DIFF", ".FULL"));
                            }

                            using (var command = new SqlCommand(query, connection))
                            {
                                command.CommandTimeout = 0;
                                connection.Open();
                                command.ExecuteNonQuery();
                            }
                        }

                        log.Info("Backup Successfull.  Database : " + databaseName + ", Type : " + BackUpType + ", Backup Path : " + BackUpPath + " @ " + DateTime.Now.ToString(Helper.AppConstants.DateTimeFormat));
                        StatusObject.Status.Add("SuccessFull");
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex);
                        StatusObject.Status.Add("Failed");
                    }
                }
                var objMailConfig = Helper.GetMailConfig();
                if (!string.IsNullOrEmpty(objBackupConfig.BackupPassword))
                {
                    CreatePasswordProtectedZip(zipPath, objBackupConfig.BackupPassword);
                }
                Mailer.SendBackupStatusMail(objMailConfig, StatusObject);
                DatabaseBackupLog(StatusObject);
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }