private IDataProcedure CreateReadCommand(IndexBackup backup) { var sql = String.Format("SELECT SUBSTRING([BackupFile], @Offset, @Length) FROM {0} WHERE BackupNumber = @BackupNumber", backup.TableName); var cmd = new SqlProcedure { CommandText = sql, CommandType = System.Data.CommandType.Text }; cmd.CommandType = CommandType.Text; cmd.Parameters.Add(new SqlParameter("@BackupNumber", SqlDbType.Int)).Value = backup.BackupNumber; cmd.Parameters.Add(new SqlParameter("@Offset", SqlDbType.BigInt)); cmd.Parameters.Add(new SqlParameter("@Length", SqlDbType.BigInt)); return cmd; }
protected internal override void SetActiveBackup(IndexBackup backup, IndexBackup lastBackup) { var sql = (lastBackup == null) ? String.Format("UPDATE {0} SET IsActive = 1 WHERE BackupNumber = @ActiveBackupNumber", backup.TableName) : String.Format(@"UPDATE {0} SET IsActive = 1 WHERE BackupNumber = @ActiveBackupNumber UPDATE {1} SET IsActive = 0 WHERE BackupNumber = @InactiveBackupNumber", backup.TableName, lastBackup.TableName); using (var cmd = new SqlProcedure { CommandText = sql, CommandType = CommandType.Text }) { cmd.Parameters.Add(new SqlParameter("@ActiveBackupNumber", SqlDbType.Int)).Value = backup.BackupNumber; if(lastBackup!=null) cmd.Parameters.Add(new SqlParameter("@InactiveBackupNumber", SqlDbType.Int)).Value = lastBackup.BackupNumber; cmd.ExecuteNonQuery(); } }
protected override void KeepOnlyLastIndexBackup() { var backup = LoadLastBackup(); if (backup == null) return; backup = new IndexBackup { BackupNumber = backup.BackupNumber - 1 }; var sql = "TRUNCATE TABLE " + backup.TableName; using (var cmd = new SqlProcedure { CommandText = sql, CommandType = CommandType.Text }) cmd.ExecuteNonQuery(); }
//protected internal override void StoreBackupStream(string backupFilePath, IndexBackup2 backup, BackupProgress progress) //{ // var fileLength = new FileInfo(backupFilePath).Length; // using (var stream = new FileStream(backupFilePath, FileMode.Open)) // { // using (var reader = new BinaryReader(stream)) // { // InitializeNewStream(backup); // progress.Type = BackupProgressType.Storing; // progress.Message = "Storing backup"; // progress.MaxValue = fileLength; // var offset = 0L; // while (offset < fileLength) // { // using (var writeCommand = CreateWriteCommand(backup)) // { // progress.Value = offset; // progress.NotifyChanged(); // var remnant = fileLength - offset; // var length = remnant < BUFFERSIZE ? Convert.ToInt32(remnant) : BUFFERSIZE; // var buffer = reader.ReadBytes(length); // writeCommand.Parameters["@Buffer"].Value = buffer; // writeCommand.Parameters["@Offset"].Value = offset; // writeCommand.Parameters["@Length"].Value = length; // writeCommand.ExecuteNonQuery(); // offset += BUFFERSIZE; // } // } // progress.Value = fileLength; // progress.NotifyChanged(); // } // } //} private SqlProcedure CreateWriteCommand(IndexBackup backup) { var sql = String.Format("UPDATE {0} SET [BackupFile].WRITE(@Buffer, @Offset, @Length) WHERE BackupNumber = @BackupNumber", backup.TableName); var cmd = new SqlProcedure { CommandText = sql, CommandType = CommandType.Text }; cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@BackupNumber", SqlDbType.Int)).Value = backup.BackupNumber; cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Offset", SqlDbType.BigInt)); cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Length", SqlDbType.BigInt)); cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Buffer", SqlDbType.VarBinary)); return cmd; }
private void InitializeNewStream(IndexBackup backup) { var sql = String.Format("UPDATE {0} SET [BackupFile] = @InitialStream WHERE BackupNumber = @BackupNumber", backup.TableName); using (var cmd = new SqlProcedure { CommandText = sql, CommandType = CommandType.Text }) { cmd.CommandType = CommandType.Text; cmd.Parameters.Add(new SqlParameter("@BackupNumber", SqlDbType.Int)); cmd.Parameters["@BackupNumber"].Value = backup.BackupNumber; cmd.Parameters.Add(new SqlParameter("@InitialStream", SqlDbType.VarBinary)); cmd.Parameters["@InitialStream"].Value = new byte[0]; cmd.ExecuteNonQuery(); } }
private IndexBackup GetBackupFromReader(SqlDataReader reader) { var result = new IndexBackup(); result.IndexBackupId = reader.GetInt32(0); // IndexBackupId result.BackupNumber = reader.GetInt32(1); // BackupNumber result.BackupDate = reader.GetDateTime(2); // BackupDate result.ComputerName = reader.GetSafeString(3); // ComputerName result.AppDomainName = reader.GetSafeString(4); // AppDomain result.BackupFileLength = reader.GetInt64(5); // BackupFileLength result.RowGuid = reader.GetGuid(6); // RowGuid result.Timestamp = GetLongFromBytes((byte[])reader[7]); // Timestamp return result; }
protected internal override void StoreBackupStream(string backupFilePath, IndexBackup backup, IndexBackupProgress progress) { var fileLength = new FileInfo(backupFilePath).Length; using (var writeCommand = CreateWriteCommand(backup)) { using (var stream = new FileStream(backupFilePath, FileMode.Open)) { using (var reader = new BinaryReader(stream)) { InitializeNewStream(backup); progress.Type = IndexBackupProgressType.Storing; progress.Message = "Storing backup"; progress.MaxValue = fileLength; var timer = Stopwatch.StartNew(); var offset = 0L; while (offset < fileLength) { progress.Value = offset; progress.NotifyChanged(); var remnant = fileLength - offset; var length = remnant < BUFFERSIZE ? Convert.ToInt32(remnant) : BUFFERSIZE; var buffer = reader.ReadBytes(length); writeCommand.Parameters["@Buffer"].Value = buffer; writeCommand.Parameters["@Offset"].Value = offset; writeCommand.Parameters["@Length"].Value = length; writeCommand.ExecuteNonQuery(); offset += BUFFERSIZE; } //progress.FinishStoreIndexBackupToDb(); ////progress.Value = fileLength; ////progress.NotifyChanged(); } } } }
protected internal override IndexBackup CreateBackup(int backupNumber) { var backup = new IndexBackup { BackupNumber = backupNumber, AppDomainName = AppDomain.CurrentDomain.FriendlyName, BackupDate = DateTime.Now, ComputerName = Environment.MachineName, }; var sql = String.Format(@"INSERT INTO {0} (BackupNumber, IsActive, BackupDate, ComputerName, [AppDomain]) VALUES (@BackupNumber, 0, @BackupDate, @ComputerName, @AppDomain)", backup.TableName); using (var cmd = new SqlProcedure { CommandText = sql, CommandType = CommandType.Text }) { cmd.Parameters.Add("@BackupNumber", SqlDbType.Int).Value = backup.BackupNumber; cmd.Parameters.Add("@BackupDate", SqlDbType.DateTime).Value = backup.BackupDate; cmd.Parameters.Add("@ComputerName", SqlDbType.NVarChar, 100).Value = backup.ComputerName; cmd.Parameters.Add("@AppDomain", SqlDbType.NVarChar, 500).Value = backup.AppDomainName; cmd.ExecuteNonQuery(); } return backup; }
protected internal override void SetActiveBackup(IndexBackup backup, IndexBackup lastBackup) { WriteLog(MethodInfo.GetCurrentMethod(), backup, lastBackup); base.SetActiveBackup(backup, lastBackup); }
protected internal override void StoreBackupStream(string backupFilePath, IndexBackup backup, IndexBackupProgress progress) { WriteLog(MethodInfo.GetCurrentMethod(), backupFilePath, backup, progress); base.StoreBackupStream(backupFilePath, backup, progress); }