SQL Server restoring backup from different version

dgsult0t  于 2023-10-15  发布在  其他
关注(0)|答案(4)|浏览(110)

I need to restore file.bak into SQL Server 2012 SP1 11.0.3000.0 from SQL Server 2017 14.0.1000.169 What I need to do as I'm receiving the error The media family on device 'myfile.bak' is incorrectly formed.

wlwcrazw

wlwcrazw1#

It is not possible to restore to a lower major version. But there is a workaround to export and then import all objects and data:

  • create an empty database on lower version SQL Server. Make sure db COLLATION is the same as on source database.
  • script CREATE of all objects (schemas, tables/indexes/triggers, stored procedures, etc)
  • Run the script on older SQL. Use Schema Compare tool (eg. RedGate) to make sure that all objects are identical. Such tool can also generate scripts to make them equal (super handy!). Proceed to next step once objects are equal.
  • Export all table data into file system using BCP OUT command-line tool. You will get one file per table. This generates the script:

-- Generate Export script

select 'bcp.exe ' + db_name() + '..' + name + ' out "c:\bcp' + name + '.bcp" -S' + @@servername + ' -N -T' from sysobjects where xtype = 'U'

  • Import rows with this generated script. Adjust file path. You do NOT have to copy files to the new server. BCP sees local folders of the machine where you run it, and file shares:

-- Generate Import script

select 'exec master..xp_cmdshell ''bcp.exe ' + db_name() + '..' + o.name + ' in "c:\bcp' + o.name + '.bcp" -S' + @@servername + ' -N -T' + case when c.id is null then '' else ' -E' end + '''' -- E to import identity from sysobjects o left join (select distinct id from syscolumns where status & 0x80 <> 0) c on o.id = c.id where o.xtype = 'U'

  • Verify row counts match between source db and target db. You can Right-click the database -> Reports -> Standard Reports -> Top tables by row count (or similar)

If you are familiar with transactional (or any) replication, maybe simpler option would be to create a SNAPSHOT REPLICATION to move the schema and data. Create a snapshot and apply it to a lower version subscriber. It can go 2 versions lower.

Or, avoid all the mess and upgrade lower environment to match, so you can use a normal restore from backup.

jtoj6r0c

jtoj6r0c2#

I've had the same issue and I can positively say that there is no way to directly restore a backup from a newer to an older server in the way you want. Depending on the size of your database you could actually achieve what you are looking for by using CREATE TO scripts for your database. This guide helped me in doing this

e4yzc0pl

e4yzc0pl3#

There are two ways you can get the database on your older one:

You can create a script for your database and run at your older sql server instance.

1. To generate script of your database just right click on your database in SSMS.
 2. Go to Tasks -> Generate Script (This will open wizard to generate script).
 3. Click on Next (You can click on advance to set advance options)
 4. Then Next then Assign the output file path and name.
 5. Then click on Next then Next to generate script.

You can create new backup file after change in compatibility option.

1. Go to your database and right click on database in SSMS.
 2. Open the properties window for your database.
 3. Change the compatibility mode to your target sql server version.

**

**

Then create new back up file and restore to your target sql server.

mrzz3bfm

mrzz3bfm4#

You cannot restore a newer backup onto an older instance. The version of the instance must be greater than or equal to the version on which the backup was taken. When restoring onto a later version the database is automatically upgraded.

相关问题