Click here to Skip to main content
16,016,067 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want to get database backup now. I am getting database backup in .bak file and I can restore it but problem is if I take backup before 1 hour and then take again after 1 hour that time my previous .bak file will be replace so how can
I get backup with Time?
my code:
C#
Backup sqlBackup = new Backup();
sqlBackup.Action = BackupActionType.Database;
sqlBackup.BackupSetDescription = "ArchiveDataBase:" + DateTime.Now.ToShortDateString();
sqlBackup.BackupSetName = "Archive";
string date = DateTime.Now.ToString();
sqlBackup.Database = databaseName;
//BackupDeviceItem deviceItem = new BackupDeviceItem(path + "\\" + databaseName + ".bak", DeviceType.File);//user physical location for store .bak file
BackupDeviceItem deviceItem = new BackupDeviceItem(databaseName+".bak", DeviceType.File);
sqlBackup.Devices.Add(deviceItem);
ServerConnection connection = new ServerConnection(serverName);
Server sqlServer = new Server(connection);

Database db = sqlServer.Databases[databaseName];

sqlBackup.Initialize = true;
sqlBackup.Checksum = true;
sqlBackup.ContinueAfterError = true;
                
sqlBackup.Incremental = false;
                
//sqlBackup.ExpirationDate = DateTime.Now.AddDays(3);
//sqlBackup.LogTruncation = BackupTruncateLogType.Truncate;

sqlBackup.FormatMedia = false;
sqlBackup.SqlBackup(sqlServer);
Posted
Updated 24-May-11 5:46am
v2

Is your problem here, possible:

MIDL
sqlBackup.BackupSetDescription = "ArchiveDataBase:" + DateTime.Now.ToShortDateString();
sqlBackup.BackupSetName = "Archive"


What happens if you do this:
sqlBackup.BackupSetDescription = "ArchiveDataBase:" + DateTime.Now.ToString();
sqlBackup.BackupSetName = "Archive" + DateTime.Now.ToString()
 
Share this answer
 
v4
If I understand your question correctly, then you're looking for a solution that will prevent newer backups from overwriting older backups. Two possibilities occur to me:

1) Make the date and time part of the filename that the database is backed up to.
For example:
C#
string backupFilename = databaseName + " [" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "].bak");
BackupDeviceItem deviceItem = new BackupDeviceItem(backupFilename, DeviceType.File);


2) After each backup, move the finished .bak file to a different location, and/or rename it, so that by the time the next backup is created the original file is no longer present with the original filename. I'd prefer option 1 though.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900