Exemplo n.º 1
0
 private Task <bool> OnFullBackupCallback(BackupInfo backupInfo, CancellationToken cancellationToken)
 {
     Assert.IsFalse(cancellationToken.IsCancellationRequested);
     FullBackupFolderPath = Path.Combine(ClassTestPath, BackupContainerFolderName, Guid.NewGuid().ToString("N"));
     FabricDirectory.Copy(backupInfo.Directory, FullBackupFolderPath, false);
     return(Task.FromResult(true));
 }
Exemplo n.º 2
0
        public ActionResult Backup()
        {
            var result = new DataJsonResult();

            if (_backupService.IsBusy)
            {
                result.ErrorMessage = "正在备份,请稍后再操作";
            }
            else
            {
                var backupInfo = new BackupInfo
                {
                    Id             = KeyGenerator.GetGuidKey(),
                    CreateUserId   = _userContainer.CurrentUser.Id,
                    CreateUserName = _userContainer.CurrentUser.UserName,
                    Status         = BackupStatus.Doing,
                    CreateTime     = DateTime.Now
                };

                _currencyService.Create(backupInfo);
                _backupService.Backup(backupInfo);
            }

            return(Json(result));
        }
Exemplo n.º 3
0
        public bool BackUpDB(BackupInfo backupInfo)
        {
            //调用mysqldump备份mysql数据库的语句
            string backupsql = string.Format("mysqldump --host={0} --port={1} --user={2} --password={3} --default-character-set=utf8 --column-statistics=0 --lock-tables  --routines --force --quick  ", "152.136.73.240", "1733", "Lsa", "llfllf");
            //mysqldump的路径
            string mysqldump = backupInfo.MysqldumpPath;
            //需要备份的数据库名称
            string strDB = "db_LibraryManagement";
            //备份数据库的路径
            string strDBpath = backupInfo.BackupPath;

            //判断备份的数据库路径是否存在
            if (!Directory.Exists(strDBpath))
            {
                Directory.CreateDirectory(strDBpath);
            }

            //备份数据库
            if (!string.IsNullOrEmpty(strDB))
            {
                string filePath = strDBpath + @"\" + backupInfo.BackupTime.ToString("yyyyMMdd_HHmmss") + strDB + ".sql";
                string cmd      = backupsql + strDB + " > " + filePath;
                RunCmd r        = new RunCmd();
                string result   = r.runCmd(mysqldump, cmd);
                return(true);
            }
            return(false);
        }
        public async Task <bool> PerformBackupAsync(BackupInfo backupInfo, CancellationToken cancellationToken,
                                                    string backupName)
        {
            var fullArchiveDirectory = Path.Combine(_temporaryDirectory, _systemservicename, _partitionName, backupName);

            var fullArchiveDirectoryInfo = new DirectoryInfo(fullArchiveDirectory);

            fullArchiveDirectoryInfo.Create();

            var blobName        = $"{backupName}_{_partitionName}_{"Backup.zip"}";
            var fullArchivePath = Path.Combine(fullArchiveDirectory, "Backup.zip");

            ZipFile.CreateFromDirectory(backupInfo.Directory, fullArchivePath, CompressionLevel.Fastest, false);

            var backupDirectory = new DirectoryInfo(backupInfo.Directory);

            backupDirectory.Delete(true);

            var container = GetBlobContainer();
            var blob      = container.GetBlockBlobReference(blobName);
            await blob.UploadFromFileAsync(fullArchivePath, CancellationToken.None);

            var tempDirectory = new DirectoryInfo(fullArchiveDirectory);

            tempDirectory.Delete(true);
            return(true);
        }
        private async Task <bool> OnBackupCompletionAsync(BackupInfo backupInfo, CancellationToken cancellationToken)
        {
            this.BackupPath = Path.Combine(this.UserBackupLocation, Guid.NewGuid().ToString("N"));
            await Utilities.CopyDirectory(backupInfo.Directory, this.BackupPath);

            return(true);
        }
Exemplo n.º 6
0
        private void dataGrid_Backups_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            BackupInfo b = this.dataGrid_Backups.SelectedItem as BackupInfo;

            this._loadBackupInfo(b);
            e.Handled = true;
        }
        public async Task ArchiveBackupAsync(BackupInfo backupInfo, CancellationToken cancellationToken)
        {
            ServiceEventSource.Current.Message("AzureBlobBackupManager: Archive Called.");

            string fullArchiveDirectory = Path.Combine(this.PartitionTempDirectory, Guid.NewGuid().ToString("N"));

            DirectoryInfo fullArchiveDirectoryInfo = new DirectoryInfo(fullArchiveDirectory);

            fullArchiveDirectoryInfo.Create();

            string blobName        = string.Format("{0}_{1}_{2}_{3}", Guid.NewGuid().ToString("N"), this.keyMin, this.keyMax, "Backup.zip");
            string fullArchivePath = Path.Combine(fullArchiveDirectory, "Backup.zip");

            ZipFile.CreateFromDirectory(backupInfo.Directory, fullArchivePath, CompressionLevel.Fastest, false);

            DirectoryInfo backupDirectory = new DirectoryInfo(backupInfo.Directory);

            backupDirectory.Delete(true);

            CloudBlockBlob blob = this.backupBlobContainer.GetBlockBlobReference(blobName);
            await blob.UploadFromFileAsync(fullArchivePath, CancellationToken.None);

            DirectoryInfo tempDirectory = new DirectoryInfo(fullArchiveDirectory);

            tempDirectory.Delete(true);

            ServiceEventSource.Current.Message("AzureBlobBackupManager: UploadBackupFolderAsync: success.");
        }
Exemplo n.º 8
0
        public void SendBackupMessage(BackupInfo info)
        {
            string msg      = "?";
            string hexColor = "";
            List <KeyValuePair <string, string> > fields = null;

            if (info.IsSuccessful)
            {
                List <string> formatted = info.SuccessfulBackupServices.Select(x => x.ToLower()).ToList();

                msg      = $"Successfully backed up repositories, using {info.DirectorySizeGB}GB of space";
                hexColor = SuccessfulHexColor;
                fields   = new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("Status", "Successful"),
                    new KeyValuePair <string, string>("Total Time", info.TotalBackupTime.ToString()),
                    new KeyValuePair <string, string>("Directory Size", $"{info.DirectorySizeGB}GB"),
                    new KeyValuePair <string, string>("Successful Services", String.Join(", ", formatted)),
                };
            }
            else
            {
                msg      = $"Failed to backup up repositories";
                hexColor = FailedHexColor;
                fields   = new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("Status", "Failed"),
                    new KeyValuePair <string, string>("Total Time", info.TotalBackupTime.ToString()),
                    new KeyValuePair <string, string>("Directory Size", $"{info.DirectorySizeGB}GB"),
                    //new KeyValuePair<string, string>("Reason", "ToDo"),
                };
            }

            SendMessage(msg, hexColor, fields);
        }
        async Task <bool> UserBackupCallbackHandler(StoreBackupInfo storeBackupInfo,
                                                    Func <BackupInfo, CancellationToken, Task <bool> > backupCallback)
        {
            var backupInfo = new BackupInfo(storeBackupInfo.BackupFolder,
                                            storeBackupInfo.BackupOption == StoreBackupOption.Full ? BackupOption.Full : BackupOption.Incremental,
                                            BackupInfo.BackupVersion.InvalidBackupVersion);
            await backupCallbackLock.WaitAsync();

            try
            {
                if (isClosingOrAborting)
                {
                    throw new FabricObjectClosedException();
                }
                backupCallbackCts  = new CancellationTokenSource();
                backupCallbackTask = backupCallback(backupInfo, backupCallbackCts.Token);
            }
            catch (Exception)
            {
                backupCallbackCts  = null;
                backupCallbackTask = null;
                throw;
            }
            finally
            {
                backupCallbackLock.Release();
            }
            return(await backupCallbackTask);
        }
        public async Task ArchiveBackupAsync(BackupInfo backupInfo, CancellationToken cancellationToken)
        {
            ServiceEventSource.Current.Message("AzureBlobBackupManager: Archive Called.");

            string fullArchiveDirectory = Path.Combine(this.PartitionTempDirectory, Guid.NewGuid().ToString("N"));

            DirectoryInfo fullArchiveDirectoryInfo = new DirectoryInfo(fullArchiveDirectory);
            fullArchiveDirectoryInfo.Create();

            string blobName = string.Format("{0}_{1}_{2}_{3}", Guid.NewGuid().ToString("N"), this.keyMin, this.keyMax, "Backup.zip");
            string fullArchivePath = Path.Combine(fullArchiveDirectory, "Backup.zip");

            ZipFile.CreateFromDirectory(backupInfo.Directory, fullArchivePath, CompressionLevel.Fastest, false);

            DirectoryInfo backupDirectory = new DirectoryInfo(backupInfo.Directory);
            backupDirectory.Delete(true);

            CloudBlockBlob blob = this.backupBlobContainer.GetBlockBlobReference(blobName);
            await blob.UploadFromFileAsync(fullArchivePath, FileMode.Open, CancellationToken.None);

            DirectoryInfo tempDirectory = new DirectoryInfo(fullArchiveDirectory);
            tempDirectory.Delete(true);

            ServiceEventSource.Current.Message("AzureBlobBackupManager: UploadBackupFolderAsync: success.");
        }
