AWS RDS SQL Server RESTORE ISSUE - sql-server

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

Related

Unable to restore SQL Server database in Github Codespace

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.

How to change file ownership of DB backup in SQL Server container

I'm following this guide to restore a database backup
https://learn.microsoft.com/en-us/sql/linux/tutorial-restore-backup-in-sql-server-container?view=sql-server-ver15
I used docker cp command to copy the DB backup files to the container
docker exec -it SQLContainer mkdir /var/opt/mssql/backup
docker cp MyDb.bak SQLContainer:/var/opt/mssql/backup/
However when trying to restore the DB by running the following query in SSMS, an error message is shown
RESTORE DATABASE MyDB FROM DISK='/var/opt/mssql/backup/MyDB.bak'
Operating system error 5(Access is denied.).
I tried copying using docker cp -a, which sets file ownership to same as destination, but I got this error.
docker cp -a MyDb.bak SQLContainer:/var/opt/mssql/backup/
Error response from daemon: getent unable to find entry "mssql" in passwd database
I'm using Microsoft's image and I don't know the password for root user, the container runs using mssql user, so chown doesn't work either. How can I change the file permissions so DB restore works?
Turns out when I copied the database backup files from the Windows host to the Ubuntu machine, the files were owned by root user and all other users didn't have read permission.
Adding read permission to the file before copying to the docker container works and the server was able to read the files.
sudo chmod a+r MyDb.bak
sudo docker cp MyDb.bak SQLContainer:/var/opt/mssql/backup/
I had a similar issue just today, about the same time as this incident, just attaching MDF and LDF files.
By doing the chmod go+w before copying files without the -a I was able to get SQL Server to treat them as writeable. Prior to that I was getting error messages whenever any update was performed.

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.

Restore SQL Server database to Linux Docker

I need to restore a large SQL Server database on a Linux Docker instance (https://hub.docker.com/r/microsoft/mssql-server-linux/)
I'm moving my .bak file to the docker and executing this command in mssql shell:
RESTORE DATABASE gIMM_Brag FROM DISK = '/var/opt/mssql/backup/BackupFull8H_gIMM.bak' WITH MOVE '[gIMM].Data' T'/var/opt/mssql/data/gIMM.mdf', MOVE '[gIMM].Log' TO '/var/opt/mssql/data/gIMM.ldf', MOVE 'TraceabilityData' TO '/var/opt/mssql/data/gIMM.TraceData.mdf', MOVE 'TraceabilityIndexes' TO '/var/opt/mssql/data/gIMM.TraceIndex.mdf', MOVE 'KpiData' TO '/var/opt/mssql/data/gIMM.KpiData.mdf', MOVE 'KpiIndexes' TO '/var/opt/mssql/data/gIMM.KpiIndex.mdf'
I'm mapping correctly every file that need to and I definitely have enough space on the docker instance but I'm getting this error:
Error: The backup or restore was aborted.
The same error occurs with a windows version of this docker actually... And as it's not supposed to be a Express version, the database size shouldn't be the issue here.
If anyone has more information about what is causing this error !
Thanks,
#TOUDIdel
You have to use the actual file system paths on linux rather than the virtual paths that are shown in the error.
RESTORE DATABASE Northwind FROM DISK='/var/opt/mssql/Northwind.bak' WITH MOVE 'Northwind' TO '/var/opt/mssql/data/NORTHWND.MDF', MOVE 'Northwind_log' TO '/var/opt/mssql/data/NORTHWND_log.ldf'
http://www.raditha.com/blog/archives/restoring-a-database-on-ms-sql-server-for-linux-docker/
You didn't mention it, but the thing that tricked me up was that I wasn't copying the BAK file to my Docker instance. In Terminal with docker and your mssql container running...
1) get container ID:
$docker inspect -f '{{.Id}}' <container_name>
2) copy BAK file to docker instance:
docker exec -i <container_id> bash -c 'cat > /var/opt/mssql/backup.bak' < '/source/path/backup.bak'
3) log into mssql:
mssql -u sa -p 'myPassword'
3) restore db: (you can replace this with your restore script, though this was sufficient for me)
RESTORE DATABASE [MyDatabase] FROM DISK = N'/var/opt/mssql/backup.bak' WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
When I had this problem, it's because the restore command was taking long enough for mssql to time out (with a totally unhelpful error message). Specifying a long timeout when connecting allowed the restore to complete. eg
mssql -s localhost -p "<sa_password>" -t 36000000 -T 36000000
I am not sure it is worth mentioning, but neither of the answers alone worked when moving a .bak made in Windows server to the docker running in Linux version.
(Note that I am using the code from the two previous answers and thus any credit should go to the below-mentioned authors)
TabsNotSpaces' solution was good until step 3 where the restore crashed with path mismatch (C:/path_to_mssql_server could not be found).
Vinicius Krauspenhar's answer was then necessary to remap the MDF and LOG files to fully complete the backup.
Thus the solution that worked for me when importing a windows-server-made .bak file into the Linux docker instance was:
In Terminal with docker and your SQL Server container running...
1) get container ID:
$docker inspect -f '{{.Id}}' <container_name>
2) copy BAK file to docker instance:
docker exec -i <container_id> bash -c 'cat > /var/opt/mssql/backup.bak' < '/source/path/backup.bak'
3) log into mssql or in any DB software and
RESTORE DATABASE Northwind FROM DISK='/var/opt/mssql/Northwind.bak' WITH MOVE 'Northwind' TO '/var/opt/mssql/data/NORTHWND.MDF', MOVE 'Northwind_log' TO '/var/opt/mssql/data/NORTHWND_log.ldf'

Resources