How can I backup a remote SQL Server database to a local drive?
Asked Answered
R

24

312

I need to copy a database from a remote server to a local one. I tried to use SQL Server Management Studio, but it only backs up to a drive on the remote server.

Some points:

  • I do not have access to the remote server in a way that I could copy files;
  • I do not have access to setup a UNC path to my server;

Any ideas of how can I copy this database? Will I have to use 3rd party tools?

Romanist answered 15/10, 2010 at 12:28 Comment(3)
I think if you don't have access to the directory structure at all, you will be challenged trying to do this.Reformatory
You're just trying to copy the database rather than specifically back it up? If so you can use the Copy Database Wizard or (in SQL Server 2008) use the "Generate Scripts" option to script the Schema and Data. Redgate SQL Compare and Data Compare can also be useful here.Selfsatisfaction
@MartinSmith According to your suggestion,I generated script using Generate and Publish Scripts option. I get all the tables and schema.But I didn't get any data with tables. How can I fix this.Decree
A
228

In Microsoft SQL Server Management Studio you can right-click on the database you wish to backup and click Tasks -> Generate Scripts.

This pops open a wizard where you can set the following in order to perform a decent backup of your database, even on a remote server:

  • Select the database you wish to backup and hit next,
  • In the options it presents to you:
    1. In 2010: under the Table/View Options, change 'Script Data' and 'Script Indexes' to True and hit next,
    2. In 2012: under 'General', change 'Types of data to script' from 'Schema only' to 'Schema and data'
    3. In 2014: the option to script the data is now "hidden" in step "Set Scripting Options", you have to click the "Advanced" and set "Types of data to script" to "Schema and data" value
  • In the next four windows, hit 'select all' and then next,
  • Choose to script to a new query window

Once it's done its thing, you'll have a backup script ready in front of you. Create a new local (or remote) database, and change the first 'USE' statement in the script to use your new database. Save the script in a safe place, and go ahead and run it against your new empty database. This should create you a (nearly) duplicate local database you can then backup as you like.

If you have full access to the remote database, you can choose to check 'script all objects' in the wizard's first window and then change the 'Script Database' option to True on the next window. Watch out though, you'll need to perform a full search & replace of the database name in the script to a new database which in this case you won't have to create before running the script. This should create a more accurate duplicate but is sometimes not available due to permissions restrictions.

Abrego answered 4/2, 2012 at 15:30 Comment(13)
In SQL Server Management Studio 2012 there is no such option as 'Script Data', so in newer versions do the following for the step 2: under 'General', change 'Types of data to script' from 'Schema only' to 'Schema and data'.Size
I checked it on SQL Server 2008 R2 and it's worked like charm. In care of using it for backup strategy need to some additional work by myself but I think is better than using some third parties tools like RedGate etc. Maybe I'v paranoia but I think most of third party tools have extra code and will got my control and reduce simplicity and clarifying of script. Thanks so much.Trod
The only bring in the schema. It doesn't bring the data. I.e. No INSERT statements.Ruskin
@ShaunLuttin If you make sure to change 'Types of data to script' from 'Schema only' to 'Schema and data in the second step (on 2012) the INSERT statements should show.Abrego
In SSMS 2014 the option to change from schema only to schema & data is now hidden behind an "Advanced" when selecting where to save the script.Advisory
is this feature available in 2008 R2Tittup
Use SQL Razor tool where you can backup you database locally: razorsql.com/docs/database_backup_tool.htmlGraticule
When I try to run a script using this method I get an erroor from SQL server management studio about "Insufficient Memory to continue the execution of the program. (mscorelib)"Osorio
Does this script way copy the data as well ?Sexy
This is not a backup - see the answer from T. WebsterSodality
Generate scripts is also painfully slow, and just fails a lot of times. A LOTTT OFFF TIMESSSS. Whereas Backup/Restore is miraculously fast even for large dbs. I would just love if there is a way in management studio to transfer a backup from remote server to my machineTeeters
On SQL Sever Management Studio 2017, Generate Scripts is next to Tasks, not on a submenu of it.Ossetia
Apparently not the actual correct answer although it will do the job. Scroll down to see the perfect answer.Southern
R
60

To copy data and schema only (will not copy stored procedures, functions etc.), use the SQL Server Import and Export Wizard, and choose New... when choosing the destination database.

Right Click Database > Tasks > Import Data.

Choose a Data Source

  • Data Source: SQL Server Native Client
  • Server Name: the remote server
  • Authentication:
  • Database: the db name

Choose a Destination

  • Data Source: SQL Server Native Client
  • Server Name: the local server
  • Authentication:
  • Database: New...