Exemplo n.º 11
0
        /// <summary>
        /// Send the backup to blob storage
        /// </summary>
        /// <param name="backupInfo"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task ArchiveBackupAsync(BackupInfo backupInfo, CancellationToken cancellationToken)
        {
            ServiceEventSource.Current.Message("AzureBlobBackupManager: Archive Called.");

            string fullArchiveDirectory = Path.Combine(this.PartitionTempDirectory, Guid.NewGuid().ToString("N"));

            DirectoryInfo fullArchiveDirectoryInfo = new DirectoryInfo(fullArchiveDirectory);

            fullArchiveDirectoryInfo.Create();

            string blobName        = string.Format("{0}_{1}_{2}_{3}", Guid.NewGuid().ToString("N"), this.keyMin, this.keyMax, "Backup.zip");
            string fullArchivePath = Path.Combine(fullArchiveDirectory, "Backup.zip");

            // Create a zip file format
            ZipFile.CreateFromDirectory(backupInfo.Directory, fullArchivePath, CompressionLevel.Fastest, false);

            DirectoryInfo backupDirectory = new DirectoryInfo(backupInfo.Directory);

            backupDirectory.Delete(true);

            CloudBlockBlob blob = this.backupBlobContainer.GetBlockBlobReference(blobName);

            // NOTE
            // If you go above NuGet package version 6.1.1 for WindowsAzure.Storage, you will see that you
            // need an extra parameter
            await blob.UploadFromFileAsync(fullArchivePath, FileMode.Open, CancellationToken.None);

            DirectoryInfo tempDirectory = new DirectoryInfo(fullArchiveDirectory);

            tempDirectory.Delete(true);

            ServiceEventSource.Current.Message("AzureBlobBackupManager: UploadBackupFolderAsync: success.");
        }
