环境:
在生产环境中,我需要移动MDF& LDF文件到新驱动器。由于我有一个停止活动事务的时间窗口,我想我可以只备份数据库,然后在将文件组配置到新位置时将其还原。
我认为这比使用新文件名分离和重新附加数据库要好得多。
由于我是新手,想在这里咨询专家。任何建议/建议非常感谢。
答案 0 :(得分:1)
这是一个简单的例子。它假定您的数据库有一个.mdf
(数据)文件和一个.ldf
(日志)文件。我将使用[model]
数据库作为示例。
--First, make note of the current location of the db files.
--Copy and paste the physical_names somewhere. Trust me, if you forget
--where the files were originally, this will save you some heartache.
SELECT d.name, f.name, f.physical_name
FROM master.sys.master_files f
JOIN master.sys.databases d
ON d.database_id = f.database_id
WHERE d.name = 'model' --Replace with the name of your db.
--Now set the new file paths.
--You can run the ALTER DATABASE statements while the db is online.
--Run once for the mdf/data file.
ALTER DATABASE [model] --Replace with the name of your db.
MODIFY FILE
(
NAME = 'modeldev', --this is the "logical" file name.
FILENAME = 'D:\SqlData\model.mdf' --Replace with the new path\filename.
)
--Run once for the ldf/data file.
ALTER DATABASE [model] --Replace with the name of your db.
MODIFY FILE
(
NAME = 'modellog',
FILENAME = 'D:\SqlData\modellog.ldf' --Replace with the new path\filename.
)
--When business rules allow, take the db OFFLINE.
ALTER DATABASE [model] --Replace with the name of your db.
SET OFFLINE
--Move the physical db files to the new location on disk.
--Bring the db back ONLINE to complete the task.
ALTER DATABASE [model] --Replace with the name of your db.
SET ONLINE