Esempio n. 1
0
        private void SetBackupProps()
        {
            try
            {
                switch (this.backupType)
                {
                case BackupType.Full:
                    if (this.backupComponent == BackupComponent.Database)
                    {
                        this.backupActionType = BackupActionType.Database;
                    }
                    else if ((this.backupComponent == BackupComponent.Files) && (null != this.backupInfo.SelectedFileGroup) && (this.backupInfo.SelectedFileGroup.Count > 0))
                    {
                        this.backupActionType = BackupActionType.Files;
                    }
                    this.isBackupIncremental = false;
                    break;

                case BackupType.Differential:
                    if ((this.backupComponent == BackupComponent.Files) && (0 != this.backupInfo.SelectedFiles.Length))
                    {
                        this.backupActionType    = BackupActionType.Files;
                        this.isBackupIncremental = true;
                    }
                    else
                    {
                        this.backupActionType    = BackupActionType.Database;
                        this.isBackupIncremental = true;
                    }
                    break;

                case BackupType.TransactionLog:
                    this.backupActionType    = BackupActionType.Log;
                    this.isBackupIncremental = false;
                    break;

                default:
                    break;
                }
            }
            catch
            {
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Restore Database target server from backup files to create database
        /// </summary>
        /// <param name="fileShare">Location of backup files</param>
        /// <param name="targetServer">Destination server</param>
        /// <param name="dbName">Name of the database to be restored</param>
        /// <param name="noRecovery">Gets or sets a Restore.NoRecovery property value that determines whether the tail of the log is backed up and whether the database is restored into the 'Restoring' state</param>

        public static void RestoreDatabaseToCreateDB(string fileShare, SMO.Server targetServer, string dbName, string newDbName, bool noRecovery = false)
        {
            string backupFilePath;
            string dataDirectory = targetServer.InstallDataDirectory;

            BackupActionType backupType = BackupActionType.Database;

            backupFilePath = Path.Combine(fileShare, string.Format(backupFileNameTemplate, dbName, backupType.ToString()));

            BackupDeviceItem backupDeviceItem = new BackupDeviceItem(backupFilePath, DeviceType.File);

            //restore on the destination
            Restore restore = new Restore();

            restore.Action     = RestoreActionType.Database;
            restore.NoRecovery = noRecovery;

            restore.Devices.Add(backupDeviceItem);
            restore.Database        = newDbName;
            restore.ReplaceDatabase = false;
            DataTable logicalFilesDt = restore.ReadFileList(targetServer);

            DataRow[] foundLogicalFilesRows = logicalFilesDt.Select();
            if (!string.IsNullOrEmpty(dataDirectory))
            {
                foreach (DataRow row in foundLogicalFilesRows)
                {
                    string logicalFileName  = row["LogicalName"].ToString();
                    string physicalFileName = (logicalFileName.EndsWith("_log", StringComparison.OrdinalIgnoreCase)) ?
                                              Path.Combine(dataDirectory, string.Format(CultureInfo.InvariantCulture, "{0}.ldf", newDbName)) :
                                              Path.Combine(dataDirectory, string.Format(CultureInfo.InvariantCulture, "{0}.mdf", newDbName));
                    restore.RelocateFiles.Add(new RelocateFile(logicalFileName, physicalFileName));
                }
            }

            restore.SqlRestore(targetServer);
        }