Exemplo n.º 12
0
 private void btnRestore_Click(object sender, EventArgs e)
 {
     try
     {
         if (dgvVersions.CurrentRow != null && dgvVersions.CurrentRow.DataBoundItem != null)
         {
             BackupInfo bi = dgvVersions.CurrentRow.DataBoundItem as BackupInfo;
             if (bi != null)
             {
                 if (MessageBox.Show(string.Format("You are going to replace current version \"{0}\" with version \"{1} ({2})\".\nContinue?", DBUtils.GetCurrentVersion(), bi.Version, bi.Timestamp), "Please confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
                 {
                     DialogResult res = MessageBox.Show("Create backup of current database?", "Please confirm", MessageBoxButtons.YesNoCancel);
                     if (res == DialogResult.Cancel)
                     {
                         return;
                     }
                     tcMain.SelectTab(tabConsole);
                     if (res == DialogResult.Yes)
                     {
                         DBUtils.BackupDBS();
                     }
                     DBUtils.RestoreDBS(bi.Version, bi.Timestamp);
                     LoadVersions();
                 }
             }
         }
     }
     catch (Exception ex)
     {
         LogException(ex);
     }
 }
Exemplo n.º 13
0
        public BackupResponse QueryBackup()
        {
            BackupState state;

            if (_backupManager != null)
            {
                state = BackupState.Executing;
            }
            else
            {
                BackupInfo info = _backupHistory.FirstOrDefault();
                if (info == null)
                {
                    state = BackupState.Initial;
                }
                else
                {
                    if (info.Message != null)
                    {
                        state = info.Message.StartsWith("Cancel", StringComparison.OrdinalIgnoreCase)
                            ? BackupState.Canceled
                            : BackupState.Faulted;
                    }
                    else
                    {
                        state = BackupState.Finished;
                    }
                }
            }

            return(CreateBackupResponse(state, true));
        }
Exemplo n.º 14
0
        public IEnumerable <BackupInfo> GetBackups()
        {
            FileInfo _currentfile = new FileInfo(CurrentFile);
            string   _pattern     = $"^{_currentfile.Name.Replace($".{_currentfile.Extension}", string.Empty)}_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9][0-9][0-9].bak$";

            Regex _regEx = new Regex(_pattern);

            foreach (FileInfo file in MultiDirectoryGetFiles(BackupLocations().ToArray()))
            {
                if (_regEx.Match(file.Name).Success)
                {
                    DateTime _date = GeDateFromFileName(file.Name);

                    var _backupInfo = new BackupInfo()
                    {
                        Name     = _currentfile.Name,
                        FileName = file.Name,
                        Path     = file.FullName,
                        DateTime = _date
                    };

                    yield return(_backupInfo);
                }
            }
        }
Exemplo n.º 15
0
        private void btn_Backupcommit_Click(object sender, EventArgs e)
        {
            try
            {
                List <string> errorList  = new List <string>();
                BackupInfo    backupInfo = getBackupInfo(ref errorList);

                if (!BackupInfo.isNull(backupInfo))
                {
                    if (File.Exists(backupInfo.MysqldumpPath + "\\mysqldump.exe"))
                    {
                        if (maintainaceBll.BackupDb(backupInfo))
                        {
                            MessageBox.Show("备份成功", "提示");
                        }
                        else
                        {
                            MessageBox.Show("备份失败", "提示");
                        }
                    }
                    else
                    {
                        MessageBox.Show("mysqldump路径错误", "提示");
                    }
                }
                else
                {
                    MessageBox.Show("格式错误", "提示");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Exemplo n.º 16
0
        private void CollectErrorMessages(Exception exception, BackupInfo targetInfo)
        {
            var sb = new StringBuilder(exception is TaskCanceledException ? "CANCELED: " : "ERROR: ");

            CollectErrorMessages(exception, sb, "");
            targetInfo.Message = sb.ToString();
        }
Exemplo n.º 17
0
        public void CreateBackupTest()
        {
            DisasterRecoveryService service = new DisasterRecoveryService();
            string databaseName             = "SqlToolsService_TestBackup_" + new Random().Next(10000000, 99999999);

            var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo(databaseName);

            using (SqlTestDb testDb = SqlTestDb.CreateNew(TestServerType.OnPrem, false, databaseName))
                using (DatabaseTaskHelper helper = AdminService.CreateDatabaseTaskHelper(liveConnection.ConnectionInfo, databaseExists: true))
                    using (SqlConnection sqlConn = ConnectionService.OpenSqlConnection(liveConnection.ConnectionInfo))
                    {
                        string backupPath = GetDefaultBackupFullPath(service, databaseName, helper.DataContainer, sqlConn);

                        BackupInfo backupInfo = CreateDefaultBackupInfo(databaseName,
                                                                        BackupType.Full,
                                                                        new List <string>()
                        {
                            backupPath
                        },
                                                                        new Dictionary <string, int>()
                        {
                            { backupPath, (int)DeviceType.File }
                        });
                        BackupOperation backupOperation = CreateBackupOperation(service, liveConnection.ConnectionInfo.OwnerUri, backupInfo, helper.DataContainer, sqlConn);

                        // Backup the database
                        service.PerformBackup(backupOperation);

                        VerifyAndCleanBackup(sqlConn, backupPath);
                    }
        }
Exemplo n.º 18
0
        //[Fact]
        public void ScriptBackupTest()
        {
            DisasterRecoveryService service   = new DisasterRecoveryService();
            string             databaseName   = "testbackup_" + new Random().Next(10000000, 99999999);
            SqlTestDb          testDb         = SqlTestDb.CreateNew(TestServerType.OnPrem, false, databaseName);
            var                liveConnection = LiveConnectionHelper.InitLiveConnectionInfo(databaseName);
            DatabaseTaskHelper helper         = AdminService.CreateDatabaseTaskHelper(liveConnection.ConnectionInfo, databaseExists: true);
            SqlConnection      sqlConn        = ConnectionService.OpenSqlConnection(liveConnection.ConnectionInfo);
            string             backupPath     = GetDefaultBackupFullPath(service, databaseName, helper.DataContainer, sqlConn);

            BackupInfo backupInfo = CreateDefaultBackupInfo(databaseName,
                                                            BackupType.Full,
                                                            new List <string>()
            {
                backupPath
            },
                                                            new Dictionary <string, int>()
            {
                { backupPath, (int)DeviceType.File }
            });
            BackupOperation backupOperation = CreateBackupOperation(service, liveConnection.ConnectionInfo.OwnerUri, backupInfo, helper.DataContainer, sqlConn);

            // Generate script for backup
            service.ScriptBackup(backupOperation);
            string script = backupOperation.ScriptContent;

            Assert.True(!string.IsNullOrEmpty(script));

            // Execute the script
            testDb.RunQuery(script);

            VerifyAndCleanBackup(backupPath);
            testDb.Cleanup();
        }
Exemplo n.º 19
0
        public async Task ArchiveBackupAsync(BackupInfo backupInfo, CancellationToken cancellationToken)
        {
            Trace.TraceInformation("MinioBackupManager: Archive Called.");

            string fullArchiveDirectory = Path.Combine(this.PartitionTempDirectory, Guid.NewGuid().ToString("N"));

            DirectoryInfo fullArchiveDirectoryInfo = new DirectoryInfo(fullArchiveDirectory);

            fullArchiveDirectoryInfo.Create();

            string blobName        = string.Format("{0}_{1}_{2}_{3}", Guid.NewGuid().ToString("N"), this.keyMin, this.keyMax, "Backup.zip");
            string fullArchivePath = Path.Combine(fullArchiveDirectory, "Backup.zip");

            ZipFile.CreateFromDirectory(backupInfo.Directory, fullArchivePath, CompressionLevel.Fastest, false);

            DirectoryInfo backupDirectory = new DirectoryInfo(backupInfo.Directory);

            backupDirectory.Delete(true);

            await EnsureBucket();

            await minio.PutObjectAsync(bucketName, blobName, fullArchivePath);

            DirectoryInfo tempDirectory = new DirectoryInfo(fullArchiveDirectory);

            tempDirectory.Delete(true);

            Trace.TraceInformation("MinioBackupManager: UploadBackupFolderAsync: success.");
        }
Exemplo n.º 20
0
        // BackupCallbackAsync is an arbitrary name, you can name it anything you want, but just make sure
        // that you specify it in the BackupDescription object you created (please see a few lines above here).
        // This is called AFTER Service Fabric has completed your backup.The backup is written by Service Fabric
        // to an internal directory. The method below moves the backup files to blob storage by use of
        // AzureBackupManager class. It is just an illustration of how to get the
        // backup files to a place you maintain and of course you would not want to hard code it.
        // If you return false from this callback, you'll indicate to Service Fabric that the backup was not successful.
        private async Task <bool> BackupCallbackAsync(BackupInfo backupInfo,
                                                      CancellationToken cancellationToken)
        {
            this.SetupBackupManager();
            ServiceEventSource.Current.ServiceMessage(this, "Inside backup callback for replica {0}|{1}",
                                                      this.Context.PartitionId, this.Context.ReplicaId);

            IReliableDictionary <string, long> backupCountDictionary =
                await this.StateManager.GetOrAddAsync <IReliableDictionary <string, long> >(CountDictionaryName);

            try
            {
                ServiceEventSource.Current.ServiceMessage(this, "Archiving backup");
                await this.backupManager.ArchiveBackupAsync(backupInfo, cancellationToken);

                ServiceEventSource.Current.ServiceMessage(this, "Backup archived");

                //only delete backups after a full backup
                if (incrementalCount == 0)
                {
                    await this.backupManager.DeleteBackupsAsyncIncremental(cancellationToken);
                }

                ServiceEventSource.Current.Message("Backups deleted");
            }
            catch (Exception e)
            {
                ServiceEventSource.Current.ServiceMessage(this,
                                                          "Archive of backup failed: Source: {0} Exception: {1}",
                                                          backupInfo.Directory, e.Message);
            }

            return(true);
        }
Exemplo n.º 21
0
        public void CreateBackup(BackupInfo backup, Guid sessionID)
        {
            Client client = this._ClientRepo.Find(this._LoginRepo.Find(sessionID).IDClient);

            client.UTCLastBackupTime = DateTime.UtcNow;
            this._ClientRepo.Update(client);

            this._BackupRepository.Update(backup);
        }
Exemplo n.º 22
0
        /// <summary>
        /// Serializes the object to JSON.
        /// </summary>
        /// <param name="writer">The <see cref="T: Newtonsoft.Json.JsonWriter" /> to write to.</param>
        /// <param name="obj">The object to serialize to JSON.</param>
        internal static void Serialize(JsonWriter writer, BackupInfo obj)
        {
            // Required properties are always serialized, optional properties are serialized when not null.
            writer.WriteStartObject();
            writer.WriteProperty(obj.BackupType, "BackupType", BackupTypeConverter.Serialize);
            if (obj.BackupId != null)
            {
                writer.WriteProperty(obj.BackupId, "BackupId", JsonWriterExtensions.WriteGuidValue);
            }

            if (obj.BackupChainId != null)
            {
                writer.WriteProperty(obj.BackupChainId, "BackupChainId", JsonWriterExtensions.WriteGuidValue);
            }

            if (obj.ApplicationName != null)
            {
                writer.WriteProperty(obj.ApplicationName, "ApplicationName", JsonWriterExtensions.WriteStringValue);
            }

            if (obj.ServiceName != null)
            {
                writer.WriteProperty(obj.ServiceName, "ServiceName", JsonWriterExtensions.WriteStringValue);
            }

            if (obj.PartitionInformation != null)
            {
                writer.WriteProperty(obj.PartitionInformation, "PartitionInformation", PartitionInformationConverter.Serialize);
            }

            if (obj.BackupLocation != null)
            {
                writer.WriteProperty(obj.BackupLocation, "BackupLocation", JsonWriterExtensions.WriteStringValue);
            }

            if (obj.EpochOfLastBackupRecord != null)
            {
                writer.WriteProperty(obj.EpochOfLastBackupRecord, "EpochOfLastBackupRecord", BackupEpochConverter.Serialize);
            }

            if (obj.LsnOfLastBackupRecord != null)
            {
                writer.WriteProperty(obj.LsnOfLastBackupRecord, "LsnOfLastBackupRecord", JsonWriterExtensions.WriteStringValue);
            }

            if (obj.CreationTimeUtc != null)
            {
                writer.WriteProperty(obj.CreationTimeUtc, "CreationTimeUtc", JsonWriterExtensions.WriteDateTimeValue);
            }

            if (obj.FailureError != null)
            {
                writer.WriteProperty(obj.FailureError, "FailureError", FabricErrorErrorConverter.Serialize);
            }

            writer.WriteEndObject();
        }
Exemplo n.º 23
0
 public int Delete(string backup_info_file)
 {
     try
     {
         OpenBackUpDrive();
         if (Path.GetDirectoryName(backup_info_file).ToLower() + "\\" != this.server_dir.ToLower())
         {
             throw new Exception("Can't delete backup from other servers");
         }
         var backup     = new BackupInfo(backup_info_file);
         var server_dir = this.server_dir + "\\files\\";
         //var time = Utils.DateTimeString2(backup.time);
         var active_links = new List <string>();
         var B            = GetBackUpList();
         foreach (var b in B)
         {
             if (b.LogFile.ToLower() != backup.LogFile.ToLower())
             {
                 var F = File.ReadAllLines(b.LogFile);
                 foreach (var f in F)
                 {
                     if (f.Trim() != "")
                     {
                         var A        = f.Split(new char[] { '|' });
                         var f_server = (server_dir + A[1]).ToLower();
                         if (!active_links.Contains(f_server))
                         {
                             active_links.Add(f_server);
                         }
                     }
                 }
             }
         }
         var res = 0;
         {
             var F = File.ReadAllLines(backup.LogFile);
             foreach (var f in F)
             {
                 if (f.Trim() != "")
                 {
                     var A        = f.Split(new char[] { '|' });
                     var f_server = (server_dir + A[1]).ToLower();
                     if (!active_links.Contains(f_server))
                     {
                         File.Delete(f_server); res++;
                     }
                 }
             }
         }
         File.Delete(backup.LogFile);
         File.Delete(backup.path);
         return(res);
     }
     finally
     { CloseBackUpDrive(); }
 }
Exemplo n.º 24
0
        /// <summary>
        /// Test creating backup with advanced options set.
        /// </summary>
        //[Fact]
        public void ScriptBackupWithAdvancedOptionsTest()
        {
            DisasterRecoveryService service   = new DisasterRecoveryService();
            string             databaseName   = "testbackup_" + new Random().Next(10000000, 99999999);
            SqlTestDb          testDb         = SqlTestDb.CreateNew(TestServerType.OnPrem, false, databaseName);
            var                liveConnection = LiveConnectionHelper.InitLiveConnectionInfo(databaseName);
            DatabaseTaskHelper helper         = AdminService.CreateDatabaseTaskHelper(liveConnection.ConnectionInfo, databaseExists: true);
            SqlConnection      sqlConn        = ConnectionService.OpenSqlConnection(liveConnection.ConnectionInfo);
            string             backupPath     = GetDefaultBackupFullPath(service, databaseName, helper.DataContainer, sqlConn);

            string certificateName         = CreateCertificate(testDb);
            string cleanupCertificateQuery = string.Format(CleanupCertificateQueryFormat, certificateName);

            BackupInfo backupInfo = CreateDefaultBackupInfo(databaseName,
                                                            BackupType.Full,
                                                            new List <string>()
            {
                backupPath
            },
                                                            new Dictionary <string, int>()
            {
                { backupPath, (int)DeviceType.File }
            });

            backupInfo.FormatMedia         = true;
            backupInfo.SkipTapeHeader      = true;
            backupInfo.Initialize          = true;
            backupInfo.MediaName           = "backup test media";
            backupInfo.MediaDescription    = "backup test";
            backupInfo.EncryptionAlgorithm = (int)BackupEncryptionAlgorithm.Aes128;
            backupInfo.EncryptorType       = (int)BackupEncryptorType.ServerCertificate;
            backupInfo.EncryptorName       = certificateName;

            BackupOperation backupOperation = CreateBackupOperation(service, liveConnection.ConnectionInfo.OwnerUri, backupInfo, helper.DataContainer, sqlConn);

            // Backup the database
            Console.WriteLine("Generate script for backup operation..");
            service.ScriptBackup(backupOperation);
            string script = backupOperation.ScriptContent;

            // Run the script
            Console.WriteLine("Execute the script..");
            testDb.RunQuery(script);

            // Remove the backup file
            Console.WriteLine("Verify the backup file exists and remove..");
            VerifyAndCleanBackup(backupPath);

            // Delete certificate and master key
            Console.WriteLine("Remove certificate and master key..");
            testDb.RunQuery(cleanupCertificateQuery);

            // Clean up the database
            Console.WriteLine("Clean up database..");
            testDb.Cleanup();
        }
Exemplo n.º 25
0
        /// <summary>
        /// Get Backup Info
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        BackupInfo GetBackupInfo(string name, Hashtable args)
        {
            var backupInfo = new BackupInfo();

            backupInfo.AllowOverwrite = args.ContainsKey("AllowOverwrite")? Convert.ToBoolean(args["AllowOverwrite"]) : true;
            backupInfo.BackupRemotePartitions = args.ContainsKey("BackupRemotePartitions") ? Convert.ToBoolean(args["BackupRemotePartitions"]) : false;
            backupInfo.File = GetFilePath(name, args["Path"].ToString());

            return backupInfo;
        }
Exemplo n.º 26
0
        private async Task <bool> BackupCallbackAzureAsync(BackupInfo backupInfo)
        {
            string            backupId          = Guid.NewGuid().ToString();
            CancellationToken cancellationToken = default(CancellationToken);

            long totalBackupCount;


            IReliableDictionary <int, long> countDictionary = await this.StateManager.GetOrAddAsync <IReliableDictionary <int, long> >(this.countDictionaryName);

            using (ITransaction txn = this.StateManager.CreateTransaction())
            {
                long count = await countDictionary.AddOrUpdateAsync(txn, 0, 0, (key, oldValue) => { return(oldValue + 1); });

                totalBackupCount = count;
                await txn.CommitAsync();
            }


            ServiceEventSource.Current.Message("Backup count dictionary updated: " + totalBackupCount);


            if ((totalBackupCount % 10) == 0)
            {
                //Store no more than 10 backups at a time - the actual max might be a bit more than 10 since more backups could have been created when deletion was taking place. Keeps behind 5 backups.
                await this.backupStore.DeleteBackupsAzureAsync(cancellationToken);
            }

            if ((totalBackupCount > 10) && (DateTime.Now.Second % 20) == 0)
            {
                //Let's simulate a data loss every time the time is a multiple of 20 seconds, and a backup just completed.
                ServiceEventSource.Current.ServiceMessage(this, "Restore Started");

                using (FabricClient fabricClient = new FabricClient())
                {
                    PartitionSelector partitionSelector = PartitionSelector.PartitionIdOf(
                        this.ServiceInitializationParameters.ServiceName,
                        this.ServiceInitializationParameters.PartitionId);

                    await fabricClient.ServiceManager.InvokeDataLossAsync(partitionSelector, DataLossMode.PartialDataLoss, cancellationToken);
                }
            }

            ServiceEventSource.Current.Message("Backing up from directory, ID  : " + backupInfo.Directory + " *** " + backupId);
            try
            {
                await this.backupStore.UploadBackupFolderAsync(backupInfo.Directory, backupId, CancellationToken.None);
            }
            catch (Exception e)
            {
                ServiceEventSource.Current.ServiceMessage(this, "Uploading to backup folder failed: " + "{0} {1}" + e.GetType() + e.Message);
            }

            return(true);
        }
Exemplo n.º 27
0
 public void Restore(BackupInfo backup, string local_dir)
 {
     try
     {
         OpenBackUpDrive();
         abort = false;
         if (progress != null)
         {
             progress(0, "بازگردانی ...");
         }
         if (!local_dir.EndsWith("\\"))
         {
             local_dir += "\\";
         }
         if (!Directory.Exists(local_dir))
         {
             Directory.CreateDirectory(local_dir);
         }
         var server_dir = Path.GetDirectoryName(backup.LogFile) + "\\files\\";
         var F          = File.ReadAllLines(backup.LogFile);
         if (progress != null)
         {
             progress(5, "بازگردانی ...");
         }
         int ii = 0;
         foreach (var f in F)
         {
             if (f.Trim() != "")
             {
                 if (progress != null)
                 {
                     progress(5 + 94.0 * ii++ / F.Length, "بازگردانی ...");
                 }
                 var A        = f.Split(new char[] { '|' });
                 var f_local  = local_dir + A[0];
                 var f_server = server_dir + A[1];
                 var dir      = Path.GetDirectoryName(f_local);
                 if (!Directory.Exists(dir))
                 {
                     Directory.CreateDirectory(dir);
                 }
                 if (!File.Exists(f_server))
                 {
                     progress(-100, new string[] { f_local, f_server });
                 }
                 else
                 {
                     File.Copy(f_server, f_local, true);
                 }
             }
         }
     }
     finally
     { CloseBackUpDrive(); }
 }
Exemplo n.º 28
0
        private async Task <bool> BackupCallbackAsync(BackupInfo backupInfo, CancellationToken cancellationToken)
        {
            ServiceEventSource.Current.ServiceMessage(Context, "Inside backup callback for replica {0}|{1}", this.Context.PartitionId, this.Context.ReplicaId);
            long totalBackupCount;

            IReliableDictionary <string, long> backupCountDictionary =
                await this.StateManager.GetOrAddAsync <IReliableDictionary <string, long> >(BackupCountDictionaryName);

            using (ITransaction tx = this.StateManager.CreateTransaction())
            {
                ConditionalValue <long> value = await backupCountDictionary.TryGetValueAsync(tx, "backupCount");

                if (!value.HasValue)
                {
                    totalBackupCount = 0;
                }
                else
                {
                    totalBackupCount = value.Value;
                }

                await backupCountDictionary.SetAsync(tx, "backupCount", ++totalBackupCount);

                await tx.CommitAsync();
            }

            ServiceEventSource.Current.Message("Backup count dictionary updated, total backup count is {0}", totalBackupCount);

            try
            {
                ServiceEventSource.Current.ServiceMessage(Context, "Archiving backup");
                await this.backupManager.ArchiveBackupAsync(backupInfo, cancellationToken);

                ServiceEventSource.Current.ServiceMessage(Context, "Backup archived");
            }
            catch (Exception e)
            {
                ServiceEventSource.Current.ServiceMessage(Context, "Archive of backup failed: Source: {0} Exception: {1}", backupInfo.Directory, e.Message);
            }

            try
            {
                ServiceEventSource.Current.ServiceMessage(Context, "Deleting backups");
                await this.backupManager.DeleteBackupsAsync(cancellationToken);

                ServiceEventSource.Current.ServiceMessage(Context, "Backups deleted");
            }
            catch (Exception e)
            {
                ServiceEventSource.Current.ServiceMessage(Context, "Delete of backup failed: Exception: {1}", e.Message);
            }

            return(true);
        }
Exemplo n.º 29
0
        /// <summary>
        /// Delete the selected BackupInfo item via index from the GUI from the collection if applicable.
        /// </summary>
        public void DeleteBackupItemBySelectedIndex()
        {
            if (SelectedBackupItemIndex == -1)
            {
                return;
            }

            // Remove the selected row.
            TaskManager.DequeueBackupJob(BackupInfo[SelectedBackupItemIndex].HashCode);
            BackupInfo.RemoveAt(SelectedBackupItemIndex);
            SelectedBackupItemIndex = BackupInfo.Count - 1;
        }
        public Task BackupAsync(BackupOption option, TimeSpan timeout, CancellationToken cancellationToken, Func <BackupInfo, CancellationToken, Task <bool> > backupCallback)
        {
            string stateBin = Path.Combine(Path.GetTempPath(), "state.bin");

            using (var fs = File.Create(stateBin))
            {
                _store.Serialize(fs);
            }
            var info = new BackupInfo(Path.GetDirectoryName(stateBin), option, new BackupInfo.BackupVersion());

            return(backupCallback(info, CancellationToken.None));
        }
Exemplo n.º 31
0
        public IActionResult Create([FromBody] BackupInfo item)
        {
            if (item == null)
            {
                return(BadRequest());
            }

            item.Id = db.GetSequence <BackupInfo>();
            db.InsertData <BackupInfo>(item);

            return(Ok(new { id = item.Id }));
        }
Exemplo n.º 32
0
        private async Task <bool> BackupCallbackAsync(BackupInfo backupInfo)
        {
            string backupId = Guid.NewGuid().ToString();


            long totalBackupCount;


            IReliableDictionary <int, long> countDictionary = await this.StateManager.GetOrAddAsync <IReliableDictionary <int, long> >(this.countDictionaryName);

            using (ITransaction txn = this.StateManager.CreateTransaction())
            {
                long count = await countDictionary.AddOrUpdateAsync(txn, 0, 0, (key, oldValue) => { return(oldValue + 1); });

                totalBackupCount = count;
                await txn.CommitAsync();
            }

            ServiceEventSource.Current.ServiceMessage(this, "Backup count dictionary updated: " + totalBackupCount);
            ServiceEventSource.Current.Message("Backup count dictionary updated: " + totalBackupCount);

            if ((totalBackupCount % 20) == 0)
            {
                //The following limits the number of backups stored to 20 per partition. The actual max might be more than 20 per partition since more backups
                //could have been created when deletion was taking place.
                //Also depending on the backup that was restored, the count of backups could be a lot larger.
                this.DeleteBackups(Path.Combine(this.localBackupStore, this.ServicePartition.PartitionInfo.Id.ToString()), 5);
            }

            //Simulate a restore/data loss event randomly. This assumes that all partitions have some state at this point.
            //Five inventory items must be added for all five partitions to have state.
            if ((totalBackupCount > 19) && (DateTime.Now.Second % 20) == 0)
            {
                CancellationToken cancellationToken = default(CancellationToken);

                ServiceEventSource.Current.ServiceMessage(this, "Restore Started");

                using (FabricClient fabricClient = new FabricClient())
                {
                    PartitionSelector partitionSelector = PartitionSelector.PartitionIdOf(
                        this.ServiceInitializationParameters.ServiceName,
                        this.ServiceInitializationParameters.PartitionId);

                    await fabricClient.ServiceManager.InvokeDataLossAsync(partitionSelector, DataLossMode.PartialDataLoss, cancellationToken);
                }
            }

            await
            this.CopyBackupFolderAsync(backupInfo.Directory, this.ServicePartition.PartitionInfo.Id.ToString(), backupId, CancellationToken.None);

            return(true);
        }
        public Task ArchiveBackupAsync(BackupInfo backupInfo, CancellationToken cancellationToken)
        {
            string fullArchiveDirectory = Path.Combine(
                this.PartitionArchiveFolder,
                string.Format("{0}_{1}_{2}", Guid.NewGuid().ToString("N"), this.keyMin, this.keyMax));

            DirectoryInfo dirInfo = new DirectoryInfo(fullArchiveDirectory);
            dirInfo.Create();

            string fullArchivePath = Path.Combine(fullArchiveDirectory, "Backup.zip");

            ZipFile.CreateFromDirectory(backupInfo.Directory, fullArchivePath, CompressionLevel.Fastest, false);

            DirectoryInfo backupDirectory = new DirectoryInfo(backupInfo.Directory);
            backupDirectory.Delete(true);

            return Task.FromResult(true);
        }
Exemplo n.º 34
0
        private Task<bool> backupToBlob(BackupInfo backupInfo)
        {
            var success = true;
            try
            {
                var blob = getBlockBlob(_backupConfig);

                using (var fileStream = System.IO.File.OpenRead(Directory.GetFiles(backupInfo.Directory).First()))
                {
                    blob.UploadFromStream(fileStream);
                }
            }
            catch(Exception ex)
            {
                ServiceEventSource.Current.Message("ERROR: {0}", ex);
                success = false;
            }

            _timer.Enabled = false;
            return Task.FromResult(success);
        }
        private async Task<bool> BackupCallbackAsync(BackupInfo backupInfo, CancellationToken cancellationToken)
        {
            ServiceEventSource.Current.ServiceMessage(this, "Inside backup callback for replica {0}|{1}", this.Context.PartitionId, this.Context.ReplicaId);
            long totalBackupCount;

            IReliableDictionary<string, long> backupCountDictionary =
                await this.StateManager.GetOrAddAsync<IReliableDictionary<string, long>>(BackupCountDictionaryName);
            using (ITransaction tx = this.StateManager.CreateTransaction())
            {
                ConditionalValue<long> value = await backupCountDictionary.TryGetValueAsync(tx, "backupCount");

                if (!value.HasValue)
                {
                    totalBackupCount = 0;
                }
                else
                {
                    totalBackupCount = value.Value;
                }

                await backupCountDictionary.SetAsync(tx, "backupCount", ++totalBackupCount);

                await tx.CommitAsync();
            }

            ServiceEventSource.Current.Message("Backup count dictionary updated, total backup count is {0}", totalBackupCount);

            try
            {
                ServiceEventSource.Current.ServiceMessage(this, "Archiving backup");
                await this.backupManager.ArchiveBackupAsync(backupInfo, cancellationToken);
                ServiceEventSource.Current.ServiceMessage(this, "Backup archived");
            }
            catch (Exception e)
            {
                ServiceEventSource.Current.ServiceMessage(this, "Archive of backup failed: Source: {0} Exception: {1}", backupInfo.Directory, e.Message);
            }

            await this.backupManager.DeleteBackupsAsync(cancellationToken);

            ServiceEventSource.Current.Message("Backups deleted");

            return true;
        }
        private async Task<bool> BackupCallbackAzureAsync(BackupInfo backupInfo)
        {
            string backupId = Guid.NewGuid().ToString();
            CancellationToken cancellationToken = default(CancellationToken);

            long totalBackupCount;


            IReliableDictionary<int, long> countDictionary = await this.StateManager.GetOrAddAsync<IReliableDictionary<int, long>>(this.countDictionaryName);
            using (ITransaction txn = this.StateManager.CreateTransaction())
            {
                long count = await countDictionary.AddOrUpdateAsync(txn, 0, 0, (key, oldValue) => { return oldValue + 1; });

                totalBackupCount = count;
                await txn.CommitAsync();
            }


            ServiceEventSource.Current.Message("Backup count dictionary updated: " + totalBackupCount);


            if ((totalBackupCount%10) == 0)
            {
                //Store no more than 10 backups at a time - the actual max might be a bit more than 10 since more backups could have been created when deletion was taking place. Keeps behind 5 backups.
                await this.backupStore.DeleteBackupsAzureAsync(cancellationToken);
            }

            if ((totalBackupCount > 10) && (DateTime.Now.Second%20) == 0)
            {
                //Let's simulate a data loss every time the time is a multiple of 20 seconds, and a backup just completed.
                ServiceEventSource.Current.ServiceMessage(this, "Restore Started");

                using (FabricClient fabricClient = new FabricClient())
                {
                    PartitionSelector partitionSelector = PartitionSelector.PartitionIdOf(
                        this.ServiceInitializationParameters.ServiceName,
                        this.ServiceInitializationParameters.PartitionId);

                    await fabricClient.ServiceManager.InvokeDataLossAsync(partitionSelector, DataLossMode.PartialDataLoss, cancellationToken);
                }
            }

            ServiceEventSource.Current.Message("Backing up from directory, ID  : " + backupInfo.Directory + " *** " + backupId);
            try
            {
                await this.backupStore.UploadBackupFolderAsync(backupInfo.Directory, backupId, CancellationToken.None);
            }
            catch (Exception e)
            {
                ServiceEventSource.Current.ServiceMessage(this, "Uploading to backup folder failed: " + "{0} {1}" + e.GetType() + e.Message);
            }

            return true;
        }
        private async Task<bool> BackupCallbackAsync(BackupInfo backupInfo)
        {
            string backupId = Guid.NewGuid().ToString();


            long totalBackupCount;


            IReliableDictionary<int, long> countDictionary = await this.StateManager.GetOrAddAsync<IReliableDictionary<int, long>>(this.countDictionaryName);
            using (ITransaction txn = this.StateManager.CreateTransaction())
            {
                long count = await countDictionary.AddOrUpdateAsync(txn, 0, 0, (key, oldValue) => { return oldValue + 1; });

                totalBackupCount = count;
                await txn.CommitAsync();
            }

            ServiceEventSource.Current.ServiceMessage(this, "Backup count dictionary updated: " + totalBackupCount);
            ServiceEventSource.Current.Message("Backup count dictionary updated: " + totalBackupCount);

            if ((totalBackupCount%20) == 0)
            {
                //The following limits the number of backups stored to 20 per partition. The actual max might be more than 20 per partition since more backups 
                //could have been created when deletion was taking place. 
                //Also depending on the backup that was restored, the count of backups could be a lot larger.
                this.DeleteBackups(Path.Combine(this.localBackupStore, this.ServicePartition.PartitionInfo.Id.ToString()), 5);
            }

            //Simulate a restore/data loss event randomly. This assumes that all partitions have some state at this point. 
            //Five inventory items must be added for all five partitions to have state.
            if ((totalBackupCount > 19) && (DateTime.Now.Second%20) == 0)
            {
                CancellationToken cancellationToken = default(CancellationToken);

                ServiceEventSource.Current.ServiceMessage(this, "Restore Started");

                using (FabricClient fabricClient = new FabricClient())
                {
                    PartitionSelector partitionSelector = PartitionSelector.PartitionIdOf(
                        this.ServiceInitializationParameters.ServiceName,
                        this.ServiceInitializationParameters.PartitionId);

                    await fabricClient.ServiceManager.InvokeDataLossAsync(partitionSelector, DataLossMode.PartialDataLoss, cancellationToken);
                }
            }

            await
                this.CopyBackupFolderAsync(backupInfo.Directory, this.ServicePartition.PartitionInfo.Id.ToString(), backupId, CancellationToken.None);

            return true;
        }
    public virtual BackupInfo GetDatabasesNameFromBackup([NotNull] SqlConnectionStringBuilder connectionString, [NotNull] string pathToBak)
    {
      Assert.ArgumentNotNull(connectionString, "connectionString");

      BackupInfo res = new BackupInfo(string.Empty, string.Empty, string.Empty, string.Empty, string.Empty);

      using (SqlConnection conn = this.OpenConnection(connectionString))
      {
        SqlCommand command = new SqlCommand("RESTORE FILELISTONLY FROM DISK='" + pathToBak + "'", conn);

        // SqlCommand command = new SqlCommand("RESTORE HEADERONLY FROM DISK = N'" + pathToBak + "' WITH NOUNLOAD", conn);
        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
          string logicName = (string)reader["LogicalName"];
          string physName = (string)reader["PhysicalName"];
          if (physName.ToLower().Contains(".mdf"))
          {
            res.logicalNameMdf = logicName;
            res.physicalNameMdf = physName;
          }
          else
          {
            res.logicalNameLdf = logicName;
            res.physicalNameLdf = physName;
          }
        }

        reader.Close();
        SqlCommand command2 = new SqlCommand("RESTORE HEADERONLY FROM DISK = N'" + pathToBak + "' WITH NOUNLOAD", conn);
        reader = command2.ExecuteReader();
        string dbName = string.Empty;
        while (reader.Read())
        {
          dbName = (string)reader["DatabaseName"];
        }

        res.dbOriginalName = dbName;
      }

      return res;
    }
Exemplo n.º 39
0
 public DomainInfo()
 {
     Backup = new BackupInfo();
 }
Exemplo n.º 40
0
        static void HandleConnection(System.IO.DirectoryInfo info, TcpClient client)
        {
            Area ws = null;
            ClientStateInfo clientInfo = new ClientStateInfo();
            using (client)
            using (SharedNetwork.SharedNetworkInfo sharedInfo = new SharedNetwork.SharedNetworkInfo())
            {
                try
                {
                    var stream = client.GetStream();
                    Handshake hs = ProtoBuf.Serializer.DeserializeWithLengthPrefix<Handshake>(stream, ProtoBuf.PrefixStyle.Fixed32);
                    DomainInfo domainInfo = null;
                    lock (SyncObject)
                    {
                        if (hs.RequestedModule == null)
                            hs.RequestedModule = string.Empty;
                        if (!Domains.TryGetValue(hs.RequestedModule, out domainInfo))
                        {
                            domainInfo = Domains.Where(x => x.Key.Equals(hs.RequestedModule, StringComparison.OrdinalIgnoreCase)).Select(x => x.Value).FirstOrDefault();
                        }
                        if (domainInfo == null)
                        {
                            if (!Config.AllowVaultCreation || !Config.RequiresAuthentication || string.IsNullOrEmpty(hs.RequestedModule) || System.IO.Directory.Exists(System.IO.Path.Combine(info.FullName, hs.RequestedModule)))
                            {
                                Network.StartTransaction startSequence = Network.StartTransaction.CreateRejection();
                                Printer.PrintDiagnostics("Rejecting client due to invalid domain: \"{0}\".", hs.RequestedModule);
                                ProtoBuf.Serializer.SerializeWithLengthPrefix<Network.StartTransaction>(stream, startSequence, ProtoBuf.PrefixStyle.Fixed32);
                                return;
                            }
                            domainInfo = new DomainInfo()
                            {
                                Bare = true,
                                Directory = null
                            };
                        }
                    }
                    try
                    {
                        ws = Area.Load(domainInfo.Directory, true, true);
                        if (domainInfo.Bare)
                            throw new Exception("Domain is bare, but workspace could be loaded!");
                    }
                    catch
                    {
                        if (!domainInfo.Bare)
                            throw new Exception("Domain not bare, but couldn't load workspace!");
                    }
                    Printer.PrintDiagnostics("Received handshake - protocol: {0}", hs.VersionrProtocol);
                    SharedNetwork.Protocol? clientProtocol = hs.CheckProtocol();
                    bool valid = true;
                    if (clientProtocol == null)
                        valid = false;
                    else
                    {
                        valid = SharedNetwork.AllowedProtocols.Contains(clientProtocol.Value);
                        if (Config.RequiresAuthentication && !SharedNetwork.SupportsAuthentication(clientProtocol.Value))
                            valid = false;
                    }
                    if (valid)
                    {
                        sharedInfo.CommunicationProtocol = clientProtocol.Value;
                        Network.StartTransaction startSequence = null;
                        clientInfo.Access = Rights.Read | Rights.Write;
                        clientInfo.BareAccessRequired = domainInfo.Bare;
                        if (PrivateKey != null)
                        {
                            startSequence = Network.StartTransaction.Create(domainInfo.Bare ? string.Empty : ws.Domain.ToString(), PublicKey, clientProtocol.Value);
                            Printer.PrintDiagnostics("Sending RSA key...");
                            ProtoBuf.Serializer.SerializeWithLengthPrefix<Network.StartTransaction>(stream, startSequence, ProtoBuf.PrefixStyle.Fixed32);
                            if (!HandleAuthentication(clientInfo, client, sharedInfo))
                                throw new Exception("Authentication failed.");
                            StartClientTransaction clientKey = ProtoBuf.Serializer.DeserializeWithLengthPrefix<StartClientTransaction>(stream, ProtoBuf.PrefixStyle.Fixed32);
                            System.Security.Cryptography.RSAOAEPKeyExchangeDeformatter exch = new System.Security.Cryptography.RSAOAEPKeyExchangeDeformatter(PrivateKey);
                            byte[] aesKey = exch.DecryptKeyExchange(clientKey.Key);
                            byte[] aesIV = exch.DecryptKeyExchange(clientKey.IV);
                            Printer.PrintDiagnostics("Got client key: {0}", System.Convert.ToBase64String(aesKey));

                            var aesCSP = System.Security.Cryptography.AesManaged.Create();

                            sharedInfo.DecryptorFunction = () => { return aesCSP.CreateDecryptor(aesKey, aesIV); };
                            sharedInfo.EncryptorFunction = () => { return aesCSP.CreateEncryptor(aesKey, aesIV); };
                        }
                        else
                        {
                            startSequence = Network.StartTransaction.Create(domainInfo.Bare ? string.Empty : ws.Domain.ToString(), clientProtocol.Value);
                            ProtoBuf.Serializer.SerializeWithLengthPrefix<Network.StartTransaction>(stream, startSequence, ProtoBuf.PrefixStyle.Fixed32);
                            if (!HandleAuthentication(clientInfo, client, sharedInfo))
                                throw new Exception("Authentication failed.");
                            StartClientTransaction clientKey = ProtoBuf.Serializer.DeserializeWithLengthPrefix<StartClientTransaction>(stream, ProtoBuf.PrefixStyle.Fixed32);
                        }
                        sharedInfo.Stream = stream;
                        sharedInfo.Workspace = ws;
                        sharedInfo.ChecksumType = Config.ChecksumType;

                        clientInfo.SharedInfo = sharedInfo;

                        while (true)
                        {
                            NetCommand command = ProtoBuf.Serializer.DeserializeWithLengthPrefix<NetCommand>(stream, ProtoBuf.PrefixStyle.Fixed32);
                            if (command.Type == NetCommandType.Close)
                            {
                                Printer.PrintDiagnostics("Client closing connection.");
                                break;
                            }
                            else if (command.Type == NetCommandType.PushInitialVersion)
                            {
                                bool fresh = false;
                                if (domainInfo.Directory == null)
                                {
                                    if (!clientInfo.Access.HasFlag(Rights.Create))
                                        throw new Exception("Access denied.");
                                    fresh = true;
                                    System.IO.DirectoryInfo newDirectory = new System.IO.DirectoryInfo(System.IO.Path.Combine(info.FullName, hs.RequestedModule));
                                    newDirectory.Create();
                                    domainInfo.Directory = newDirectory;
                                    if (!newDirectory.Exists)
                                        throw new Exception("Access denied.");
                                }
                                if (!clientInfo.Access.HasFlag(Rights.Write))
                                    throw new Exception("Access denied.");
                                lock (SyncObject)
                                {
                                    ws = Area.InitRemote(domainInfo.Directory, Utilities.ReceiveEncrypted<ClonePayload>(clientInfo.SharedInfo));
                                    clientInfo.SharedInfo.Workspace = ws;
                                    domainInfo.Bare = false;
                                    if (fresh)
                                        Domains[hs.RequestedModule] = domainInfo;
                                }
                            }
                            else if (command.Type == NetCommandType.PushBranchJournal)
                            {
                                if (!clientInfo.Access.HasFlag(Rights.Write))
                                    throw new Exception("Access denied.");
                                SharedNetwork.ReceiveBranchJournal(sharedInfo);
                            }
                            else if (command.Type == NetCommandType.QueryBranchID)
                            {
                                if (!clientInfo.Access.HasFlag(Rights.Read))
                                    throw new Exception("Access denied.");
                                Printer.PrintDiagnostics("Client is requesting a branch info for {0}", string.IsNullOrEmpty(command.AdditionalPayload) ? "<root>" : "\"" + command.AdditionalPayload + "\"");
                                bool multiple = false;
                                Objects.Branch branch = string.IsNullOrEmpty(command.AdditionalPayload) ? ws.RootBranch : ws.GetBranchByPartialName(command.AdditionalPayload, out multiple);
                                if (branch != null)
                                {
                                    ProtoBuf.Serializer.SerializeWithLengthPrefix<NetCommand>(stream, new NetCommand() { Type = NetCommandType.Acknowledge, AdditionalPayload = branch.ID.ToString() }, ProtoBuf.PrefixStyle.Fixed32);
                                }
                                else if (!multiple)
                                {
                                    ProtoBuf.Serializer.SerializeWithLengthPrefix<NetCommand>(stream, new NetCommand() { Type = NetCommandType.Error, AdditionalPayload = "branch not recognized" }, ProtoBuf.PrefixStyle.Fixed32);
                                }
                                else
                                {
                                    ProtoBuf.Serializer.SerializeWithLengthPrefix<NetCommand>(stream, new NetCommand() { Type = NetCommandType.Error, AdditionalPayload = "multiple branches with that name!" }, ProtoBuf.PrefixStyle.Fixed32);
                                }
                            }
                            else if (command.Type == NetCommandType.ListBranches)
                            {
                                if (!clientInfo.Access.HasFlag(Rights.Read))
                                    throw new Exception("Access denied.");
                                Printer.PrintDiagnostics("Client is requesting a branch list.");
                                ProtoBuf.Serializer.SerializeWithLengthPrefix<NetCommand>(stream, new NetCommand() { Type = NetCommandType.Acknowledge }, ProtoBuf.PrefixStyle.Fixed32);
                                if (command.Identifier == 1) // send extra data
                                {
                                    BranchList bl = new BranchList();
                                    bl.Branches = clientInfo.SharedInfo.Workspace.Branches.ToArray();
                                    Dictionary<Guid, Objects.Version> importantVersions = new Dictionary<Guid, Objects.Version>();
                                    List<KeyValuePair<Guid, Guid>> allHeads = new List<KeyValuePair<Guid, Guid>>();
                                    foreach (var x in bl.Branches)
                                    {
                                        if (x.Terminus.HasValue && !importantVersions.ContainsKey(x.Terminus.Value))
                                        {
                                            importantVersions[x.Terminus.Value] = clientInfo.SharedInfo.Workspace.GetVersion(x.Terminus.Value);
                                            continue;
                                        }
                                        var heads = clientInfo.SharedInfo.Workspace.GetBranchHeads(x);
                                        foreach (var head in heads)
                                        {
                                            if (!importantVersions.ContainsKey(head.Version))
                                                importantVersions[head.Version] = clientInfo.SharedInfo.Workspace.GetVersion(head.Version);
                                        }
                                        allHeads.AddRange(heads.Select(y => new KeyValuePair<Guid, Guid>(y.Branch, y.Version)));
                                    }
                                    bl.Heads = allHeads.ToArray();
                                    bl.ImportantVersions = importantVersions.Values.ToArray();
                                    Utilities.SendEncrypted<BranchList>(clientInfo.SharedInfo, bl);
                                }
                                else
                                {
                                    BranchList bl = new BranchList();
                                    bl.Branches = clientInfo.SharedInfo.Workspace.Branches.ToArray();
                                    List<KeyValuePair<Guid, Guid>> allHeads = new List<KeyValuePair<Guid, Guid>>();
                                    foreach (var x in bl.Branches)
                                    {
                                        if (x.Terminus.HasValue)
                                            continue;
                                        var heads = clientInfo.SharedInfo.Workspace.GetBranchHeads(x);
                                        if (heads.Count == 1)
                                            allHeads.Add(new KeyValuePair<Guid, Guid>(x.ID, heads[0].Version));
                                    }
                                    bl.Heads = allHeads.ToArray();
                                    Utilities.SendEncrypted<BranchList>(clientInfo.SharedInfo, bl);
                                }
                            }
                            else if (command.Type == NetCommandType.RequestRecordUnmapped)
                            {
                                if (!clientInfo.Access.HasFlag(Rights.Read))
                                    throw new Exception("Access denied.");
                                Printer.PrintDiagnostics("Client is requesting specific record data blobs.");
                                SharedNetwork.SendRecordDataUnmapped(sharedInfo);
                            }
                            else if (command.Type == NetCommandType.Clone)
                            {
                                if (!clientInfo.Access.HasFlag(Rights.Read))
                                    throw new Exception("Access denied.");
                                Printer.PrintDiagnostics("Client is requesting to clone the vault.");
                                Objects.Version initialRevision = ws.GetVersion(ws.Domain);
                                Objects.Branch initialBranch = ws.GetBranch(initialRevision.Branch);
                                Utilities.SendEncrypted<ClonePayload>(sharedInfo, new ClonePayload() { InitialBranch = initialBranch, RootVersion = initialRevision });
                            }
                            else if (command.Type == NetCommandType.PullVersions)
                            {
                                if (!clientInfo.Access.HasFlag(Rights.Read))
                                    throw new Exception("Access denied.");
                                Printer.PrintDiagnostics("Client asking for remote version information.");
                                Branch branch = ws.GetBranch(new Guid(command.AdditionalPayload));
                                if (branch == null)
                                    ProtoBuf.Serializer.SerializeWithLengthPrefix<NetCommand>(stream, new NetCommand() { Type = NetCommandType.Error, AdditionalPayload = string.Format("Unknown branch {0}", command.AdditionalPayload) }, ProtoBuf.PrefixStyle.Fixed32);
                                else
                                    ProtoBuf.Serializer.SerializeWithLengthPrefix<NetCommand>(stream, new NetCommand() { Type = NetCommandType.Acknowledge }, ProtoBuf.PrefixStyle.Fixed32);
                                Stack<Objects.Branch> branchesToSend = new Stack<Branch>();
                                Stack<Objects.Version> versionsToSend = new Stack<Objects.Version>();
                                if (!SharedNetwork.SendBranchJournal(sharedInfo))
                                    throw new Exception();
                                if (!SharedNetwork.GetVersionList(sharedInfo, sharedInfo.Workspace.GetBranchHeadVersion(branch), out branchesToSend, out versionsToSend))
                                    throw new Exception();
                                if (!SharedNetwork.SendBranches(sharedInfo, branchesToSend))
                                    throw new Exception();
                                if (!SharedNetwork.SendVersions(sharedInfo, versionsToSend))
                                    throw new Exception();
                            }
                            else if (command.Type == NetCommandType.PushObjectQuery)
                            {
                                if (!clientInfo.Access.HasFlag(Rights.Write))
                                    throw new Exception("Access denied.");
                                Printer.PrintDiagnostics("Client asking about objects on the server...");
                                SharedNetwork.ProcesPushObjectQuery(sharedInfo);
                            }
                            else if (command.Type == NetCommandType.PushBranch)
                            {
                                if (!clientInfo.Access.HasFlag(Rights.Write))
                                    throw new Exception("Access denied.");
                                Printer.PrintDiagnostics("Client attempting to send branch data...");
                                SharedNetwork.ReceiveBranches(sharedInfo);
                            }
                            else if (command.Type == NetCommandType.PushVersions)
                            {
                                if (!clientInfo.Access.HasFlag(Rights.Write))
                                    throw new Exception("Access denied.");
                                Printer.PrintDiagnostics("Client attempting to send version data...");
                                SharedNetwork.ReceiveVersions(sharedInfo);
                            }
                            else if (command.Type == NetCommandType.PushHead)
                            {
                                if (!clientInfo.Access.HasFlag(Rights.Write))
                                    throw new Exception("Access denied.");
                                Printer.PrintDiagnostics("Determining head information.");
                                string errorData;
                                lock (ws)
                                {
                                    clientInfo.SharedInfo.Workspace.RunLocked(() =>
                                    {
                                        try
                                        {
                                            clientInfo.SharedInfo.Workspace.BeginDatabaseTransaction();
                                            if (!SharedNetwork.ImportBranchJournal(clientInfo.SharedInfo, false))
                                            {
                                                clientInfo.SharedInfo.Workspace.RollbackDatabaseTransaction();
                                                return false;
                                            }
                                            if (AcceptHeads(clientInfo, ws, out errorData))
                                            {
                                                ImportVersions(ws, clientInfo);
                                                clientInfo.SharedInfo.Workspace.CommitDatabaseTransaction();
                                                ProtoBuf.Serializer.SerializeWithLengthPrefix<NetCommand>(stream, new NetCommand() { Type = NetCommandType.AcceptPush }, ProtoBuf.PrefixStyle.Fixed32);
                                                return true;
                                            }
                                            else
                                                ProtoBuf.Serializer.SerializeWithLengthPrefix<NetCommand>(stream, new NetCommand() { Type = NetCommandType.RejectPush, AdditionalPayload = errorData }, ProtoBuf.PrefixStyle.Fixed32);
                                            clientInfo.SharedInfo.Workspace.RollbackDatabaseTransaction();
                                            return false;
                                        }
                                        catch
                                        {
                                            clientInfo.SharedInfo.Workspace.RollbackDatabaseTransaction();
                                            return false;
                                        }
                                    }, false);
                                }
                            }
                            else if (command.Type == NetCommandType.SynchronizeRecords)
                            {
                                if (!clientInfo.Access.HasFlag(Rights.Read))
                                    throw new Exception("Access denied.");
                                Printer.PrintDiagnostics("Received {0} versions in version pack, but need {1} records to commit data.", sharedInfo.PushedVersions.Count, sharedInfo.UnknownRecords.Count);
                                Printer.PrintDiagnostics("Beginning record synchronization...");
                                if (sharedInfo.UnknownRecords.Count > 0)
                                {
                                    Printer.PrintDiagnostics("Requesting record metadata...");
                                    SharedNetwork.RequestRecordMetadata(clientInfo.SharedInfo);
                                    Printer.PrintDiagnostics("Requesting record data...");
                                    SharedNetwork.RequestRecordData(sharedInfo);
                                    if (!sharedInfo.Workspace.RunLocked(() =>
                                    {
                                        return SharedNetwork.ImportRecords(sharedInfo);
                                    }, false))
                                    {
                                        throw new Exception("Unable to import records!");
                                    }
                                }
                                ProtoBuf.Serializer.SerializeWithLengthPrefix<NetCommand>(stream, new NetCommand() { Type = NetCommandType.Synchronized }, ProtoBuf.PrefixStyle.Fixed32);
                            }
                            else if (command.Type == NetCommandType.FullClone)
                            {
                                if (!clientInfo.Access.HasFlag(Rights.Read))
                                    throw new Exception("Access denied.");
                                ProtoBuf.Serializer.SerializeWithLengthPrefix<NetCommand>(stream, new NetCommand() { Type = NetCommandType.Acknowledge, Identifier = (int)ws.DatabaseVersion }, ProtoBuf.PrefixStyle.Fixed32);
                                command = ProtoBuf.Serializer.DeserializeWithLengthPrefix<NetCommand>(stream, ProtoBuf.PrefixStyle.Fixed32);
                                if (command.Type == NetCommandType.Acknowledge)
                                {
                                    bool accept = false;
                                    BackupInfo backupInfo = null;
                                    lock (domainInfo)
                                    {
                                        string backupKey = ws.LastVersion + "-" + ws.LastBranch + "-" + ws.BranchJournalTipID.ToString();
                                        backupInfo = domainInfo.Backup;
                                        if (backupKey != backupInfo.Key)
                                        {
                                            Printer.PrintMessage("Backup key out of date for domain DB[{0}] - {1}", domainInfo.Directory, backupKey);
                                            if (System.Threading.Interlocked.Decrement(ref backupInfo.Refs) == 0)
                                                backupInfo.Backup.Delete();
                                            backupInfo = new BackupInfo();
                                            domainInfo.Backup = backupInfo;
                                            var directory = new System.IO.DirectoryInfo(System.IO.Path.Combine(ws.AdministrationFolder.FullName, "backups"));
                                            directory.Create();
                                            backupInfo.Backup = new System.IO.FileInfo(System.IO.Path.Combine(directory.FullName, System.IO.Path.GetRandomFileName()));
                                            if (ws.BackupDB(backupInfo.Backup))
                                            {
                                                System.Threading.Interlocked.Increment(ref backupInfo.Refs);
                                                accept = true;
                                                backupInfo.Key = backupKey;
                                            }
                                        }
                                        else
                                        {
                                            accept = true;
                                        }

                                        if (accept)
                                            System.Threading.Interlocked.Increment(ref backupInfo.Refs);
                                    }
                                    if (accept)
                                    {
                                        Printer.PrintDiagnostics("Backup complete. Sending data.");
                                        byte[] blob = new byte[256 * 1024];
                                        long filesize = backupInfo.Backup.Length;
                                        long position = 0;
                                        using (System.IO.FileStream reader = backupInfo.Backup.OpenRead())
                                        {
                                            while (true)
                                            {
                                                long remainder = filesize - position;
                                                int count = blob.Length;
                                                if (count > remainder)
                                                    count = (int)remainder;
                                                reader.Read(blob, 0, count);
                                                position += count;
                                                Printer.PrintDiagnostics("Sent {0}/{1} bytes.", position, filesize);
                                                if (count == remainder)
                                                {
                                                    Utilities.SendEncrypted(sharedInfo, new DataPayload()
                                                    {
                                                        Data = blob.Take(count).ToArray(),
                                                        EndOfStream = true
                                                    });
                                                    break;
                                                }
                                                else
                                                {
                                                    Utilities.SendEncrypted(sharedInfo, new DataPayload()
                                                    {
                                                        Data = blob,
                                                        EndOfStream = false
                                                    });
                                                }
                                            }
                                        }
                                        ProtoBuf.Serializer.SerializeWithLengthPrefix<NetCommand>(stream, new NetCommand() { Type = NetCommandType.Acknowledge }, ProtoBuf.PrefixStyle.Fixed32);
                                        lock (domainInfo)
                                        {
                                            if (System.Threading.Interlocked.Decrement(ref backupInfo.Refs) == 0)
                                                backupInfo.Backup.Delete();
                                        }
                                    }
                                    else
                                    {
                                        Utilities.SendEncrypted<DataPayload>(sharedInfo, new DataPayload() { Data = new byte[0], EndOfStream = true });
                                        ProtoBuf.Serializer.SerializeWithLengthPrefix<NetCommand>(stream, new NetCommand() { Type = NetCommandType.Error }, ProtoBuf.PrefixStyle.Fixed32);
                                    }
                                }
                            }
                            else
                            {
                                Printer.PrintDiagnostics("Client sent invalid command: {0}", command.Type);
                                throw new Exception();
                            }
                        }
                    }
                    else
                    {
                        Network.StartTransaction startSequence = Network.StartTransaction.CreateRejection();
                        Printer.PrintDiagnostics("Rejecting client due to protocol mismatch.");
                        ProtoBuf.Serializer.SerializeWithLengthPrefix<Network.StartTransaction>(stream, startSequence, ProtoBuf.PrefixStyle.Fixed32);
                        return;
                    }
                }
                catch (Exception e)
                {
                    Printer.PrintDiagnostics("Client was a terrible person, because: {0}", e);
                }
                finally
                {
                    if (ws != null)
                        ws.Dispose();
                }
            }

            Printer.PrintDiagnostics("Ended client processor task!");
        }