Writing to disk on mac/linux using SQL Server Command Line tools - sql-server

My objective is to backup a remote SQL Server database using SQL Server command line tools for Mac (or Linux). After installing the sqlcmd utility, I am able to connect to the database but am not sure how to handle the TO DISK command, whose default is to assume a Windows operating system.
sqlcmd -S myserver.com -U AIS -Q "BACKUP DATABASE mydb TO DISK = N'/mybackups/mydb.bak' WITH NOFORMAT, NOINIT, NAME = 'mydb', SKIP, NOREWIND, NOUNLOAD, STATS = 10"
This command returns the error:
Cannot open backup device 'D:\BACKUP\mybackups\mydb.bak'. Operating system error 3 (The system cannot find the path specified).
I have tried changing access permissions of the folder mybackups using chmod 777 mybackups but the same error is still thrown. Any idea about how to solve this?

Related

Backup SQL Server database on local PC

I am using this script to backup my SQL Server database on computer A:
sqlcmd -U MyDatabase -P MyPassword -S .\SQLEXPRESS -Q "EXEC sp_BackupDatabases #backupLocation = 'c:\shared', #databaseName='MyDatabase', #BackupType='F'"
Everything works fine, but I decided to store backup files to computer B. I shared folder on computer B, which is accessible from:
\\computerB\shared
I changed backup script to include new backup location:
sqlcmd -U MyDatabase -P MyPassword -S .\SQLEXPRESS -Q "EXEC sp_BackupDatabases #backupLocation = '\\computerB\shared', #databaseName='MyDatabase', #BackupType='F'"
But when I run this, I get this error:
Cannot open backup device '\computerB\shared\MyDatabase_FULL_101 22022_232734.BAK'. Operating system error 5 (Access denied
.).
Msg 3013, Level 16, State 1, Server WIN-C28934ASNF1\SQLEXPRESS, Line 1
BACKUP DATABASE is terminating abnormally.
I don't understand why am I getting Access denied. I can easily access \\computerB\shared from file explorer on computer A. What is wrong here?
We have to think carefully about the user account running the backup process.
When you access the \\computerB\shared location in Windows File Explorer, you use your own user account. When you run the sqlcmd script it's probably also with your own user account. However, the script is telling SQL Server to perform the backup action; it does not perform the backup directly on it's own. SQL Server is running using a different account. This different account does not have access to the shared folder.
To fix this, you can change the account used to run SQL Server, or you can let SQL Server backup locally and then copy or more the files to the share after the backup completes.
What you will probably NOT be able to do is grant SQL Server's current service account access to the shared folder, because it is most likely a special account like SYSTEM (hopefully not) or Network Service that can't be given this access.

Connect to sql server and do bulk load of data file from Linux server

Problem Statement:
I am trying to Load data file from Linux- source to SQL server table- target (sql server installed Windows server).
After many meetings with Microsoft we got resolve the connection issue.
Connection string:
[]$kinit windowsserverusername (Windows server AD Account) :- After this command system will prompt for password
[]$ sqlcmd -S servername,port -E -C :- After this command system will connect to sql server with ">"
BULK INSERT SQLSCHEMA.dbo.TABLENAME FROM '**/SOURCE/DataFile/INPUT_DATA.txt**' WITH (FIELDTERMINATOR=',',ROWTERMINATOR='\n');
go
And I got the following error:
Msg 4861, Level 16, State 1, Server SERVERNAME, Line 2
Cannot bulk load because the file '/SOURCE/DataFile/INPUT_DATA.txt' could not be opened. Operating system error code 3(The system cannot find the path specified.).
Issue:
Unable to use fetch the source data file path in sql statement. How to use the Linux server data file path in sql statement or in .sql file.
I have save the BULK load sql statement into .sql file and try to run the command & got same above error.
[]$ sqlcmd -S servername,port -E -C -i INPUTFILE.sql
How to use source file Linux path in sql server sql prompt or in .sql file ??
I have tried .py script by following connection string and we got trusted connection/ certificate issue and Microsoft suggested above sqlcmd method.
"pyodbc.connect('DRIVER={ODBC Driver 18 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)"
``

How to back up sql server 2014 db data via bat file?

