Пример #1
0
        /// <summary>
        /// Invoke the whole backup process - get sources, discover files, copy to destination
        /// </summary>
        static public void InvokeBackup()
        {
            try
            {
                BackupStarted?.Invoke();

                //while (!cts.IsCancellationRequested)
                //{
                //    Thread.Sleep(1000);
                //    Debug.WriteLine("WORKING");
                //}
                //Debug.WriteLine("COMPLETE");
                //isWorking = false;
                //return;

                var sources = GetSources();
                var files   = DiscoverFiles(sources);
                SaveDiscoveredFiles(files);
                CopyFiles();
                UpdateTimestamp(sources);

                BackupCompleted?.Invoke();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                BackupError?.Invoke(ex.Message);
            }
        }
Пример #2
0
        static void GetFiles(string path, string pattern, List <string> list)
        {
            try
            {
                DirectoryInfo info      = new DirectoryInfo(path);
                var           isDir     = info.Attributes.HasFlag(FileAttributes.Directory);
                var           isHidden  = info.Attributes.HasFlag(FileAttributes.Hidden);
                var           isSys     = info.Attributes.HasFlag(FileAttributes.System);
                var           isReparse = info.Attributes.HasFlag(FileAttributes.ReparsePoint);
                if (isSys || isHidden /*|| isReparse*/)
                {
                    return;
                }

                list.AddRange(Directory.GetFiles(path, pattern));

                var dirs = Directory.GetDirectories(path);
                foreach (var d in dirs)
                {
                    GetFiles(d, pattern, list);
                }
            }
            catch (Exception ex)
            {
                DirectoryInfo info = new DirectoryInfo(path);
                var           att1 = info.Attributes.HasFlag(FileAttributes.Directory);

                Debug.WriteLine(ex.Message);
                BackupError?.Invoke(ex.Message);
            }
        }
Пример #3
0
        /// <summary>
        /// Issue a request to get Glacier inventory or process existing request
        /// </summary>
        public static async Task <GlacierResult> GetGlacierInventoryAsync(Settings settings)
        {
            #region pseudocode
            // Is there an inventory file?
            // Yes: return Inventory and issue new request
            // Not found:
            //   Is an inventory request issued?
            //      Yes: get request details
            //      No: Issue request and save SNS topic, Job ID, Queue URL
            // Poll topic for response
            // Poll Message received?
            //    Yes: serialize inventory to file and return
            //    No: return details of inventory request (in progress)
            #endregion
            try
            {
                Topic topic       = GetExistingTopic(InventoryTopicFileName);
                var   updateIsDue = IsInventoryUpdateDue(settings);
                if (topic == null && updateIsDue)
                {
                    // Issue new request and serialize topic details to global file
                    topic = await SetupTopicAndSubscriptionsAsync(InventoryTopicFileName, GetTempDirectory(), settings.AWSGlacierRegion.SystemName, null, InventoryFileName);

                    topic.Type        = "inventory-retrieval";
                    topic.Description = "This job is to download a vault inventory";
                    InitiateGlacierJob(topic);
                    settings.InventoryUpdateRequested = DateTime.Now;
                    SaveSettingsAsync(settings);
                    return(topic.Status); // only requested - no need to process yet
                }
                if (!updateIsDue)
                {
                    return(GlacierResult.NoJob);
                }

                var result = await ProcessQueueAsync(topic);

                Debug.WriteLine("ProcessQueue result: " + result);
                if (result == GlacierResult.Completed)
                {
                    DownloadSuccess?.Invoke("Glacier Inventory was updated");
                }

                return(topic.Status);
            }
            catch (Exception ex)
            {
                BackupError?.Invoke(ex.Message);
                return(GlacierResult.Error);
            }
        }
Пример #4
0
 /// <summary>
 /// SQLs the backup on percent complete.
 /// </summary>
 /// <param name="sender">The sender.</param>
 /// <param name="e">The <see cref="PercentCompleteEventArgs"/> instance containing the event data.</param>
 private static void SqlBackupOnPercentComplete(object sender, PercentCompleteEventArgs e)
 {
     if (e.Error != null)
     {
         BackupPercentage?.Invoke(null, e.Percent);
     }
     else
     {
         if (e.Error != null)
         {
             BackupError?.Invoke(null, e.Error.Message);
         }
     }
 }
Пример #5
0
        /// <summary>
        /// Invoke backup of the discovered files list, possibly modified by user
        /// </summary>
        static public void InvokeBackupFromUser()
        {
            try
            {
                BackupStarted?.Invoke();

                CopyFiles();

                UpdateTimestamp(GetSources());

                BackupCompleted?.Invoke();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                BackupError?.Invoke(ex.Message);
            }
        }
