How to make a distibutable .bak file?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I am trying to restore 2 databases. One is adventureworks database downloaded from msdn(its .bak file), and another one is created by me from one databases from my old server. Both have .bak extension, are in the same folder where the new instance where they should be restored, has all the access privileges. Now while restoring adventureworks database everything goes alright. While trying to restore the other .bak file made by me I get an access error, it wants access to its original server's DATA folder. Here is the error
enter image description here
Now, I want to know how to create .bak backup in order to restore it to any other server without this error.



Thanks for your time!










share|improve this question





























    up vote
    1
    down vote

    favorite












    I am trying to restore 2 databases. One is adventureworks database downloaded from msdn(its .bak file), and another one is created by me from one databases from my old server. Both have .bak extension, are in the same folder where the new instance where they should be restored, has all the access privileges. Now while restoring adventureworks database everything goes alright. While trying to restore the other .bak file made by me I get an access error, it wants access to its original server's DATA folder. Here is the error
    enter image description here
    Now, I want to know how to create .bak backup in order to restore it to any other server without this error.



    Thanks for your time!










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am trying to restore 2 databases. One is adventureworks database downloaded from msdn(its .bak file), and another one is created by me from one databases from my old server. Both have .bak extension, are in the same folder where the new instance where they should be restored, has all the access privileges. Now while restoring adventureworks database everything goes alright. While trying to restore the other .bak file made by me I get an access error, it wants access to its original server's DATA folder. Here is the error
      enter image description here
      Now, I want to know how to create .bak backup in order to restore it to any other server without this error.



      Thanks for your time!










      share|improve this question















      I am trying to restore 2 databases. One is adventureworks database downloaded from msdn(its .bak file), and another one is created by me from one databases from my old server. Both have .bak extension, are in the same folder where the new instance where they should be restored, has all the access privileges. Now while restoring adventureworks database everything goes alright. While trying to restore the other .bak file made by me I get an access error, it wants access to its original server's DATA folder. Here is the error
      enter image description here
      Now, I want to know how to create .bak backup in order to restore it to any other server without this error.



      Thanks for your time!







      sql-server backup restore






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 15:00









      Goncalo Peres

      135113




      135113










      asked Nov 10 at 14:23









      Eleonora Grigoryan

      56813




      56813




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          SQL Server won't automatically restore a database to the folder where the .BAK is located. This would be a disaster waiting to happen (say, that drive is out of space, or is encrypted or compressed, or SQL Server only has read access). Instead, it will try to place the files using the same configuration they had on the system they were backed up from. As Tibor alluded to in a comment below, you certainly could standardize all of your users' installs to have SQL Server installed to the same data folders as the source system, or at least all have the same data folder available to write to.



          Barring that, you need to use RESTORE ... WITH MOVE in order to place the data and log files in the right folder for this instance. First verify what that is:



          EXEC master.sys.sp_helpfile;


          That will give you two rows with paths like:



          filename
          ------------------------
          C:somepathmaster.mdf
          C:somepathmastlog.ldf


          Now run the following from the backup file:



          RESTORE FILELISTONLY FROM DISK = 'C:pathfile.bak';


          This will give you a row for each file in the backup, corresponding to each data/log file you'll need to restore (and move). For example:



          LogicalName
          -----------
          AdventureWorks2016_Data
          AdventureWorks2017_Log


          Take the C:somepath part from the first query, and the logical names from the second query, into a statement like this:



          RESTORE DATABASE AdventureWorks
          FROM DISK = 'C:pathfile.bak'
          WITH REPLACE, RECOVERY,
          MOVE 'AdventureWorks2016_Data' TO 'C:somepathaw.mdf',
          MOVE 'AdventureWorks2016_Log' TO 'C:somepathaw.ldf';


          There are likely complications when your backup includes things like filestream or In-Memory OLTP.



          But if your goal is to make a "distributable" backup that will do all that work for the end user, I don't think a traditional SQL Server backup is what you are looking for. Maybe you should look into database projects or providing scripts instead of backups.






          share|improve this answer






















          • Just to emphasize that where the database files are to be created will default to where they were when the backup was produced. So unless you can enforce a folder (the same for all your users) for where to put the database files, your users need to use the MOVE option in the RESTORE command. There's no option in the backup to say "put it in the destinations servers default database files folder".
            – Tibor Karaszi
            Nov 10 at 18:37










          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "182"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f222254%2fhow-to-make-a-distibutable-bak-file%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          SQL Server won't automatically restore a database to the folder where the .BAK is located. This would be a disaster waiting to happen (say, that drive is out of space, or is encrypted or compressed, or SQL Server only has read access). Instead, it will try to place the files using the same configuration they had on the system they were backed up from. As Tibor alluded to in a comment below, you certainly could standardize all of your users' installs to have SQL Server installed to the same data folders as the source system, or at least all have the same data folder available to write to.



          Barring that, you need to use RESTORE ... WITH MOVE in order to place the data and log files in the right folder for this instance. First verify what that is:



          EXEC master.sys.sp_helpfile;


          That will give you two rows with paths like:



          filename
          ------------------------
          C:somepathmaster.mdf
          C:somepathmastlog.ldf


          Now run the following from the backup file:



          RESTORE FILELISTONLY FROM DISK = 'C:pathfile.bak';


          This will give you a row for each file in the backup, corresponding to each data/log file you'll need to restore (and move). For example:



          LogicalName
          -----------
          AdventureWorks2016_Data
          AdventureWorks2017_Log


          Take the C:somepath part from the first query, and the logical names from the second query, into a statement like this:



          RESTORE DATABASE AdventureWorks
          FROM DISK = 'C:pathfile.bak'
          WITH REPLACE, RECOVERY,
          MOVE 'AdventureWorks2016_Data' TO 'C:somepathaw.mdf',
          MOVE 'AdventureWorks2016_Log' TO 'C:somepathaw.ldf';


          There are likely complications when your backup includes things like filestream or In-Memory OLTP.



          But if your goal is to make a "distributable" backup that will do all that work for the end user, I don't think a traditional SQL Server backup is what you are looking for. Maybe you should look into database projects or providing scripts instead of backups.






          share|improve this answer






















          • Just to emphasize that where the database files are to be created will default to where they were when the backup was produced. So unless you can enforce a folder (the same for all your users) for where to put the database files, your users need to use the MOVE option in the RESTORE command. There's no option in the backup to say "put it in the destinations servers default database files folder".
            – Tibor Karaszi
            Nov 10 at 18:37














          up vote
          2
          down vote



          accepted










          SQL Server won't automatically restore a database to the folder where the .BAK is located. This would be a disaster waiting to happen (say, that drive is out of space, or is encrypted or compressed, or SQL Server only has read access). Instead, it will try to place the files using the same configuration they had on the system they were backed up from. As Tibor alluded to in a comment below, you certainly could standardize all of your users' installs to have SQL Server installed to the same data folders as the source system, or at least all have the same data folder available to write to.



          Barring that, you need to use RESTORE ... WITH MOVE in order to place the data and log files in the right folder for this instance. First verify what that is:



          EXEC master.sys.sp_helpfile;


          That will give you two rows with paths like:



          filename
          ------------------------
          C:somepathmaster.mdf
          C:somepathmastlog.ldf


          Now run the following from the backup file:



          RESTORE FILELISTONLY FROM DISK = 'C:pathfile.bak';


          This will give you a row for each file in the backup, corresponding to each data/log file you'll need to restore (and move). For example:



          LogicalName
          -----------
          AdventureWorks2016_Data
          AdventureWorks2017_Log


          Take the C:somepath part from the first query, and the logical names from the second query, into a statement like this:



          RESTORE DATABASE AdventureWorks
          FROM DISK = 'C:pathfile.bak'
          WITH REPLACE, RECOVERY,
          MOVE 'AdventureWorks2016_Data' TO 'C:somepathaw.mdf',
          MOVE 'AdventureWorks2016_Log' TO 'C:somepathaw.ldf';


          There are likely complications when your backup includes things like filestream or In-Memory OLTP.



          But if your goal is to make a "distributable" backup that will do all that work for the end user, I don't think a traditional SQL Server backup is what you are looking for. Maybe you should look into database projects or providing scripts instead of backups.






          share|improve this answer






















          • Just to emphasize that where the database files are to be created will default to where they were when the backup was produced. So unless you can enforce a folder (the same for all your users) for where to put the database files, your users need to use the MOVE option in the RESTORE command. There's no option in the backup to say "put it in the destinations servers default database files folder".
            – Tibor Karaszi
            Nov 10 at 18:37












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          SQL Server won't automatically restore a database to the folder where the .BAK is located. This would be a disaster waiting to happen (say, that drive is out of space, or is encrypted or compressed, or SQL Server only has read access). Instead, it will try to place the files using the same configuration they had on the system they were backed up from. As Tibor alluded to in a comment below, you certainly could standardize all of your users' installs to have SQL Server installed to the same data folders as the source system, or at least all have the same data folder available to write to.



          Barring that, you need to use RESTORE ... WITH MOVE in order to place the data and log files in the right folder for this instance. First verify what that is:



          EXEC master.sys.sp_helpfile;


          That will give you two rows with paths like:



          filename
          ------------------------
          C:somepathmaster.mdf
          C:somepathmastlog.ldf


          Now run the following from the backup file:



          RESTORE FILELISTONLY FROM DISK = 'C:pathfile.bak';


          This will give you a row for each file in the backup, corresponding to each data/log file you'll need to restore (and move). For example:



          LogicalName
          -----------
          AdventureWorks2016_Data
          AdventureWorks2017_Log


          Take the C:somepath part from the first query, and the logical names from the second query, into a statement like this:



          RESTORE DATABASE AdventureWorks
          FROM DISK = 'C:pathfile.bak'
          WITH REPLACE, RECOVERY,
          MOVE 'AdventureWorks2016_Data' TO 'C:somepathaw.mdf',
          MOVE 'AdventureWorks2016_Log' TO 'C:somepathaw.ldf';


          There are likely complications when your backup includes things like filestream or In-Memory OLTP.



          But if your goal is to make a "distributable" backup that will do all that work for the end user, I don't think a traditional SQL Server backup is what you are looking for. Maybe you should look into database projects or providing scripts instead of backups.






          share|improve this answer














          SQL Server won't automatically restore a database to the folder where the .BAK is located. This would be a disaster waiting to happen (say, that drive is out of space, or is encrypted or compressed, or SQL Server only has read access). Instead, it will try to place the files using the same configuration they had on the system they were backed up from. As Tibor alluded to in a comment below, you certainly could standardize all of your users' installs to have SQL Server installed to the same data folders as the source system, or at least all have the same data folder available to write to.



          Barring that, you need to use RESTORE ... WITH MOVE in order to place the data and log files in the right folder for this instance. First verify what that is:



          EXEC master.sys.sp_helpfile;


          That will give you two rows with paths like:



          filename
          ------------------------
          C:somepathmaster.mdf
          C:somepathmastlog.ldf


          Now run the following from the backup file:



          RESTORE FILELISTONLY FROM DISK = 'C:pathfile.bak';


          This will give you a row for each file in the backup, corresponding to each data/log file you'll need to restore (and move). For example:



          LogicalName
          -----------
          AdventureWorks2016_Data
          AdventureWorks2017_Log


          Take the C:somepath part from the first query, and the logical names from the second query, into a statement like this:



          RESTORE DATABASE AdventureWorks
          FROM DISK = 'C:pathfile.bak'
          WITH REPLACE, RECOVERY,
          MOVE 'AdventureWorks2016_Data' TO 'C:somepathaw.mdf',
          MOVE 'AdventureWorks2016_Log' TO 'C:somepathaw.ldf';


          There are likely complications when your backup includes things like filestream or In-Memory OLTP.



          But if your goal is to make a "distributable" backup that will do all that work for the end user, I don't think a traditional SQL Server backup is what you are looking for. Maybe you should look into database projects or providing scripts instead of backups.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 19:06

























          answered Nov 10 at 15:12









          Aaron Bertrand

          148k18280480




          148k18280480











          • Just to emphasize that where the database files are to be created will default to where they were when the backup was produced. So unless you can enforce a folder (the same for all your users) for where to put the database files, your users need to use the MOVE option in the RESTORE command. There's no option in the backup to say "put it in the destinations servers default database files folder".
            – Tibor Karaszi
            Nov 10 at 18:37
















          • Just to emphasize that where the database files are to be created will default to where they were when the backup was produced. So unless you can enforce a folder (the same for all your users) for where to put the database files, your users need to use the MOVE option in the RESTORE command. There's no option in the backup to say "put it in the destinations servers default database files folder".
            – Tibor Karaszi
            Nov 10 at 18:37















          Just to emphasize that where the database files are to be created will default to where they were when the backup was produced. So unless you can enforce a folder (the same for all your users) for where to put the database files, your users need to use the MOVE option in the RESTORE command. There's no option in the backup to say "put it in the destinations servers default database files folder".
          – Tibor Karaszi
          Nov 10 at 18:37




          Just to emphasize that where the database files are to be created will default to where they were when the backup was produced. So unless you can enforce a folder (the same for all your users) for where to put the database files, your users need to use the MOVE option in the RESTORE command. There's no option in the backup to say "put it in the destinations servers default database files folder".
          – Tibor Karaszi
          Nov 10 at 18:37

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f222254%2fhow-to-make-a-distibutable-bak-file%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3

          Museum of Modern and Contemporary Art of Trento and Rovereto