未加星标

Restore or Attach a Windows SQL Server Database Backup on a Linux Server

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二03 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

By:Daniel Farina || Related Tips:More >SQL Server on linux

Problem

Now that SQL Server is available on Linux, many companies may start to use a Linux server for testing database development due to license costs. In this tip I will show you how to move a copy of a database from windows SQL Server to a Linux SQL Server installation.

Solution

It's a fact that companies across the globe want to reduce costs. That and the forthcoming edition of SQL Server for Linux brings us a possible scenario where non-production SQL Server databases may be running on top of Linux systems. Because of this, we must know how to move a SQL Server database from Windows to Linux.

Create a SQL Server Database Backup

The first step to copy a database to Linux is, of course, to take a backup of the database we want to move. But remember that if the database is in production you should take the backup with the COPY_ONLY option to not break the backup sequence. If you don't know about this concept I suggest you to read the SQL Server Backup Options and Commands Tutorial and the following tip COPY_ONLY Backups with SQL Server in order to know more.

Copying SQL Server Backup Files to Linux

The most common way to move files into Linux is with Secure Copy (also known as SCP). It is based on the Secure Shell (referred to as SSH) protocol. For us who are used to the Windows GUI, I suggest you use the open source program WinSCP, which can be downloaded for free at this URL: https://winscp.net/eng/download.php .

When you start WinSCP it shows a login screen like the image below, asking you for the connection details.


Restore or Attach a Windows SQL Server Database Backup on a Linux Server

After successfully logging in, the program shows two panels the file and folder structure of the current machine (on the left) and the remote server (on the right). To copy a file is as easy as dragging the file from the local file system panel to the remote file system panel. I am using my home folder on the remote system to copy the backup file.


Restore or Attach a Windows SQL Server Database Backup on a Linux Server
Restoring the SQL Server Database on a Linux Server

Now that we have the backup file on our Linux server, it's time to restore the database. Let's check the contents of the backup file.

USE master
GO
RESTORE FILELISTONLY FROM DISK = '/home/daniel/AdventureWorks2012.bak'

As you can see on the image below, the database contains one data file and one log file named AdventureWorks2012_Data and AdventureWorks2012_Log respectively.


Restore or Attach a Windows SQL Server Database Backup on a Linux Server

If you take a look at the PhysicalName column from the result, you will see that it is represented as a Windows path. This means that we must restore the database using the WITH MOVE option; otherwise we will get an error message telling us that the Directory lookup has failed as shown below.


Restore or Attach a Windows SQL Server Database Backup on a Linux Server

I will restore the database to the default SQL Server data location which is /var/opt/mssql/data using the WITH MOVE option.

USE master;
RESTORE DATABASE AdventureWorks2012
FROM DISK = '/home/daniel/AdventureWorks2012.bak'
WITH MOVE 'AdventureWorks2012_Data' TO '/var/opt/mssql/data/AdventureWorks2012.mdf',
MOVE 'AdventureWorks2012_Log' TO '/var/opt/mssql/data/AdventureWorks2012.ldf',
STATS = 10

The next is a screen capture showing that we have successfully restored the database, and in this case the database was upgraded to the latest version.


Restore or Attach a Windows SQL Server Database Backup on a Linux Server
Attaching a SQL Server Database

To attach a database on a SQL Server instance running on Linux is not much different. After copying the mdf and ldf files to the Linux server to the SQL Server data location which is /var/opt/mssql/data we must grant ownership of the files to the SQL Server user account, usually mssql. To change ownership we must use the chown command as root.

I have copied two files of a database named DBA (DBA.mdf and DBA_log.ldf) to the location /var/opt/mssql/data. In order to change ownership of those files to the SQL Server user account I have to run the following command.

Chown mssql:mssql /var/opt/mssql/data/DBA*

The mssql:mssql on the command represents the user and group to grant ownership, the rest is the file names (notice that I have used a wildcard DBA*).

Also we need to grant read and write permissions on the files to the SQL Server user account. We do so with the chmod command.

chmod u=+rw,g=+rw,o=-rw /var/opt/mssql/data/DBA*

The previous statement grants read and write permissions to the owner and to the group the owner belongs and removes permissions to the rest of the users.


Restore or Attach a Windows SQL Server Database Backup on a Linux Server

After completing the previous step I am able to attach the database the way we normally do.

USE master;
CREATE DATABASE DBA
ON PRIMARY (FILENAME = '/var/opt/mssql/data/DBA.mdf'),
(FILENAME = '/var/opt/mssql/data/DBA_log.ldf')
FOR ATTACH
Restore or Attach a Windows SQL Server Database Backup on a Linux Server
Next Steps Check out moreSQL Server 2016 tips You can browse the SQL Server Backup tips category to learn more about how to protect your databases. On the following tip you can get a deeper description about attaching a database: Attach Sample Database - Adventureworks in SQL Server 2012 . Learn how to install SQL Server vNext on a Mac: Run SQL Server vNext (CTP1) as a Docker Container on a Mac . Stay tuned for more SQL Server on Linux Tips .

Last Update: 2/9/2017


Restore or Attach a Windows SQL Server Database Backup on a Linux Server
Restore or Attach a Windows SQL Server Database Backup on a Linux Server
About the author
Restore or Attach a Windows SQL Server Database Backup on a Linux Server
Daniel Farina was born in Buenos Aires, Argentina. Self-educated, since childhood he showed a passion for learning. View all my tips

Related Resources

More SQL Server DBA Tips...

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

主题: SQLLinuxWindowsSQL ServerRESTDockerRYRIM
分页:12
转载请注明
本文标题:Restore or Attach a Windows SQL Server Database Backup on a Linux Server
本站链接:http://www.codesec.net/view/531719.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(48)