Beispiel #1
0
        /// <summary>
        ///   Restores each of the backups. If the default file location is available it will move the files to there.
        /// </summary>
        /// <param name="backupOrder">An ordered list of backups to apply.</param>
        /// <param name="databaseName">Database to restore to.</param>
        /// <param name="fileRelocation">Option for renaming files during the restore.</param>
        public void Restore(IEnumerable <BackupMetadata> backupOrder, string databaseName,
                            Func <string, string> fileRelocation = null)
        {
            var restore = new Restore {
                Database = databaseName, NoRecovery = true
            };

            foreach (var backup in backupOrder)
            {
                var device           = BackupFileTools.IsUrl(backup.PhysicalDeviceName) ? DeviceType.Url : DeviceType.File;
                var backupDeviceItem = new BackupDeviceItem(backup.PhysicalDeviceName, device);
                if (_credentialName != null && device == DeviceType.Url)
                {
                    backupDeviceItem.CredentialName = _credentialName;
                }

                restore.Devices.Add(backupDeviceItem);

                var defaultFileLocations = DefaultFileLocations();
                if (defaultFileLocations != null)
                {
                    restore.RelocateFiles.Clear();
                    foreach (var file in restore.ReadFileList(_server).AsEnumerable())
                    {
                        var physicalName = (string)file["PhysicalName"];
                        var fileName     = Path.GetFileName(physicalName) ??
                                           throw new InvalidBackupException($"Physical name in backup is incomplete: {physicalName}");

                        if (fileRelocation != null)
                        {
                            fileName = fileRelocation(fileName);
                        }

                        var path = (string)file["Type"] == "L" ? defaultFileLocations?.Log : defaultFileLocations?.Data;
                        path = path ?? Path.GetFullPath(physicalName);

                        var newFilePath = Path.Combine(path, fileName);

                        restore.RelocateFiles.Add(new RelocateFile((string)file["LogicalName"], newFilePath));
                    }
                }

                _server.ConnectionContext.StatementTimeout = 86400; // 60 * 60 * 24 = 24 hours

                restore.SqlRestore(_server);
                restore.Devices.Remove(backupDeviceItem);
            }
        }
Beispiel #2
0
        private void Backup(Backup backup, string backupDirectoryPathQuery, string databaseName,
                            BackupFileTools.BackupType type)
        {
            var backupDirectory = BackupDirectoryOrDefault(backupDirectoryPathQuery);
            var filePath        =
                $"{backupDirectory}/{databaseName}_backup_{DateTime.Now.ToString("yyyy_MM_dd_hhmmss_fff")}.{BackupFileTools.BackupTypeToExtension(type)}";
            var deviceType = BackupFileTools.IsUrl(filePath) ? DeviceType.Url : DeviceType.File;

            var bdi = new BackupDeviceItem(filePath, deviceType);

            if (_credentialName != null && deviceType == DeviceType.Url)
            {
                bdi.CredentialName = _credentialName;
            }

            backup.Devices.Add(bdi);
            backup.SqlBackup(_server);
        }