The rest is straight forward.

Ruskin answered 18/4, 2015 at 3:47 Comment(3)
Welp. It doesn't set identities and default constraints unfortunately. :(Advisory
This is way simpler and would be my go to, but if for some reason you need columns like object_id, create_data, modify date AS IS in sys.tables just remember that they will be modified upon creation.Deadbeat
@Advisory you can generate scripts first and then import/export data by enabling identity insert on mapping step.Toggery
S
60

First, grant full control permissions to a local path on your machine (as shown below) with Everyone. (Or alternatively grant permissions specifically to the SQL Server Agent account).

Second, execute the following:

BACKUP DATABASE [dev] TO  DISK = N'\\myMachine\c\dev.bak' WITH COPY_ONLY, INIT;
Shel answered 2/8, 2016 at 20:28 Comment(3)
This assumes that the remote machine has access to your local machines files, which is not the general case.Rathbun
Even after sharing local folder. Error: Operating system error 53(The network path was not found.).Phonetic
BACKUP DATABASE [Your_DB] TO DISK = N'G:\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\backup_custom.trn' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N'MyDB-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10 GORegelation
H
33

You cannot create a backup from a remote server to a local disk - there is just no way to do this. And there are no third-party tools to do this either, as far as I know.

All you can do is create a backup on the remote server machine, and have someone zip it up and send it to you.

Henrik answered 15/10, 2010 at 12:43 Comment(17)
I disagree. You can in many circumstances use MS SQL Server Management Studio's 'Generate Scripts' command to create a script that can be run to generate a local database which you can then do with as you wish. See my answer below or Martin Smith's comment on the question.Abrego
I second Daniel. You can use 'Generate Scripts' and 'Export Data' to copy data from remote server to local server.Petulant
@Rafid: yes -but that's NOT a true BACKUP - it's a script/data export .....Henrik
@Henrik What is there about a "true backup" that is different from an exported copy? The log? Anything else?Skink
@Dronz: YES! The script export will reproduce the structure and possibly even the data in a table - but it cannot include stuff like transaction logs and statistics and other vital parts of a SQL Server database.Henrik
My understanding is that the script also doesn't bring the data.Ruskin
@ShaunLuttin, you can include the data.Cangue
so u say that back up is not possible when we only possess database credentials and NOT THE SERVER CREDENTIALS??Tittup
@clarifier: sure you can make a backup - but the resulting .bak file will be written on the REMOTE server into its filesystemHenrik
but here the thing is, i only have access to database on the server and i do not have that server credentials to log in to it.Tittup
@clarifier: then you're out of luck - the backup will be made on the server ....Henrik
i tried writing a vbscript to take a back up of database (.bak file) and the requirement is to schedule it on a daily basis.. so now i will ask for privileges to access to that server..Thanks alot @Henrik and may i know the reason why this feature is not provided from remote server??Tittup
@clarifier: do you really want an unknown, remote server under someone else's control to have write access to your local machine?!?!?!?!?!?Henrik
Let us continue this discussion in chat.Tittup
Generate scripts is also painfully slow, and just fails a lot of times. A LOTTT OFFF TIMESSSS. Whereas Backup/Restore is miraculously fast even for large dbs. I would just love if there is a way in management studio to transfer a backup from remote server to my machineTeeters
If you set up a share on your local computer and set the rights correctly Everyone with read/write you can use the UNC path \\your_pc\your_share\db.bak on the remote server :) ...I do realize that the OP specifically states he doesn't have those rights , but it is possibleSeattle
@MadisonCourto: your code is for MySQL .- not for Microsoft SQL Server ....Henrik
C
17

I know this is late answer but I have to make a comment about most voted answer that says to use generate scripts option in SSMS.

Problem with that is this option doesn’t necessarily generate script in correct execution order because it doesn't take dependencies into account.

For small databases this is not an issue but for larger ones it certainly is because it requires to manually re-order that script. Try that on 500 object database ;)

Unfortunately in this case the only solution are third party tools.

I successfully used comparison tools from ApexSQL (Diff and Data Diff) for similar tasks but you can’t go wrong with any other already mentioned here, especially Red Gate.

Cydnus answered 12/8, 2013 at 8:55 Comment(2)
i do not agree, for me sql always orders the dependancies correctly, do u have a specific example of how to reproduce the behaviour that you have mentioned?Swope
also, there's an option to take this matter into accountAutarky
C
13

You can try SQLBackupAndFTP. It will create scripts to create all the objects in your database and INSERT statements for all the rows in your tables. In any database you can run this script file and the entire database will be re-created.

