I am now trying to restore .bak file of MS SQL Server 2008 to docker mssql-serverlinux in order to see the content inside and draw my own scheman in Postgres
I read this and conclude that my command to restore it to my container supposed to be in single line
sqlcmd -S localhost -U SA -Q "RESTORE DATABASE mytrash FROM DISK = '/trash/dbWINS_MPMBA201803141100.bak' WITH FILE = 1, NOUNLOAD, REPLACE, NORECOVERY, STATS = 5"
Here is the full error and directory I place the bak file. Since it will be no use when I finish I name the directory trash in order to delete it later.
root#891ce27ef07a:/trash# /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -Q "RESTORE DATABASE mytrash FROM DISK = '/trash/dbWINS_MPMBA201803141100.bak' WITH FILE = 1, NOUNLOAD, REPLACE, NORECOVERY, STATS = 5"
Password:
Msg 5133, Level 16, State 1, Server 891ce27ef07a, Line 1
Directory lookup for the file "D:\MSSQL\Data\dbWINS_MPMBA.mdf" failed with the operating system error 2(The system cannot find the file specified.).
Msg 3156, Level 16, State 3, Server 891ce27ef07a, Line 1
File 'dbERP_New_Data' cannot be restored to 'D:\MSSQL\Data\dbWINS_MPMBA.mdf'. Use WITH MOVE to identify a valid location for the file.
Msg 5133, Level 16, State 1, Server 891ce27ef07a, Line 1
Directory lookup for the file "D:\MSSQL\Data\dbWINS_MPMBA_1.ldf" failed with the operating system error 2(The system cannot find the file specified.).
Msg 3156, Level 16, State 3, Server 891ce27ef07a, Line 1
File 'dbERP_New_Log' cannot be restored to 'D:\MSSQL\Data\dbWINS_MPMBA_1.ldf'. Use WITH MOVE to identify a valid location for the file.
Msg 3119, Level 16, State 1, Server 891ce27ef07a, Line 1
Problems were identified while planning for the RESTORE statement. Previous messages provide details.
Msg 3013, Level 16, State 1, Server 891ce27ef07a, Line 1
RESTORE DATABASE is terminating abnormally.
root#891ce27ef07a:/trash# pwd
/trash
root#891ce27ef07a:/trash# ls
dbWINS_MPMBA201803141100.bak
root#891ce27ef07a:/trash#
My docker-compose. For any one who would like to show me the answer.
version: "3.5"
services:
mssql:
image: "microsoft/mssql-server-linux"
environment:
SA_PASSWORD: "hardlongpassword"
ACCEPT_EULA: "Y"
volumes:
- type: bind
source: ./data
target: /trash
Update
I tried to move the location according to the TZHX comment from this
I got new error now
1>
1> RESTORE DATABASE NewDB
2> FROM DISK = '/trash/dbWINS_MPMBA201803141100.bak'
3> WITH MOVE 'NewDB' TO '/trash/NewDB.mdf',
4> MOVE 'NewDB' TO '/trash/NewDB_Log.ldf'
5> GO
Msg 3234, Level 16, State 2, Server 891ce27ef07a, Line 1
Logical file 'NewDB' is not part of database 'NewDB'. Use RESTORE FILELISTONLY to list the logical file names.
Msg 3013, Level 16, State 1, Server 891ce27ef07a, Line 1
RESTORE DATABASE is terminating abnormally.
Update2:
From this. I am reading the output
1> RESTORE FILELISTONLY FROM DISK ='/trash/dbWINS_MPMBA201803141100.bak' WITH FILE=1
2> GO
LogicalName PhysicalName Type FileGroupName Size MaxSize FileId CreateLSN DropLSN UniqueId ReadOnlyLSN ReadWriteLSN BackupSizeInBytes SourceBlockSize FileGroupId LogGroupGUID DifferentialBaseLSN DifferentialBaseGUID IsReadOnly IsPresent TDEThumbprint SnapshotUrl
-------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---- -------------------------------------------------------------------------------------------------------------------------------- -------------------- -------------------- -------------------- --------------------------- --------------------------- ------------------------------------ --------------------------- --------------------------- -------------------- --------------- ----------- ------------------------------------ --------------------------- ------------------------------------ ---------- --------- ------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
dbERP_New_Data D:\MSSQL\Data\dbWINS_MPMBA.mdf D PRIMARY 256311296 35184372080640 1 0 0 00000000-0000-0000-0000-000000000000 0 0 143196160 512 1 NULL 73150000001788800289 B582C71F-CCDF-4030-9CF8-30785258D515 0 1 NULL NULL
dbERP_New_Log D:\MSSQL\Data\dbWINS_MPMBA_1.ldf L NULL 2945384448 35184372080640 2 0 0 00000000-0000-0000-0000-000000000000 0 0 0 512 0 NULL 0 00000000-0000-0000-0000-000000000000 0 1 NULL NULL
(2 rows affected)
update3:
1> RESTORE DATABASE dbERP_New_Data
2> FROM DISK = '/trash/dbWINS_MPMBA201803141100.bak'
3> WITH MOVE 'dbERP_New_Data' TO '/trash/dbERP_New_Data.mdf',
4> MOVE 'dbERP_New_Log' TO '/trash/dbERP_New_Log.ldf'
5> GO
Msg 5120, Level 16, State 101, Server 891ce27ef07a, Line 1
Unable to open the physical file "/trash/dbERP_New_Data.mdf". Operating system error 87: "87(The parameter is incorrect.)".
Msg 3013, Level 16, State 1, Server 891ce27ef07a, Line 1
RESTORE DATABASE is terminating abnormally.
Update4:
Apparently, restoring database files to a volume on the host doesn't work (at least not with the default options on a MacOS host; there may be advanced Docker options to get it to work). What will work is restoring the files within the host itself:
RESTORE DATABASE dbERP_New_Data
FROM DISK = '/trash/dbWINS_MPMBA201803141100.bak'
WITH MOVE 'dbERP_New_Data' TO '/root/dbERP_New_Data.mdf',
MOVE 'dbERP_New_Log' TO '/root/dbERP_New_Log.ldf'
Probably not something you should do for a production setup, but good enough if you only need to access the database for a short while.
Related
Returns error below command using SQL Server Management Studio and SQL Server is running on Ubuntu 18.04.
EXEC sp_execute_external_script
#language = N'Python',
#script =
N'import pandas as pd
InputDataSet.to_csv("/home/dataexport/1.csv")',
#input_data_1 = N'select user_id from test.dbo.[users]'
GO
Error:
Msg 39004, Level 16, State 20, Line 11
A 'Python' script error occurred during execution of 'sp_execute_external_script' with HRESULT 0x80004004.
Msg 39019, Level 16, State 2, Line 11
An external script error occurred:
Error in execution. Check the output for more information.
Traceback (most recent call last):
File "<string>", line 5, in <module>
File "/var/opt/mssql-extensibility/data/f131c9cc-88b2-4fb0-9611-311e79fe345b/EC1513EA-1DD0-4174-A820-4652B1F4EF6A/sqlindb_0.py", line 30, in transform
InputDataSet.to_csv("/home/dataexport/1.csv")
File "/opt/mssql/mlservices/runtime/python/lib/python3.7/site-packages/pandas/core/frame.py", line 1745, in to_csv
formatter.save()
File "/opt/mssql/mlservices/runtime/python/lib/python3.7/site-packages/pandas/io/formats/csvs.py", line 156, in save
compression=self.compression)
File "/opt/mssql/mlservices/runtime/python/lib/python3.7/site-packages/pandas/io/common.py", line 400, in _get_handle
f = open(path_or_buf, mode, encoding=encoding)
FileNotFoundError: [Errno 2] No such file or directory: '/home/dataexport/1.csv'
Msg 39019, Level 16, State 2, Line 11
An external script error occurred:
SqlSatelliteCall error: Error in execution. Check the output for more information.
STDOUT message(s) from external script:
SqlSatelliteCall function failed. Please see the console output for more information.
Traceback (most recent call last):
File "/opt/mssql/mlservices/libraries/PythonServer/revoscalepy/computecontext/RxInSqlServer.py", line 605, in rx_sql_satellite_call
rx_native_call("SqlSatelliteCall", params)
File "/opt/mssql/mlservices/libraries/PythonServer/revoscalepy/RxSerializable.py", line 375, in rx_native_call
ret = px_call(functionname, params)
RuntimeError: revoscalepy function failed.
I have given full permissions on the SQL Server folders that are existing on /opt/ and /var/opt but no luck. even I gave Sudo permissions to users that are running SQL Server and LaunchPad service but didn't work. Even i given full permission on folder path /home/dataexport using chmod -R ugo+rwx /home/dataexport
I have to restore a database and am following this official documentation where I follow two steps:
- List the files
- Run the Restore command with respect to the files aforementioned.
However, I am facing "already claimed" error.
I tried to use different names but it is not possible since the backup has certain files. I also tried other answers across different domains, all have GUI.
The first command that I ran was:
sudo docker exec -it sql1 /opt/mssql-tools/bin/sqlcmd -S localhost \
-U SA -P '<YourStrong#Passw0rd>' \
-Q 'RESTORE FILELISTONLY FROM DISK = "/var/opt/mssql/backup/us_national_statistics.bak"' \
| tr -s ' ' | cut -d ' ' -f 1-2
I got the following output:
LogicalName PhysicalName
-------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
us_national_statistics C:\Program
us_national_statistics_log C:\Program
Then, as per the documentation, I ran this command:
sudo docker exec -it sql1 /opt/mssql-tools/bin/sqlcmd \
-S localhost -U SA -P '<YourStrong#Passw0rd>' \
-Q 'RESTORE DATABASE US_NATIONAL FROM DISK = "/var/opt/mssql/backup/us_national_statistics.bak" WITH MOVE "us_national_statistics" TO "C:\Program", MOVE "us_national_statistics_log" TO "C:\Program"'
Here, I get the following error:
Msg 3176, Level 16, State 1, Server 0a6a6aac7476, Line 1
File 'C:\Program\New' is claimed by 'us_national_statistics_log'(2) and 'us_national_statistics'(1). The WITH MOVE clause can be used to relocate one or more files.
Msg 3013, Level 16, State 1, Server 0a6a6aac7476, Line 1
RESTORE DATABASE is terminating abnormally.
I expect the database to be restored.
You can't restore to C:\Program for multiple reasons. That's not a full path (you seem to have lost the string after the first space in Program Files); the data and log can't both be put in the same file; you don't typically have write access to the root of any drive; and C:\ is not valid in Docker or Linux.
You need the LogicalName, but you should not be using the PhysicalName directly, either in the case where you are restoring to Docker or Linux, or in the case where you are restoring a database alongside an existing copy that you want to keep, or in the case where you are restoring a database to a different instance (which will more than likely have a different data folder structure).
Try:
RESTORE DATABASE US_NATIONAL_COPY
FROM DISK = "/var/opt/mssql/backup/us_national_statistics.bak"
WITH REPLACE, RECOVERY,
MOVE "us_national_statistics" TO "/var/opt/mssql/data/usns_copy.mdf",
MOVE "us_national_statistics_log" TO "/var/opt/mssql/data/usns_copy.ldf";
I have a docker file where I import two databases.
FROM mcr.microsoft.com/mssql/server
RUN mkdir -p /var/opt/mssql/backup
COPY clean_SCManager.bak /var/opt/mssql/backup/SCManager.bak
COPY clean_SCServer.bak /var/opt/mssql/backup/SCServer.bak
COPY SCManager-setup.sql /var/opt/mssql/SCManager-setup.sql
COPY startup.sh /var/opt/mssql/startup.sh
ENV MSSQL_SA_PASSWORD=P#ssw0rd
ENV ACCEPT_EULA=Y
RUN chmod +x /var/opt/mssql/startup.sh
CMD /var/opt/mssql/startup.sh
EXPOSE 1433
Now I want to insert some values into one of my databases, for that I am using a SQL file which contains a INSERT INTO command.
USE SCManager;
GO
INSERT INTO epls_dbo.Account("uid", "login", "password_hash", "lastLogin", "failedLogins", "locked", "role", "locale", "accountPenIdUid")
VALUES (4294967306, 'sysadm', '2398ef2774cd07f31037e9347de3e660', '2017-09-12 14:50:35.607', 0, 'False', 'Admin', 'en_US', NULL);
This SQL will be called by a bash script, in this I am using sqlcmd to execute the SQL script.
#/bin/bash
echo "startup.sh is called, starting sqlserver"
/opt/mssql/bin/sqlservr &
echo "start sleeping for 20 seconds"
sleep 20
echo "sleeping done"
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "P#ssw0rd" -Q "RESTORE FILELISTONLY FROM DISK = '/var/opt/mssql/backup/SCServer.bak'"
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "P#ssw0rd" -Q "RESTORE FILELISTONLY FROM DISK = '/var/opt/mssql/backup/SCManager.bak'"
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "P#ssw0rd" -Q "RESTORE DATABASE SCManager FROM DISK = '/var/opt/mssql/backup/SCManager.bak' WITH MOVE 'SCManager' TO '/var/opt/mssql/data/SCManager.mdf', MOVE 'SCManager_log' TO '/var/opt/mssql/data/SCManager_log.ldf'"
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "P#ssw0rd" -Q "RESTORE DATABASE SCServer FROM DISK = '/var/opt/mssql/backup/SCServer.bak' WITH MOVE 'SCServer' TO '/var/opt/mssql/data/SCServer.mdf', MOVE 'SCServer_log' TO '/var/opt/mssql/data/SCServer_log.ldf'"
sleep 10
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -d SCManager -P "P#ssw0rd" -i "/var/opt/mssql/SCManager-setup.sql"
tail -f /dev/null
When I run the docker image I can see in the logs that the database has been imported and that the sql file has been executed but the sql has not been successfully executed
Changed database context to 'SCManager'.
Msg 207, Level 16, State 1, Server fb5db2041be5, Line 4
Invalid column name 'uid'.
Msg 207, Level 16, State 1, Server fb5db2041be5, Line 4
Invalid column name 'login'.
Msg 207, Level 16, State 1, Server fb5db2041be5, Line 4
Invalid column name 'password_hash'.
Msg 207, Level 16, State 1, Server fb5db2041be5, Line 4
Invalid column name 'lastLogin'.
Msg 207, Level 16, State 1, Server fb5db2041be5, Line 4
Invalid column name 'failedLogins'.
Msg 207, Level 16, State 1, Server fb5db2041be5, Line 4
Invalid column name 'locked'.
Msg 207, Level 16, State 1, Server fb5db2041be5, Line 4
Invalid column name 'role'.
Msg 207, Level 16, State 1, Server fb5db2041be5, Line 4
Invalid column name 'locale'.
Msg 207, Level 16, State 1, Server fb5db2041be5, Line 4
Invalid column name 'accountPenIdUid'.
When I connect to the running docker container I can query the database, so the database is available and the table which I want to insert into exits
root#fb5db2041be5:/# /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "P#ssw0rd" -d SCManager -Q "select * from epls_dbo.Account"
uid login password_hash lastLogin failedLogins locked role
locale accountPenIdUid
-------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------- ----------------------- ------------ ------ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------ --------------------
(0 rows affected)
Also I can execute in the bash my insert into command an it inserts correctly the values, why does it not work by calling it from my bash script?
I already have an existing database I am trying to keep up to date on a daily basis. I get a daily dump of sql files. The batch script below created and populated the database the first time I run it, but that doesn't work when I try to update the database with it.
`#echo off
ECHO %USERNAME% started the batch process at %TIME% >output.txt
for %%f in (*.sql) do (
sqlcmd.exe -S servername -E -d DatabaseName -i %%f >>output.txt
)
pause`
Is there a different command for updating a database with sql files?
This is the output i get.
HUTRC started the batch process at 9:55:12.25 Changed database context to 'master'. Msg 15416, Level 16, State 1, Server HUTRC1-HP, Procedure sp_dbcmptlevel, Line 67 Usage: sp_dbcmptlevel [dbname [, compatibilitylevel]] Valid values of the database compatibility level are 100, 110, or 120.
What the sql file looks like. It is very long. I just got the first few lines.
USE [master]
GO
IF NOT EXISTS (SELECT [name] FROM sys.databases WHERE name = N'Migration')
BEGIN
CREATE DATABASE [Migration] COLLATE SQL_Latin1_General_CP1_CI_AS
END
GO
EXEC dbo.sp_dbcmptlevel #dbname=N'Migration', #new_cmptlevel=90
The error message tells you precisely where to look for the problem and what it is (emphasis added):
Procedure sp_dbcmptlevel, Line 67 Usage: sp_dbcmptlevel [dbname [, compatibilitylevel]] Valid values of the database compatibility level are 100, 110, or 120.
Examining your SQL script for sp_dbcmptlevel shows that it uses a different value:
EXEC dbo.sp_dbcmptlevel #dbname=N'Migration', #new_cmptlevel=90
^^
You'll need to either edit the SQL script to a valid compatibility level, or downgrade your server version to the same version as the source server.
I am automating a test environment setup process and below are part of the manual steps involved:
Copy a .bak file from a network drive to my D: drive
Launch SQL Management Studio, use the Restore Files and Filegroups option to restore the .bak file
For point #1, I used:
robocopy \\10.xx.xxx.x\dirA\Build\%myVar%\DB\ D:\ myDB_V%myVar%.bak
It copied just fine!
Now for point 2, I am trying the below command using sqlcmd from command prompt:
sqlcmd -E -S localhost\myInstance -Q "RESTORE DATABASE myDB_V2.2.2.10 FROM DISK='D:\myDB_V2.2.2.10.bak' WITH FILE = 1, MOVE 'myDB_V2.2.2.10' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf', MOVE 'myDB_V2.2.2.10_log' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf';"
It says
Msg 102, Level 15, State 1, Server localhost\myInstance, Line 1
Incorrect syntax near '.2'.
Msg 319, Level 15, State 1, Server localhost\myInstance, Line 1
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
I could not interpret what it is saying.
Can anyone help me please?
Try this:
sqlcmd -E -S localhost\myInstance -Q "RESTORE DATABASE 'myDB_V2.2.2.10' FROM DISK='D:\myDB_V2.2.2.10.bak' WITH FILE = 1, MOVE 'myDB_V2.2.2.10' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf', MOVE 'myDB_V2.2.2.10_log' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf';"
Try this
sqlcmd -E -S localhost\myInstance -Q "RESTORE DATABASE [myDB_V2.2.2.10] FROM DISK='D:\myDB_V2.2.2.10.bak' WITH FILE = 1, MOVE 'myDB_V2.2.2.10' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf', MOVE 'myDB_V2.2.2.10_log' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf';"
It looks like the error is about your database name, "RESTORE DATABASE myDB_V2**.2**.2.10 [...]". I would suggest not using periods in your database name as periods are used to separate database objects in queries. If you are already using periods, try wrapping the database name in square brackets [].