Пример #6
0
        /// <summary>
        /// Get a Glacier archive
        /// </summary>
        public static async Task <GlacierResult> RequestGlacierArchiveAsync(string archiveId, string downloadDirectory, string filename)
        {
            #region pseudocode
            // Is there an archive request issued (check for file)?
            // Yes: Poll queue for response
            // No: create a request and persist details to file
            // Poll topic
            // Message received?
            // Yes: serialise job to filePath
            // No: NOP
            #endregion
            try
            {
                string topicFileName = GetArchiveTopicFileName(archiveId);
                Topic  topic         = GetExistingTopic(topicFileName);
                if (topic == null)
                {
                    var settings = GetSettingsAsync().Result;
                    // Issue new request and serialize topic details to file
                    topic = await SetupTopicAndSubscriptionsAsync(topicFileName, downloadDirectory, settings.AWSGlacierRegion.SystemName, archiveId, filename);

                    topic.Type        = "archive-retrieval";
                    topic.Description = "This job is to download a vault archive";
                    topic.ArchiveId   = archiveId;
                    InitiateGlacierJob(topic);
                }
                //ProcessQueue(topic);
                //if (topic.Status == GlacierResult.Completed)
                //    DownloadSuccess?.Invoke($"Backup downloaded {topic.OutputPath}");

                return(topic.Status);
            }
            catch (Exception ex)
            {
                BackupError?.Invoke(ex.Message);
                return(GlacierResult.Error);
            }
        }
Пример #7
0
        public static GlacierResult ProcessQueue(Topic topic)
        {
            // Check for notifications on topic and process any message
            try
            {
                var settings = SettingsManager.GetSettings();
                using (var client = new AmazonGlacierClient(
                           settings.AWSAccessKeyID,
                           settings.AWSSecretAccessKey,
                           RegionEndpoint.GetBySystemName(settings.AWSS3Region.SystemName)))
                {
                    var receiveMessageRequest = new ReceiveMessageRequest {
                        QueueUrl = topic.QueueUrl, MaxNumberOfMessages = 1
                    };
                    var sqsClient = new AmazonSQSClient(settings.AWSAccessKeyID, settings.AWSSecretAccessKey, RegionEndpoint.GetBySystemName(settings.AWSS3Region.SystemName));
                    var receiveMessageResponse = sqsClient.ReceiveMessage(receiveMessageRequest);
                    if (receiveMessageResponse.Messages.Count == 0)
                    {
                        topic.Status = GlacierResult.Incomplete;
                        SaveTopicFile(topic);
                        return(topic.Status);
                    }

                    // Process message
                    string status = GetResponseStatus(receiveMessageResponse);
                    if (string.Equals(status, GlacierUtils.JOB_STATUS_SUCCEEDED, StringComparison.InvariantCultureIgnoreCase))
                    {
                        DownloadGlacierJobOutput(topic.JobId, client, settings.AWSGlacierVault, topic.GetOutputFile());
                        Debug.WriteLine($"Downloaded job output to {topic.GetOutputFile()}");
                        if (topic.ArchiveId != null)
                        {
                            DownloadSuccess?.Invoke($"Glacier archive was downloaded to {topic.GetOutputFile()}");
                        }
                        DeleteTopic(topic);
                        return(GlacierResult.Completed);
                    }
                    else if (string.Equals(status, GlacierUtils.JOB_STATUS_FAILED, StringComparison.InvariantCultureIgnoreCase))
                    {
                        DownloadError?.Invoke("Job failed, cannot download the file");
                        DeleteTopic(topic);
                        return(GlacierResult.JobFailed);
                    }
                    else if (string.Equals(status, GlacierUtils.JOB_STATUS_INPROGRESS, StringComparison.InvariantCultureIgnoreCase))
                    {
                        DownloadWarning?.Invoke("Job in progress, Queue ARN: " + topic.QueueARN);
                        DeleteTopic(topic);
                        return(GlacierResult.JobInProgress);
                    }
                    else
                    {
                        DeleteTopic(topic);
                        return(GlacierResult.Error);
                    }
                }
            }
            catch (AmazonServiceException azex)
            {
                // Handle specific potential errors here
                Debug.WriteLine("AmazonServiceException " + azex.Message);

                if (azex.StatusCode == System.Net.HttpStatusCode.Forbidden)
                {
                    // Invalid credentials
                    BackupError?.Invoke("Invalid AWS credentials were provided while connecting");
                    return(GlacierResult.Incomplete);
                }
                if (azex.InnerException != null &&
                    azex.InnerException is System.Net.WebException &&
                    ((System.Net.WebException)azex.InnerException).Status == System.Net.WebExceptionStatus.NameResolutionFailure)
                {
                    // Not connected to internet
                    BackupError?.Invoke("Network connection failure");
                    return(GlacierResult.Incomplete);
                }
                if (azex.InnerException != null &&
                    azex.InnerException is System.Net.WebException)
                {
                    // Network errors
                    BackupError?.Invoke($"A network error occurred ({((System.Net.WebException)azex.InnerException).Status})");
                    return(GlacierResult.Incomplete);
                }
                if (azex.StatusCode == System.Net.HttpStatusCode.BadRequest
                    //&& topic.Status == GlacierResult.JobRequested
                    && azex.Message.Contains("The specified queue does not exist") &&
                    DateTime.Now - topic.DateRequested < new TimeSpan(24, 0, 0))
                {
                    // Job was recently requested and the queue has not been created yet
                    Debug.WriteLine("Job request may be in progress");
                    return(GlacierResult.JobRequested);
                }

                // TODO Check expiry?
                // Glacier ref: "A job ID will not expire for at least 24 hours after Amazon Glacier completes the job."
                DeleteTopic(topic);
                BackupWarning?.Invoke("An AWS Glacier job has expired, a new job will be issued");

                // Reissue expired job
                InitiateGlacierJob(topic);
                return(topic.Status);
            }
            catch (Exception ex)
            {
                DeleteTopic(topic);
                throw ex;
            }
        }