I am using this query in bat file to back-up db,
sqlcmd -S .\MSSQLSERVER -E -Q "EXEC sp_BackupDb #backupLocation='D:\SQLBackups\', #databaseName='TestDb', #backupType='F'" pause
It is throwing this error,
Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : SQL Server
Network Inte rfaces: Connection string is not valid [87]. Sqlcmd:
Error: Microsoft ODBC Driver 11 for SQL Server : Login timeout
expired. Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : A
network-related or in stance-specific error has occurred while
establishing a connection to SQL Server . Server is not found or not
accessible. Check if instance name is correct and i f SQL Server is
configured to allow remote connections. For more information see SQL
Server Books Online..
Can someone please help? Thanks in advance.
you can use SQLCMD in a .bat file to make this bakcup, in this link you have an example stackoverflow link
SQLCMD:
The sqlcmd utility lets you enter Transact-SQL statements, system procedures, and script files at the command prompt, in Query Editor in SQLCMD mode, in a Windows script file or in an operating system (Cmd.exe) job step of a SQL Server Agent job. This utility uses OLE DB to execute Transact-SQL batches.
here is the link for examples and definition: msdn link definition
for example:
sqlcmd -E -S %SERVERNAME% -d master -Q "BACKUP DATABASE [%DATABASENAME%] TO DISK = N'%BACKUPFILENAME%' WITH INIT , NOUNLOAD , NAME = N'%DATABASENAME% backup', NOSKIP , STATS = 10, NOFORMAT"
hope this help!

Back up of SQL server database using command line

I am trying to create a back up for an SQL Server database using command line.I have used the following command:
C:\>SqlCmd -E -S DEVE-PC3/SQLEXPRESS -Q "BACKUP DATABASE Exam_Db To Disk='d:\Exam_db.bak'"
But I am getting an error message
Msg 3201,cannot open backup device Operating System Error 5.
Msg 3013,Backup database is terminating abnormally.
What am I missing?
That error message tells you that whatever account is running the SQL Server service doesn't have permission to write to the location that you've specified. Either choose another location or give the service account permission.

Maintenance Plan Fails But Query Runs

I have an SQL Server 2005 Enterprise Edition whose Maintenance plan fails constantly with the error:
backup MYSERVER (MYSERVER)
Backup Database on MYSERVER
Databases that have a compatibility level of 70 (SQL Server version 7.0) will be skipped.
Databases: All databases
Type: Differential
Append existing
Task start: 2011-10-18T00:10:09.
Task end: 2011-10-18T00:10:09.
Failed:(-1073548784) Executing the query "BACKUP DATABASE [model] TO DISK = N'\\myNetworkDrive\\opovo\\BackupSQL\\MYSERVER\\model\\model_backup_201110180010.bkp' WITH DIFFERENTIAL , RETAINDAYS = 13, NOFORMAT, NOINIT, NAME = N'model_backup_20111018001008', SKIP, REWIND, NOUNLOAD, STATS = 10
" failed with the following error: "Cannot open backup device 'C:\\Program Files\\Microsoft SQL Server\\MSSQL.1\\MSSQL\\Backup\\Arca\\opovo\\BackupSQL\\MYSERVER\\model\\model_backup_201110180010.bkp'. Operating system error 3(The system cannot find the path specified.).
BACKUP DATABASE is terminating abnormally.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
But the query:
BACKUP DATABASE [model] TO DISK = N'\\myNetworkDrive\\opovo\\BackupSQL\\MYSERVER\\model\\model_backup_201110180010.bkp' WITH DIFFERENTIAL , RETAINDAYS = 13, NOFORMAT, NOINIT, NAME = N'model_backup_20111018001008', SKIP, REWIND, NOUNLOAD, STATS = 10
runs normally and gives me the expected results.
Is this a bug?
What am I missing here?
What is the elegant way to backup to a network location?
You need to check the account that SQL Server Agent service runs as. If the account doesn't have permissions on that network share, then it won't be able to see that path. Executing that query outside of a SQL Server Agent job (indirectly through a maintenance plan) doesn't use the security context of the SQL Server Agent service.
Make the SQL Server Agent service run as a domain account with access to that network share.

Resources