Exemplo n.º 1
0
        private void PrepareForIncrementalBackup()
        {
            if (Directory.Exists(backupDestinationDirectory) == false)
            {
                Directory.CreateDirectory(backupDestinationDirectory);
            }

            var incrementalBackupState = Path.Combine(backupDestinationDirectory, Constants.IncrementalBackupState);

            if (File.Exists(incrementalBackupState))
            {
                var state = RavenJObject.Parse(File.ReadAllText(incrementalBackupState)).JsonDeserialization <IncrementalBackupState>();

                if (state.ResourceId != storage.ServerId)
                {
                    throw new InvalidOperationException(string.Format("Can't perform an incremental backup to a given folder because it already contains incremental backup data of different database. Existing incremental data origins from '{0}' database.", state.ResourceName));
                }
            }
            else
            {
                var state = new IncrementalBackupState
                {
                    ResourceId   = storage.ServerId,
                    ResourceName = counterDocument.Id
                };

                File.WriteAllText(incrementalBackupState, RavenJObject.FromObject(state).ToString());
            }
        }
Exemplo n.º 2
0
        public void Execute()
        {
            try
            {
                log.Info("Starting backup of '{0}' to '{1}'", backupSourceDirectory, backupDestinationDirectory);
                UpdateBackupStatus(
                    string.Format("Started backup process. Backing up data to directory = '{0}'",
                                  backupDestinationDirectory), null, BackupStatus.BackupMessageSeverity.Informational);

                if (incrementalBackup)
                {
                    var incrementalBackupState = Path.Combine(backupDestinationDirectory, Constants.IncrementalBackupState);

                    if (File.Exists(incrementalBackupState))
                    {
                        var state = RavenJObject.Parse(File.ReadAllText(incrementalBackupState)).JsonDeserialization <IncrementalBackupState>();

                        if (state.ResourceId != filesystem.Storage.Id)
                        {
                            throw new InvalidOperationException(string.Format("Can't perform an incremental backup to a given folder because it already contains incremental backup data of different file system. Existing incremental data origins from '{0}' file system.", state.ResourceName));
                        }
                    }
                    else
                    {
                        var state = new IncrementalBackupState()
                        {
                            ResourceId   = filesystem.Storage.Id,
                            ResourceName = filesystem.Name
                        };

                        if (!Directory.Exists(backupDestinationDirectory))
                        {
                            Directory.CreateDirectory(backupDestinationDirectory);
                        }

                        File.WriteAllText(incrementalBackupState, RavenJObject.FromObject(state).ToString());
                    }

                    if (CanPerformIncrementalBackup())
                    {
                        backupDestinationDirectory = DirectoryForIncrementalBackup();
                    }
                    else
                    {
                        incrementalBackup = false; // destination wasn't detected as a backup folder, automatically revert to a full backup if incremental was specified
                    }
                }
                else if (BackupAlreadyExists)
                {
                    throw new InvalidOperationException("Denying request to perform a full backup to an existing backup folder. Try doing an incremental backup instead.");
                }

                UpdateBackupStatus(string.Format("Backing up indexes.."), null, BackupStatus.BackupMessageSeverity.Informational);

                // Make sure we have an Indexes folder in the backup location
                if (!Directory.Exists(Path.Combine(backupDestinationDirectory, "Indexes")))
                {
                    Directory.CreateDirectory(Path.Combine(backupDestinationDirectory, "Indexes"));
                }

                filesystem.Search.Backup(backupDestinationDirectory);

                UpdateBackupStatus(string.Format("Finished indexes backup. Executing data backup.."), null, BackupStatus.BackupMessageSeverity.Informational);

                ExecuteBackup(backupDestinationDirectory, incrementalBackup);

                if (filesystemDocument != null)
                {
                    File.WriteAllText(Path.Combine(backupDestinationDirectory, Constants.FilesystemDocumentFilename), RavenJObject.FromObject(filesystemDocument).ToString());
                }

                OperationFinished();
            }
            catch (AggregateException e)
            {
                var ne = e.ExtractSingleInnerException();
                log.ErrorException("Failed to complete backup", ne);
                UpdateBackupStatus("Failed to complete backup because: " + ne.Message, ne.ExceptionToString(null), BackupStatus.BackupMessageSeverity.Error);
            }
            catch (Exception e)
            {
                log.ErrorException("Failed to complete backup", e);
                UpdateBackupStatus("Failed to complete backup because: " + e.Message, e.ExceptionToString(null), BackupStatus.BackupMessageSeverity.Error);
            }
            finally
            {
                CompleteBackup();
            }
        }