如何随时间获取SqlServer数据库备份和还原 [英] How to get SqlServer dataBase Backup and Restore with time
问题描述
我现在要备份数据库.我正在获取.bak文件中的数据库备份,并且可以还原它,但问题是,如果我在1小时之前进行备份,然后在1小时后再次进行备份,那么之前的.bak文件将被替换,那么该怎么办
我有时间备份吗?
我的代码:
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:
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);
推荐答案
在这里是您的问题,可能是:
Is your problem here, possible:
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()
如果我正确理解了您的问题,那么您正在寻找一种解决方案,以防止新备份覆盖旧备份.我想到了两种可能性:
1)将日期和时间作为备份数据库的文件名的一部分.
例如:
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:
string backupFilename = databaseName + " [" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "].bak");
BackupDeviceItem deviceItem = new BackupDeviceItem(backupFilename, DeviceType.File);
2)每次备份后,请将完成的.bak文件移动到其他位置,和/或重命名该文件,以便在创建下一个备份时,原始文件不再包含原始文件名.我还是更喜欢选项1.
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.
这篇关于如何随时间获取SqlServer数据库备份和还原的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!