Unable to restore SQL Server database in Github Codespace - sql-server

I have the following script with its corresponding backup file
USE [master]
RESTORE DATABASE [Imports]
FROM DISK = '/workspaces/Imports/.devcontainer/mssql/Imports.bak'
WITH MOVE 'Imports' TO '/var/opt/mssql/data/Imports.mdf',
MOVE 'Imports_log' TO '/var/opt/mssql/data/Imports_log.ldf',
FILE = 1, NOUNLOAD, STATS = 5
GO
In command line I execute this line
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P MSSqlPasswd -d master -i /workspaces/Imports.devcontainer/mssql/restoreDataBases.sql
but I get this error
Changed database context to 'master'.
Msg 3201, Level 16, State 2, Server 8738a617a215, Line 2
Cannot open backup device '/workspaces/Imports/.devcontainer/mssql/Importaciones.bak'. Operating system error 2
(The system cannot find the file specified.).
Msg 3013, Level 16, State 1, Server 8738a617a215, Line 2
RESTORE DATABASE is terminating abnormally.

Related

Backup SQL Server 2019 in a Docker Linux container fails

Attempted multiple ways to do the backup.
Tried adding SA as a root user in the container
Azure Data studio
BACKUP DATABASE [PrestigeCars] TO DISK = N'/var/opt/mssql/backup//PrestigeCars-202044-8-4-52.bak' WITH NOFORMAT, NOINIT, NAME = N'PrestigeCars--2020-04-04T12:04:52', NOSKIP, REWIND, NOUNLOAD, STATS = 10
Msg 3201, Level 16, State 1, Line 1
Cannot open backup device '/var/opt/mssql/backup//PrestigeCars-202044-8-4-52.bak'. Operating system error 2(The system cannot find the file specified.).
Msg 3013, Level 16, State 1, Line 1
BACKUP DATABASE is terminating abnormally.
Total execution time: 00:00:00.217
SSMS
backup database [PrestigeCars]
to disk = N'/var/opt/mssql/backup//PrestigeCars-202044-6-42-9.bak'
with noformat
, noinit
, name = N'PrestigeCars--20200404'
, noskip
, rewind
, nounload
, compression
, stats = 10;
SQLCMD
sqlcmd -S localhost,12001 -U SA -Q "BACKUP DATABASE [PrestigeCars] TO DISK = N'/var/opt/mssql/backup/CSCI331-Backup/PrestigeCars-202044-6-42-9.bak' WITH NOFORMAT, NOINIT, NAME = 'PrestigeCars-20200404', SKIP, NOREWIND, NOUNLOAD, STATS = 10"
Thanks to all of those of you that have helped. The issue was not missing folders but a permissions issue.
The folders were initially create when copying backups to the container:
docker cp /Users/YourUsername/CSCI331-Backup/TSQLV4.bak linux-sql2k19:/var/opt/mssql/backup/
*The permissions were 4 drwxr-xr-**x 3 root root 4096 Jan 31 19:06 backup***
I tried various Ubuntu create sudo adduser but none of the commands sudo or apt-get worked. (https://help.ubuntu.com/community/FilePermissions)
I found this command to connect as the root user
docker exec -it -u root 874 bash
cd /var/opt/mssql/backup/
cd ..
chmod 777 backup
cd ..
chmod 777 mssql
cd ..
chmod 777 opt
cd ..
chmod 777 var
Close the container. I backed up databases in SSMS and Azure Data Studio. Double Yeah!
Directory /var/opt/mssql/backup does not exist in standard SQL Server Linux images. You'll need to first create the directory by running the following command in the container:
mkdir /var/opt/mssql/backup
Also, as #Larnu pointed out, you have an extra backslash in the path. The backup command should be:
BACKUP DATABASE [PrestigeCars] TO DISK = N'/var/opt/mssql/backup/PrestigeCars-202044-8-4-52.bak' WITH NOFORMAT, NOINIT, NAME = N'PrestigeCars--2020-04-04T12:04:52', NOSKIP, REWIND, NOUNLOAD, STATS = 10;

Cannot open backup device '/var/opt/mssql/backup/MyDB.bak'. Operating system error 5(Access is denied.)

I found a lot of cases, where people try to open a file from outside the docker container. But mine is clearly inside.
The whole message I am getting is:
$ /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "mypw" -Q
"RESTORE FILELISTONLY FROM DISK = '/var/opt/mssql/backup/MyDB.bak'"
Msg 3201, Level 16, State 2, Server 1c7bf85afdaf, Line 1
Cannot open backup device '/var/opt/mssql/backup/MyDB.bak'. Operating system error 5(Access is denied.).
Msg 3013, Level 16, State 1, Server 1c7bf85afdaf, Line 1
RESTORE FILELIST is terminating abnormally.
Same happens when I try to actually restore the database. Any idea what is going wrong?
I have the feeling it might be some permission things where need to change permissions for the file
$ ls -la var/opt/mssql/backup/MyDB.bak
-rw-r----- 1 501 dialout 3395584 Jan 16 02:12 var/opt/mssql/backup/MyDB.bak
The solution was to change the permission of the file.
I needed to do:
docker exec -it -u root MicrosoftSQLServer "bash"
Then change the user (whoami returns mssql when I run the docker container not as root)
chown mssql /var/opt/mssql/backup/TestDB.bak
And then it works.
The answer of Juliano works for me

How make sqlcmd hold file access rights on linux?

Just installed sql-server on Ubuntu 16.04, 2 days ago. Using sqlcmd for bulk insert i got:
Msg 4860, Level 16, State 1, Line 6 Cannot bulk load. The file
"~/test_data.txt" does not exist or you don't have file access rights.
Yes the file did exist, i made sure of it using the command cat.
Then i tried bcp tool, but i got:
SQLState = S1000, NativeError = 0 Error = [Microsoft][ODBC Driver 13
for SQL Server]Unable to open BCP host data-file
Also tried installing visual studio code and adding mssql extension but i got the same "file access rights" warning. And already used the chmod 777 trying to fix it. Didn't work.
Command bulk insert sqlcmd:
BULK INSERT TestEmployees FROM '~/test_data.txt'
WITH(
rowterminator = ','
);
Command on bcp tool
bcp auth in path/auth2.tsv -S localhost -U sa -P <my password> -d Trabalho1BD -c
I think your problem is the '~/test_data.txt' part of your bulk insert command. Specifically, that says "find a file called test_data.txt in the home directory". But whose home directory? Not yours! It's looking for the file in the home directory of the account that's running your SQL Server. Try changing that to a full path (i.e. '/home/«username»/test_data.txt' and that should do it.

AWS RDS SQL Server RESTORE ISSUE

I created SQL Server res instance on AWS. I want to restore the database from a .bak file. I performed below steps in order to restore:
sqlcmd -S XXX.rds.amazonaws.com -U root -P XXXX
USE master
go
create database xyzzy
RESTORE DATABASE xyzzy
FROM DISK = N'C:\Users\Administrator\Downloads\FMAT_CPS_backup_2017_09_29_040005_9422965.bak'
go
but I got this error
Msg 3110, Level 14, State 1, Server EC2AMAZ-BUJEKP3, Line 1
User does not have permission to RESTORE database 'catalyst'.
Msg 3013, Level 16, State 1, Server EC2AMAZ-BUJEKP3, Line 1
RESTORE DATABASE is terminating abnormally.
When I tried this command
C:\Users\Administrator>sqlcmd -e -S xxxxrds.amazonaws.com -U root -P xxxx -i "C:\Users\Administrator\Downloads\FMAT_CPS_backup_2017_09_29_040005_9422965.bak"
I got another error:
Sqlcmd: Error: Syntax error at line 224 near command 'S' in file 'C:\Users\Administrator\Downloads\FMAT_CPS_backup_2017_09_29_040005_9422965.bak'.
The .bak file is of 25GB size, I'm not able to open it.
How to resolve these errors?
You might have in-sufficient Permission do you proper IAM Credentials.
Alternatively
You can upload the file to S3 and Restore to RDS

SQL Server docker container with NFS mount as volume mapping

I am trying to run SQL Server on ubuntu as a docker container using stranded microsoft/mssql-server-linux:latest docker image. For the data persistence I am mapping a volume from my host (having ext4 file system) to container using -v option in docker run command like below
docker run -it -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=######' -p 1433:1433
-v /var/opt/database:/var/opt/mssql/data microsoft/mssql-server-linux
Container is running just fine and i am able to connect to the database. But I do not want to store data on my host because of the growing size of the database and for swarm reasons. Now I thought to run a NFS server on some other machine and mount the nfs directory onto my docker host and then use that nfs dir on the volume mapping now the volume mapping is
-v /var/nfs/database:/var/opt/mssql/data
where /var/nfs/database is a nfs mount dir on the docker host. Now when I start the container I get the following error
2017-08-18 10:15:53.98 spid5s FCB::Open failed: Could not open file /var/opt/mssql/data/master.mdf for file number 1. OS error: 87(The parameter is incorrect.).
2017-08-18 10:15:53.98 spid5s Error: 5120, Severity: 16, State: 101.
2017-08-18 10:15:53.98 spid5s Unable to open the physical file "/var/opt/mssql/data/master.mdf". Operating system error 87: "87(The parameter is incorrect.)".
2017-08-18 10:15:54.13 spid5s Error: 17204, Severity: 16, State: 1.
2017-08-18 10:15:54.13 spid5s FCB::Open failed: Could not open file /var/opt/mssql/data/mastlog.ldf for file number 2. OS error: 87(The parameter is incorrect.).
2017-08-18 10:15:54.13 spid5s Error: 5120, Severity: 16, State: 101.
2017-08-18 10:15:54.13 spid5s Unable to open the physical file "/var/opt/mssql/data/mastlog.ldf". Operating system error 87: "87(The parameter is incorrect.)".
I have given all the permissions to database files. Below is the nfs mount snippet
sharedstorageIp:/var/nfs nfs4 443G 47G 375G 11% /var/nfs
The release notes explain that you can't do this:
Hosting database files on a NFS server is not supported in this
release. This includes using NFS for shared disk failover clustering
as well as databases on non-clustered instances. We are working on
enabling NFS server support in the upcoming releases.
Hosting database files on a networked path is not trivial, given the demands the database has for reliability. Hosting files on SMB shares wasn't possible until version 2.2 of the protocol.

Resources