Reputation: 71
string dbname = _dbContext.Database.Connection.Database;
const string sqlCommand = @"BACKUP DATABASE [{0}] TO DISK = N'{1}' WITH NOFORMAT,
NOINIT, NAME = N'MajesticDb-Ali-Full Database Backup',
SKIP, NOREWIND, NOUNLOAD, STATS = 10";
int path = _dbContext.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior
.DoNotEnsureTransaction,
string.Format(sqlCommand, dbname, "MajesticDB"));
I am using this code to make a database backup. It's working fine but this code is replacing the previous file with the new one. How do I change it so it doesn't?
Upvotes: 0
Views: 4514
Reputation: 7095
you can try something like this:
string backupname= "MajesticDB" + DateTime.Now.ToString("yyyyMMddHHmm");
const string sqlCommand = @"BACKUP DATABASE [{0}] TO DISK = N'{1}' WITH NOFORMAT, NOINIT, NAME = N'MajesticDb-Ali-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10";
int path= _dbContext.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, string.Format(sqlCommand, dbname, backupname));
this way, to backup name "MajesticDB" is appended date and time, so every time you call your backup procedure, database will be backed up with different name.
Upvotes: 1
Reputation: 581
Try like this.
string dbname = _dbContext.Database.Connection.Database;
string dbBackUp= "MajesticDB" + DateTime.Now.ToString("yyyyMMddHHmm");
const string sqlCommand = @"BACKUP DATABASE [{0}] TO DISK = N'{1}' WITH NOFORMAT, NOINIT, NAME = N'MajesticDb-Ali-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10";
int path= _dbContext.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, string.Format(sqlCommand, dbname , dbBackUp));
Upvotes: 2