We back up our SQL Server databases to NAS, and was curious if it would be possible to back up to a temporary mapped drive, as opposed to the UNC path. I'm hitting a wall.
Using New-PSDrive:
New-PSDrive -Name "Y" -PSProvider FileSystem -Root "\\NAS\Backups" -Scope Global -Persist
Test-Path "Y:\SQLDBA" <# returns True #>
<# Method 1 #>
sqlcmd -E -d master -Q "BACKUP DATABASE [SQLDBA] TO DISK = N'Y:\SQLDBA\SQLDBA.bak' WITH INIT" -b
<# Method 2 #>
Invoke-Sqlcmd -Database master -Query "BACKUP DATABASE [SQLDBA] TO DISK = N'Y:\SQLDBA\SQLDBA.bak' WITH INIT" -QueryTimeout 999999999 -verbose -ErrorAction Stop
Remove-PSDrive "Y"
Using net use:
net use Y: \\NAS\Backups /persistent:yes
<# Method 1 #>
sqlcmd -E -d master -Q "BACKUP DATABASE [SQLDBA] TO DISK = N'Y:\SQLDBA\SQLDBA.bak' WITH INIT" -b
<# Method 2 #>
Invoke-Sqlcmd -Database master -Query "BACKUP DATABASE [SQLDBA] TO DISK = N'Y:\SQLDBA\SQLDBA.bak' WITH INIT" -QueryTimeout 999999999 -verbose -ErrorAction Stop
net use Y: /delete
Either method gets me the same error:
Cannot open backup device 'Y:\SQLDBA\SQLDBA.bak'. Operating system error 3(The system cannot find the path specified.).
Is this possible?
No, it is not. Mapped drives exist in a Session. The server process (of sql server) has it's own session, so it does not see your mapped drive.
To back up to a network drive when SQL Server is running in a domain user account, the shared drive must be mapped as a network drive in the session where SQL Server is running. If you start Sqlservr.exe from command line, SQL Server sees any network drives you have mapped in your login session.
Please check the following article for reference:
http://msdn.microsoft.com/en-us/library/ms179313.aspx#NetworkShare
Related
I have an issue with deleting snapshot job. The SQL Server job history shows:
Message Executed as user: NT Service\SQLSERVERAGENT. The step did not generate any output. Process Exit Code 255. The step failed.
NT Service\SQLSERVERAGENT account has "full control" permissions to whole snapshot path.
My job has one step:
Get-ChildItem -Path G:\Data\snapshot\unc\SNAPSHOT_PUBLICATION -Include *.* -Recurse | foreach { $_.Delete()}
get-Date >> g:\data\snapshot\unc\snapshot-cleaner.log
Any ideas why this action fails every time?
I'm running SQL Server in a Linux container. When the container starts, I'd like some initialization and create a database using Powershell Core.
My Dockerfile looks like this:
FROM mcr.microsoft.com/mssql/server:2017-latest
ENV ACCEPT_EULA="Y" `
DATA_PATH="./data" `
sa_password="MyP#ssw0rd"
VOLUME ${DATA_PATH}
WORKDIR ./init
RUN apt-get update
RUN apt-get install -y powershell
COPY SomeFolder/Initialize-Database.ps1 .
CMD pwsh ./Initialize-Database.ps1 -sa_password $env:sa_password -data_path $env:DATA_PATH -Verbose
In the Powershell script I'm installing the SQLServer module in order to perform SQL queries.
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
Install-Module SQLServer
if ($sa_password -ne "_") {
Write-Verbose 'Changing SA login credentials'
$sqlcmd = "CREATE DATABASE my-db"
Invoke-SqlCmd -Query $sqlcmd -ServerInstance "."
}
The Invoke-SqlCmd command fails with a connection error:
A network-related or instance-specific error occurred while
| establishing a connection to SQL Server. The server was not
| found or was not accessible. Verify that the instance name is
| correct and that SQL Server is configured to allow remote
| connections. (provider: TCP Provider, error: 40 - Could not
| open a connection to SQL Server)
How do I check if the SQL instance is started and if not, how can I start it?
When I start a container using this image and exec into the container, Get-SqlInstance -ServerInstance "." -Credential Get-Credential fails with the error 'Failed to connect to server'.
Found it. In the Dockerfile, I'm now calling the Powershell script after SQL has started:
CMD ( /opt/mssql/bin/sqlservr & ) | grep -q "Service Broker manager has started" \
&& pwsh ./Initialize-Database.ps1 -sa_password $SA_PASSWORD -data_path $DATA_PATH -Verbose
https://www.kenmuse.com/blog/devops-sql-server-dacpac-docker
I have a problem setting the BackupFile-attribute on the Backup-SqlDatabase command (in Powershell for Linux). It expects a Windows path.
What I did:
I've installed PowerShell on a centos Linux. I started Powershell with pwsh and installed the SqlServer module. I store my db-credentials in $cred.
I then run:
PS /> Backup-SqlDatabase -ServerInstance "-----" -Database "MyDatabase" -BackupFile "~/db-backup.bak" -Credential $cred
The output is:
Backup-SqlDatabase: System.Data.SqlClient.SqlError: Cannot open backup device 'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Backup\~\db-backup.bak'. Operating system error 3(The system cannot find the path specified.).
If I omit the BackupFile attribute, it'll actually do a backup - but I have no idea, where it stored the .bak-file (if it even did?)
So, how can I make the BackupFile-attribute accept a Linux path?
I want to connect two VM/server one has data file and batch file another have sql install wanted to trigger command of server A which will hit server b files
Sql command--- in server A
EXEC master..xp_cmdshell 'cd.. && "C:\Program Files\Powershell\6\pwsh.exe" -File "C:\Users\sprasad\Desktop\script\command1.ps1"'
error are
1- import-module: The specified module 'C:\Program
Files\Derivation_19_01_rev0\Core.PowershellModule.TradeLoader.dll' was
not loaded because no valid module file was found in any module
directory.
because files are in server B
I am running sql scripts stored in files (sqlscript.sql) on ServerA against a SQL installation on ServerB using remote powershell. The Powershel module SQLPS is needed on serverA.
see link
steps:
enable remotepowershell on serverB
Enable-PSRemoting -force
in your SQL server, add the user that executes the script on serverA in the list of users with rights on the database.
use a script on severA like:
Import-Module -Name SQLPS -NoClobber -DisableNameChecking -Scope Local Invoke-Sqlcmd -ServerInstance serverA -InputFile sqlscript.sql
-Verbose
Invoke-Sqlcmd -ServerInstance 'serverB' -InputFile sqlscript.sql -Verbose
I am using the following command in my MSBuild file to drop a database
sqlcmd -E -S <ServerName> -Q "DROP DATABASE <DBName>"
But sometimes I get the error
Cannot drop database because it is currently in use.
What command should I use so that the DB is dropped even if it is in use?
You can set your database to SINGLE_USER to drop all existing connections.
sqlcmd -E -S <ServerName> -Q "ALTER DATABASE <DBName> SET SINGLE_USER WITH ROLLBACK IMMEDIATE"
sqlcmd -E -S <ServerName> -Q "DROP DATABASE <DBName>"
This is preferred to looping over each of the connections and dropping them one by one, because some applications will immediately reconnect.
To kill all connections to your database. See Kill All Active Connections To A Database
Now issue your DROP DATABASE command after the above procedure.
Please use something like this:
sqlcmd -S servername\instance -Q"use master;DROP DATABASE yourdatabase"
Try this:
sqlcmd -S .\MAPS -Q "RESTORE DATABASE Awards FROM DISK = 'C:\Awards_Project
\Awards_FULL.bak' WITH REPLACE, MOVE 'Awards' to 'C:\Awards_Project\Awards.mdf',
MOVE 'Awards_log' to 'C:\Awards_Project\Awards.ldf'"
You could alternatively restart the sql service and then perform the drop e.g. in a myfile.bat
#echo off
REM Requires administrative rights to restart service
REM open powershell and execute command in administrative mode
powershell -Command "Start-Process 'cmd' -Verb RunAs -ArgumentList '/c net stop MSSQLSERVER && net start MSSQLSERVER && exit'"
echo Please wait ~10 seconds for sql service to restart.
Timeout /t 10 /nobreak
echo Dropping database.
sqlcmd -S localhost -Q "drop database [database Name]"
You cannot drop a database that is in use. Check link for ref