Skip to content

Commit

Permalink
Merge pull request #3 from GerryLaplante/namebackup
Browse files Browse the repository at this point in the history
Allow user to rename backup of database.
  • Loading branch information
Michael Minton committed Sep 24, 2015
2 parents 2886b47 + a93f2e6 commit 3616ee1
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
34 changes: 28 additions & 6 deletions src/DataButler/Backup.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/DataButler/Backup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace DataButler
public partial class Backup : Form
{
readonly BackgroundWorker _backgroundWorker = new BackgroundWorker { WorkerReportsProgress = true };
public string userBackupName;
public Backup()
{
InitializeComponent();
Expand Down Expand Up @@ -51,15 +52,15 @@ private void BackupClicked(object sender, EventArgs e)
txtLog.Visible = true;
txtLog.Clear();
Cursor = Cursors.WaitCursor;
btnBackup.Enabled = cbDatabase.Enabled = false;
WaitImage.Visible = true;
_backgroundWorker.RunWorkerAsync(cbDatabase.SelectedItem as SqlDatabase);
}

private void BackgroundWorkerOnDoWork(object sender, DoWorkEventArgs e)
{
userBackupName = txtBackupAs.Text;
var database = e.Argument as SqlDatabase;
e.Result = Database.Backup(database, _backgroundWorker);
e.Result = Database.Backup(database, userBackupName, _backgroundWorker);
}

private void BackgroundWorkerOnRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
Expand Down
15 changes: 11 additions & 4 deletions src/DataButler/Utilities/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ from msdb.dbo.backupset bs
}
}

public static string Backup(SqlDatabase database, BackgroundWorker backgroundWorker = null)
public static string Backup(SqlDatabase database, string userBackupName, BackgroundWorker backgroundWorker = null)
{
string result;
var fileName = GetBackupFileName(database);
var fileName = GetBackupFileName(database, userBackupName);
_bgw = backgroundWorker;
using (var con = (SqlConnection)OpenConnection())
{
Expand All @@ -88,12 +88,12 @@ public static string Backup(SqlDatabase database, BackgroundWorker backgroundWor
return result;
}

static string GetBackupFileName(SqlDatabase database)
static string GetBackupFileName(SqlDatabase database, string userBackupName)
{
var backupDir = string.IsNullOrEmpty(database.LastBackupName)
? @"C:\temp\"
: Path.GetDirectoryName(database.LastBackupName);
var backupName = string.Format("{0}{1}", database.Name, DateTime.Now.ToString("yyyyMMddHHmm"));
var backupName = SetBackupName(database.Name, userBackupName);
var fullBackupName = Path.Combine(backupDir, string.Format("{0}.bak", backupName));
var copyCount = 0;
while (File.Exists(fullBackupName))
Expand All @@ -103,6 +103,13 @@ static string GetBackupFileName(SqlDatabase database)
return fullBackupName;
}

public static object SetBackupName(string databaseName, string userBackupName)
{
return string.IsNullOrWhiteSpace(userBackupName)
? string.Format("{0}, {1}", databaseName, DateTime.Now.ToString("yyyyMMddHHmm"))
: userBackupName;
}

public static string GetDatabaseName(string file, out string failure)
{
failure = string.Empty;
Expand Down
2 changes: 1 addition & 1 deletion src/DataButler/Utilities/SqlDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public SqlDatabase(string name, string lastBackupName)
LastBackupName = lastBackupName;
}

public string Name { get; private set; }
public string Name { get; set; }
public string LastBackupName { get; set; }
public override string ToString()
{
Expand Down
2 changes: 1 addition & 1 deletion src/DataButlerTests/DataAccessTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void CanRetreiveDatabaseName()
{
string failure;
var name = Database.GetDatabaseName(SampleBackup, out failure);
Assert.AreEqual("IddealDemo", name);
Assert.AreEqual("Sample", name);
}
[Test]
public void CanRestoreToNonExistingDatabase()
Expand Down

0 comments on commit 3616ee1

Please sign in to comment.