sql-serversql-server-2019-express

The EXECUTE permission was denied on the object 'DatabaseBackup', database 'master', schema 'dbo'


I'm trying to setup automated backups for my SQL Server Express 2019. I'm using the script from https://github.com/olahallengren/sql-server-maintenance-solution

Here's the command I run from an elevated command prompt.

sqlcmd -E -S .\SQLEXPRESS -d master -Q 
"USE master; 
grant execute on dbo.master.DatabaseBackup to [TODDCOM\itadmin];
EXECUTE dbo.DatabaseBackup @Databases = 'USER_DATABASES', @Directory = 'c:\sql2019\backup', @BackupType = 'FULL', @Verify = 'Y', @CheckSum ='Y'" -b -o C:\SQL2019\Backup\DatabaseBackup.txt
robocopy c:\sql2019\backup \\NAS\backup\SQL\ /E /MIR /LOG:"\\NAS\Backup\robocopy-log.txt" /np

DatabaseBackup.txt reports this error:

Changed database context to 'master'. Msg 15151, Level 16, State 1, Server SPURR\SQLEXPRESS, Line 1 Cannot find the object 'DatabaseBackup', because it does not exist or you do not have permission. Msg 229, Level 14, State 5, Server SPURR\SQLEXPRESS, Procedure dbo.DatabaseBackup, Line 1 The EXECUTE permission was denied on the object 'DatabaseBackup', database 'master', schema 'dbo'.

I've used select suser_sname() to confirm that the account being used is the same as the one i gave permissions to. So far I've applied execute permissions to it in the following places:

I'm assuming that I only need it in one place but I can't figure out which place to put the permissions. Can anyone help me figure out what I'm doing wrong?

UPDATE: I updated the commands and error report to show what we've tried from the comments.

UPDATE: Adding more info. I ran this code from the task scheduled batch file:

sqlcmd -E -S .\SQLEXPRESS -d master -Q "SELECT SUSER_NAME(); select name, USER_NAME(s.principal_id) AS Schema_Owner from sys.schemas s;" -o C:\SQL2019\Backup\User.txt

and it output:

domain\admin

(1 rows affected)
name                 Schema_Owner
-------------------- ----------------
dbo                  dbo
guest                guest
INFORMATION_SCHEMA   INFORMATION_SCHEMA
sys                  sys
db_owner             domain\admin
db_accessadmin       db_accessadmin
db_securityadmin     db_securityadmin
db_ddladmin          db_ddladmin
db_backupoperator    domain\admin
db_datareader        db_datareader
db_datawriter        db_datawriter
db_denydatareader    db_denydatareader
db_denydatawriter    db_denydatawriter

(13 rows affected)```


Solution

  • After some more interactive investigation with OP, this appears to have been an orphaned user in the master database. This is why all the signs of "permissions have been granted to that user" checked out - because they had. But because the login wasn't getting mapped to the user appropriately (but was getting mapped to the guest user), the login wasn't getting those permissions. I had them run alter user [domain\admin] with login [domain\admin] and that fixed the issue.