Cathe answered 9/2, 2012 at 20:39 Comment(3)
Seconded. Had to install a new version of sql server management studio and couldn't script like I used to. This tool took care of it quickly and without issues even without having full access to the remote server (appharbor sqlserver add-on)Abrego
Please note this software heavily uses resources.Enticement
On top of being buggy and not entirely sure this works correctly. Can connect no problem to a remote server via PHP, but this can not? When I have proper settings on the server itself. SO ya... moving on.Dorie
M
10

There is the 99% solution to get bak file from remote sql server to your local pc. I described it there in my post http://www.ok.unsode.com/post/2015/06/27/remote-sql-backup-to-local-pc

In general it will look like this:

  • execute sql script to generate bak files

  • execute sql script to insert each bak file into temp table with varbinary field type and select this row and download data

  • repeat prev. step as many time as you have bak files

  • execute sql script to remove all temporary resources

that's it, you have your bak files on your local pc.

Macilroy answered 27/6, 2015 at 11:52 Comment(0)
B
8

I know this is an older post, but for what it's worth, I've found that the "simplest" solution is to just right-click on the database, and select "Tasks" -> "Export Data-tier Application". It's possible that this option is only available because the server is hosted on Azure (from what I remember working with Azure in the past, the .bacpac format was quite common there).

Once that's done, you can right-click on your local server "Databases" list, and use the "Import Data-tier Application" to get the data to your local machine using the .bacpac file.

Just bear in mind the export could take a long time. Took roughly two hours for mine to finish exporting. Import part is much faster, though.

Barbital answered 30/4, 2020 at 18:47 Comment(2)
This is exactly what I needed. Thanks!Neogaea
Doesn't include Stored Procedures, just to note.Unclear
W
7

Look at this blog for a description how to copy a remote database:

Backup a SQL Server 2008 Database From a Shared Hosting Environment

Willette answered 25/5, 2011 at 8:32 Comment(4)
This approach explains how to script the structure of the database, create it locally and then use SQL management tools to copy data between a local and remote database.Motherinlaw
You can flip the 'Script Data' option to True in order to also script the data in one go.Abrego
Copy and backup are two different things.Rathbun
That blog does not explain how to copy the data to your local PC. Also, in Management Studio, the Tasks > Export Data wizard says "To move or copy from one server instance to another, cancel this wizard and use the Copy Database Wizard instead". Mgmt Studio Express does not seem to have this wizard.Fool
M
5

You can use Copy database ... right click on the remote database ... select tasks and use copy database ... it will asks you about source server and destination server . that your source is the remote and destination is your local instance of sql server.

it's that easy

Montparnasse answered 11/7, 2012 at 13:58 Comment(3)
Copy database requires SysAdmin privileges. This is not a solution for regular users of the database.Blanc
In my issue today, our dev team does have sysadmin privileges. So I'll use Pouyan's approach.Tinct
but i need to do it on a daily basis and i need script to accomplish thisTittup
H
5

The answers above are just not correct. A SQL Script even with data is not a backup. A backup is a BAK file that contains the full database in its current structure including indizes.

Of course a BAK file containg the full backup with all data and indizes from a remote SQL Server database can be retrieved on a local system.

This can be done with commercial software, to directly save a backup BAK file to your local machine, for example This one will directly create a backup from a remote SQL db on your local machine.

Hartebeest answered 3/2, 2014 at 11:55 Comment(2)
You need sysadmin permissions to do this.Direful
Right. The account (windows or sql server) used to remotely connect to the sql server instance needs sysadmin permissions to retrieve the backup junks and store them onto your local machine. The mentioned tool from FIDA Software does encrypt and compress network traffic from and to sql server while retrieving the backup junks. Once you have the bak file on your machine you can restore it where you want.Hartebeest
W
4

If you are in a local network you can share a folder on your local machine and use it as destination folder for the backup.

Example:

  • Local folder:

    C:\MySharedFolder -> URL: \\MyMachine\MySharedFolder

  • Remote SQL Server:

    Select your database -> Tasks -> Back Up -> Destination -> Add -> Apply '\\MyMachine\MySharedFolder\BACKUP_NAME.bak'

Wentworth answered 10/1, 2018 at 15:54 Comment(1)
This worked for me, thank you! Just had to make sure to update the Sharing settings of my local shared folder to give Everyone Read/Write PermissionsEnvirons
A
3

The AppHarbor gang has been struggling with this and has developed a temporary solution using SQL server management objects and SqlBulkCopy.

Check out their blog post about it, or go straight to the code.

They've only tested it with AppHarbor but it may be worth checking out.

Abrego answered 16/12, 2011 at 8:52 Comment(0)
G
3

For 2019, I would recommend using mssql-scripter if you want an actual local backup. Yes it's scripts but you can adjust it to include whatever you want, which can include all of the data. I wrote a bash script to do automated daily backups using this on a linux machine. Checkout my gist:

https://gist.github.com/tjmoses/45ee6b3046be280c9daa23b0f610f407
Gyroplane answered 15/11, 2019 at 18:8 Comment(0)
B
3

Create a local shared folder, with "everyone" read/write privileges

Connect to the target database, start the backup and point to the share like below

\mymachine\shared_folder\mybackup.bak

(Tried on Windows domain environment)

Bailsman answered 5/3, 2020 at 5:59 Comment(0)
M
2

As Martin Smith said, if you have no access to the machine or the filesystem, you will need to use third party tools, like Red Gate or Adept to do a compare on the source and destination systems. Red Gate's tools will allow you to copy the objects and schemas AND the data.

Mell answered 15/10, 2010 at 15:1 Comment(0)
T
2

Using a stored procedure in SQL Server 2019

CREATE PROCEDURE [dbo].[BackupDB]
@backupPath nchar(200), 
@dbname nchar(50)
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @backupDate as nchar(10) 
    SELECT @backupDate = CONVERT(VARCHAR(10),GETDATE(),112) 
    IF DB_ID(TRIM(@dbname)) IS NOT NULL
    BEGIN
        SET @backupPath = TRIM(@backupPath) + TRIM(@dbname) + '_' + TRIM(@backupDate) + '.BAK'
        BACKUP DATABASE @dbname TO DISK = @backupPath
    END 
END
GO
Tackle answered 16/11, 2021 at 3:46 Comment(0)
H
1

yone way you could take a backup from a remote SQL Server instance to your local drive, given the following condition is met:

  1. You have a shared folder on your local drive.
  2. the shared folder is accessible from the SQL Server box.

Now when specifying the backup command, use the shared folder path when specifying the disk option.

Hyperspace answered 15/5, 2013 at 6:11 Comment(1)
And how is your answer different from the one by @Vivek?Moleskins
F
1

just try this one:

1)Share a folder with full permission in your computer

2) in your SQL server : control panel -> administrative tools -> services -> right click on all SQL services

on log on tab should start with your domain administrator

3) in maintenance wizard of sql server place the back up location and folder (\yourcomputername\sharedfoldernam)

I did remote backup on 8 server of sql server 2008 in our company

Floreneflorentia answered 8/1, 2014 at 19:56 Comment(0)
S
1

I'm astonished that no-one has mentioned the scripted backup solution offered by Ola Hallengren which absolutely does allow you to backup a DB from a remote server to a UNC path on your network for free (I'm actually using it as I type to backup a DB from a dev server to which I have no remote access other than through SSMS to a share on my dev PC). This has been available since 2008 and works on SQL Server 2005 through to 2014.

You need to ensure that the share you set up has enough access: I tend to allow full read/write to the 'Everyone' AD group for the duration of the backup process because I'm too lazy to figure out anything more restrictive but that's personal choice.

It's well-used, well-documented and very flexible. I tend to put the procs and the logging table in their own little utility database and then fire it up. Provided everything is in your AD domain and not remote in the sense that it's out on a co-located server or something, this works very well.

Apologies for adding to a very old thread but I came across this when looking for something else and figured it was a worthwhile addition for anyone looking for this topic.

Scarito answered 30/5, 2014 at 11:7 Comment(1)
Does it still work for SQL Server 2022? I'm not seeing anything documented about that. Where do you specify the remote server?Vinia
H
0

I could do that once...TO do this you have to have a share opened on the remote server. then you can directly place the backup on the share itself, than the default location...

Usually the admin takes the backup and shares it with us in some shared folder. I tried if that will work if i place the backup there. It worked.

Hermann answered 14/6, 2012 at 9:51 Comment(0)
F
0

If you use the Generate Scripts under SSMS, click the Advanced button. Under the 'Generate Scripts for the dependent objects' option, click True. By clicking that any dependencies of each object will also be scripted out in proper order.

Folie answered 3/10, 2013 at 5:12 Comment(1)
True is the default in SSMS v17.8.1.Mestas
S
0

Some third-party backup programs allow setting file transferring with specific network permissions. It it very useful when SQL Server service is running under restricted account and does not have enough network permissions. Try using EMS SQL Backup which solves this task.

Subjection answered 7/4, 2014 at 6:17 Comment(0)
T
0

I use Redgate backup pro 7 tools for this purpose. you can create mirror from backup file in create tile on other location. and can copy backup file after create on network and on host storage automatically.

Thieve answered 16/6, 2014 at 8:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.