Backup SQL Server database on local PC - sql-server

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.

Related

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

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?

Using PsEXEC via SQL Server agent job

I have a need to add a step (a batch file that needs to run on the application server) once my backup from db server is complete. I am thinking of using the PsEXEC. For whatever reason the command I am testing via the query window executes the batch file at the app server just fine but when I add the same command as step in SQL server agent job it is giving me the error "The process could not be created for step 1 of job 0x318474904D93B54A81BA8B1AEE891A13 (reason: The system cannot find the file specified). The step failed."
Here is the command line running just fine from the query window
EXEC XP_CMDShell 'psexec -accepteula -u domain\user -p psswordhere \appserverip -s -d cmd.exe /c "F:\MovefFilesTest\TheJob\MoveTest.bat"'
FYI: domain\user is local admin on both servers and the sql service and agent are running under this account. It is also a sysadmin on the sql server itself.
Your help is appreciated.
Also F:\ is a local drive on the app server. I am puzzled why it is working on the query window but not when added as agent job

Running SQLCMD in a Batch Script for local .sql file on local SQLSERVEREXPRESS2014 throwing 'Access Denied' error

I have a created a .sql file that will update a table in a data warehouse I am creating using SQL Server 2014 Express. Since Express does not support scheduling jobs, I am attempting to use a batch script scheduled in task scheduler to accomplish this.
ECHO OFF
ECHO This is running to update the [Table Name] Table in the SQL Server Database
PAUSE
sqlcmd -S [ComputerName]\SQLEXPRESS2014 -U myusername -P mypassword -i "My Path With Spaces" of [MyQuery].sql
ECHO Query Worked!
PAUSE
However, when I run it (even as Administrator), it denies me access. I am the administrator on this computer and the SQL Server Express 2014 service is local to my machine. I appreciate any advise the community may be able to provide here. :)

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.

I forgot my sql server 2008 r2 username an password

I installed an instance of sql server on my machine a while back.
I generally login via SSMS. I currently forgot the username and password for the instance.
Is there a way I can recover it. I searched online including a pervious stack overflow post but didnt
find it helpful. Is there anything I can do to recover my username and password ?
Did you try to Troubleshooting: Connecting to SQL Server When System Administrators Are Locked Out
Start the instance of SQL Server in single-user mode by using either
the -m or -f options. Any member of the computer's local
Administrators group can then connect to the instance of SQL Server as
a member of the sysadmin fixed server role.
And then try to do the following steps as explained by Remus Rusanu in this answer.
shutdown MSSQL$EXPRESS service (or whatever the name of your SQL Express service is)
start add the -m and -f startup parameters (or you can start sqlservr.exe -c -sEXPRESS -m -f from console)
connect to DAC: sqlcmd -E -A -S .\EXPRESS or from SSMS use admin:.\EXPRESS
run create login [machinename\username] from windows to create your Windows login in SQL
run sp_addsrvrolemember 'machinename\username', 'sysadmin'; to make urself sysadmin member
restart service w/o the -m -f

Resources