Пример #8
0
 public void Init()
 {
     _backupError = new BackupError(SOURCE, DESTINATION, new ArgumentException(EXCEPTION_MESSAGE));
 }
Пример #9
0
        /// <summary>
        /// Backup task.
        /// </summary>
        /// <param name="dbName">Name of the database.</param>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="destPath">The dest path.</param>
        /// <param name="instaceName">Name of the instace.</param>
        private async static void BackupTask(string dbName, string fileName, string destPath, string instaceName)
        {
            Log.NewLog(LogType.Info, "Start SQL Backup Task");
            Backup sqlBackup = new Backup();

            try
            {
                ////Define Server connection
                //ServerConnection connection = new ServerConnection(frm.serverName, frm.userName, frm.password);
                ServerConnection connection = new ServerConnection(instaceName);

                ////To Avoid TimeOut Exception
                Server sqlServer = new Server(connection);
                sqlServer.ConnectionContext.StatementTimeout = 60 * 60;
                Database db = sqlServer.Databases[dbName];

                sqlBackup.Initialize         = true;
                sqlBackup.Checksum           = true;
                sqlBackup.ContinueAfterError = true;

                if (db.Size < PathUtility.Information.GetFreeSpace(Path.GetPathRoot(destPath)))
                {
                    ////Specify the type of backup, the description, the name, and the database to be backed up.
                    sqlBackup.Action = BackupActionType.Database;
                    sqlBackup.BackupSetDescription = "BackUp of:" + dbName + "on" + DateTime.Now.ToShortDateString();
                    sqlBackup.BackupSetName        = "FullBackUp";
                    sqlBackup.Database             = dbName;

                    ////Declare a BackupDeviceItem
                    string           destinationPath = destPath;
                    string           backupfileName  = fileName + "_" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + " " + DateTime.Now.Hour + DateTime.Now.Minute + ".bak";
                    BackupDeviceItem deviceItem      = new BackupDeviceItem(destinationPath + "\\" + backupfileName, DeviceType.File);


                    ////Add the device to the Backup object.
                    sqlBackup.Devices.Add(deviceItem);
                    ////Set the Incremental property to False to specify that this is a full database backup.
                    sqlBackup.Incremental = false;

                    //sqlBackup.ExpirationDate = DateTime.Now.AddDays(3);

                    ////Specify that the log must be truncated after the backup is complete.
                    sqlBackup.LogTruncation = BackupTruncateLogType.Truncate;

                    sqlBackup.FormatMedia = false;

                    ////Subsribe Event
                    sqlBackup.PercentComplete += SqlBackupOnPercentComplete;
                    sqlBackup.Information     += SqlBackupOnInformation;
                    sqlBackup.Complete        += SqlBackupOnComplete;

                    ////Run SqlBackup to perform the full database backup on the instance of SQL Server.
                    Log.NewLog(LogType.Info, "Start SQL Backup Procedure");
                    sqlBackup.SqlBackup(sqlServer);

                    ////Remove the backup device from the Backup object.
                    sqlBackup.Devices.Remove(deviceItem);

                    Log.NewLog(LogType.Info, "Finish SQL Backup Task");
                }
                else
                {
                    string Message = "No free space aviable";

                    Log.NewLog(LogType.Error, Message);
                    BackupError?.Invoke(null, Message);
                }
            }
            catch (Exception ex)
            {
                Log.NewLog(LogType.Error, "Error in SQL Backup Task. Exception: " + ex.Message);
                BackupError?.Invoke(null, ex.Message);
            }
        }