Backup SQL Server 2019 in a Docker Linux container fails - sql-server

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;

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.

MS SQL Server - Create schema dump without FILENAME property

I would like to create an SQL Server schema dump from a local server that I can run from within an sql server docker container to create an empty database ready for integration tests.
I generate the scripts via SQL Server Management Studio. I right click the database > Tasks > Generate script and set Types of data to script to "Schema only".
I copy the sql files across as part of my docker-compose.yml and when I run the sql dump from within the docker container I get
Directory lookup for the file
"C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\mydatabase.mdf"
failed with the operating system error 2(The system cannot find the file specified.).
the part of the script that's causing the above is...
CREATE DATABASE [mydatabase]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'mydatabase', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\mydatabase.mdf' , SIZE = 8192KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB )
LOG ON
( NAME = N'mydatabase_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\mydatabase_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )
WITH CATALOG_COLLATION = DATABASE_DEFAULT
GO
If I go through the same generation process from an azure hosted database the create code generated is...
CREATE DATABASE [mydatabase]
GO
and if I manually run the above from within the container, it creates as expected.
So what do I need to do to generate an SQL schema dump that doesn't include the FILENAME property of the create command?
Following #Jeroen Mostert's suggestion I generate the scripts without the CREATE DATABASE part, this is done by selecting "specific database objects" instead of "entire database and all database objects"
I then run a script I manually wrote that creates all the required databases, followed by running the scripts that create the tables and procedures against each database.
docker-compose.yml
version: "3.8"
services:
db:
image: mcr.microsoft.com/mssql/server:2019-latest
environment:
- ACCEPT_EULA=true
- SA_PASSWORD=localdevpassword#123
volumes:
- ./sql:/scripts/
ports:
- "1434:1433"
command: /bin/bash -c /scripts/init.sh
init.sh
#!/bin/bash
echo "Starting SQL server"
/opt/mssql/bin/sqlservr &
echo "Sleeping for 10 seconds"
sleep 10
echo "Done sleeping, run the SQL scripts"
cd /scripts/
echo "Creating databases using file databases.sql"
/opt/mssql-tools/bin/sqlcmd -U sa -P $SA_PASSWORD -l 30 -e -i databases.sql
for FILENAME in *.sql; do
DATABASE="${FILENAME%.*}"
if [ "$DATABASE" != "databases" ]; then
echo "Running import for $DATABASE using file $FILENAME"
/opt/mssql-tools/bin/sqlcmd -U sa -P $SA_PASSWORD -d $DATABASE -l 30 -e -i $FILENAME
fi
done
echo "SQL scripts run, sleep forever to keep container running"
sleep infinity

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

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

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