简体   繁体   English

从另一台计算机上的.bak文件还原数据库

[英]Restoring a database from .bak file on another machine

I've not done much SQL and am still pretty new to this, so please excuse what's probably a basic question. 我没有做太多的SQL,我仍然很新,所以请原谅可能是一个基本问题。

I've been asked to look into creating an SQL job to backup our databases, store the .baks on another machine and then to restore them to a second server. 我被要求研究创建一个SQL作业来备份我们的数据库,将.baks存储在另一台机器上,然后将它们恢复到第二台服务器。 I've been doing a bit of research and playing with SSMS and have back-ed up the database to my personal machine by setting up a share and running a backup job to the share location. 我一直在做一些研究和SSMS玩,并通过设置共享和运行备份作业到共享位置将数据库备份到我的个人计算机。 I'm now trying to create a new database (on the same server I back-ed up from) by restoring the .bak file (but giving the database I'm trying to create a new name and what-not) but am unable to specify restoring it from the share, like I did when backing it up/I can't find how to specify other network locations and am just browsing the server's C drive when I try to locate the file. 我现在正在尝试通过恢复.bak文件创建一个新数据库(在我备份的同一台服务器上)(但是给数据库我正在尝试创建一个新名称而不是什么)但是我无法指定从共享中恢复它,就像我在备份时所做的那样/我无法找到如何指定其他网络位置,而我只是在尝试查找文件时浏览服务器的C驱动器。

For now, I'm just using the built-in wizards to try and achieve this (open SSMS -> Connect to server -> right click DataBases -> Restore DataBases and then select From Device and browse to find the file). 现在,我只是使用内置向导来尝试实现这一点(打开SSMS - >连接到服务器 - >右键单击DataBases - > Restore DataBases,然后选择From Device并浏览查找文件)。

This isn't the final process, just me trying to get to grips with how this works. 这不是最后的过程,只是我试图掌握这是如何工作的。 As I said, the idea is to ultimately have a scheduled job to backup the DB from server1 to a .bak on, say, my personal machine and then to restore that to a DB on server2 (different network, different city) and, probably, with a series of SQL commands rather than using the wizard every time (there are a few DBs that'll, ultimately, need backing up). 正如我所说,我的想法是最终有一个预定的工作,将数据库从server1备份到我的个人计算机上的.bak,然后将其恢复到server2(不同网络,不同城市)上的数据库,并且可能,使用一系列SQL命令而不是每次都使用向导(有一些DB最终需要备份)。

My apologies for the, possibly, quite drawn out and convoluted question - essentially, all I need to know is can I/how can I restore a DB in SSMS from a .bak on a different machine? 我为这个可能非常抽象和复杂的问题道歉 - 基本上,我需要知道的是我/如何在不同的机器上从.bak恢复SSMS中的数据库? Many thanks 非常感谢

You could use something like the following script. 您可以使用类似以下脚本的内容。 It restores a database from the filesystem, and it overwrites the existing database with the name of "MyDB", moving the files to new locations of your choice in the process. 它从文件系统恢复数据库,并用名称“MyDB”覆盖现有数据库,将文件移动到您选择的新位置。

RESTORE DATABASE
    MyDB
FROM DISK = '\\MyShare\MyBackup.bak'
WITH 
    MOVE 'DataFile' TO 'D:\myNewDBLocation\DataFile.mdf',
    MOVE 'LogFile' TO 'E:\\myNewDBLocation\LogFile.ldf'
, REPLACE

You can find out the name of the llogical files (in the above, those are called DataFile and LogFile by running the following: 您可以找到llogical文件的名称(在上面,通过运行以下命令将其称为DataFileLogFile

RESTORE FILELISTONLY 
FROM DISK = '\\MyShare\MyBackup.bak'

Additional information about various options and parameters: 有关各种选项和参数的其他信息:

RESTORE (Transact-SQL) RESTORE(Transact-SQL)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM