简体   繁体   中英

Scripting backup of SQL Server 2008 R2

I have been trying to follow this link to automate my database backups:

https://support.microsoft.com/en-us/help/2019698/how-to-schedule-and-automate-backups-of-sql-server-databases-in-sql-se

I have loaded the stored procedure in my [master] database and have created a batch file to execute the stored procedure.

When running the batch file below using SQL Server authentication, I don't get any output in my backup location

SQLCMD -S .\servername -U username -P password 
       -Q "EXEC sp_BackupDatabases  @databaseName='databasename', @backupType=’F’, @backupLocation = 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup'"

I have made sure that the username login has the appropriate role membership for the database I am trying to backup ( db_backupoperator is selected, together with db_owner ).

I am also not sure how to generate a log file to see if there are any tracebacks - the batch file just completes with no output.

Here are the server properties:

Product: Microsoft SQL Server Express Edition with Advanced Services
Operating System: Microsoft Windows NT 6.1 (7601)
Platform: NT INTEL X86
Version: 10.50.1617.0 RTM

I appreciate very much if someone would help me out, thank you.

Your error is here:

@backupLocation = 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup'

In documentation is written how you should specify the path:

Example1

sqlcmd -S .\EXPRESS –E -Q "EXEC sp_BackupDatabases @backupLocation='D:\SQLBackups\', @backupType='F'" 

Note the last slash in the path.

Now, the code simplified that was executed looks like this:

declare @backupLocation nvarchar(200) = 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup',
        @DBNAME sysname = 'databasename',
        @BackupFile varchar(100)

SET @BackupFile = @backupLocation+REPLACE(REPLACE(@DBNAME, '[',''),']','')+ '_FULL_'+ '20190222'+ '.BAK'
select @BackupFile

So the file name is

C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backupdatabasename_FULL_20190222

And you should look for it in C:\\Program Files\\Microsoft SQL Server\\MSSQL10_50.MSSQLSERVER\\MSSQL\\

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM