Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Auto Backup Script Expand / Collapse
Author
Message
Posted Tuesday, July 07, 2009 4:56 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 07, 2013 10:08 PM
Points: 39, Visits: 388
Hi SQL Gurus,
I am new to SQL Server and i am trying to create a SQL Script which will create a directory with name todays date and will backup the test database. I am getting errors while the script starts backingup the database.
Seems there is some problem with the temporary variables (@). Please look at the below code and let me know the issue.

################################################
-- 1 - Declare variables
DECLARE @CMD1 varchar(8000)
DECLARE @RestoreRootDirectory varchar(255)
DECLARE @CurrentDate datetime
DECLARE @CurrentName varchar(8)
-- 2 - Initialize variables
SET @CMD1 = ''
SET @RestoreRootDirectory = 'D:\backups\test\'
SET @CurrentDate = GETDATE()
SELECT @CurrentName = CONVERT(varchar(8), @CurrentDate, 112)

-- 3a - Create the current directory
SELECT @CMD1 = 'EXEC master.dbo.xp_cmdshell ' + char(39) + 'mkdir ' + @RestoreRootDirectory + @CurrentName + '\' + char(39)
-- SELECT @CMD1
EXEC(@CMD1)
BACKUP DATABASE [test] TO DISK = N'@RestoreRootDirectory + @CurrentName '+dd.bak'' WITH NOFORMAT, NOINIT, NAME = N'test-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO
declare @backupSetId as int
select @backupSetId = position from msdb..backupset where database_name=N'test' and backup_set_id=(select max(backup_set_id) from msdb..backupset where database_name=N'test' )
if @backupSetId is null begin raiserror(N'Verify failed. Backup information for database ''test'' not found.', 16, 1) end
RESTORE VERIFYONLY FROM DISK = N'@RestoreRootDirectory + @CurrentName '+dd.bak'' WITH FILE = @backupSetId, NOUNLOAD, NOREWIND
GO

################################################
Post #748415
Posted Tuesday, July 07, 2009 6:18 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 2:12 AM
Points: 1,322, Visits: 4,400
Try this:-
-- 1 - Declare variables
DECLARE @CMD1 varchar(8000)
DECLARE @RestoreRootDirectory varchar(255)
DECLARE @CurrentDate datetime
DECLARE @CurrentName varchar(8)
-- 2 - Initialize variables
SET @CMD1 = ''
SET @RestoreRootDirectory = 'c:\backups\test\'
SET @CurrentDate = GETDATE()
SELECT @CurrentName = CONVERT(varchar(8), @CurrentDate, 112)

-- 3a - Create the current directory
SELECT @CMD1 = 'EXEC master.dbo.xp_cmdshell ' + char(39) + 'mkdir ' + @RestoreRootDirectory + @CurrentName + '\' + char(39)
-- SELECT @CMD1
EXEC(@CMD1)
DECLARE @FileName varchar(255)
SET @FileName = @RestoreRootDirectory + @CurrentName + '\dd.bak'
BACKUP DATABASE [test] TO DISK = @FileName WITH NOFORMAT, NOINIT, NAME = N'test-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO




Post #748456
Posted Tuesday, July 07, 2009 6:36 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 07, 2013 10:08 PM
Points: 39, Visits: 388
Perfect Guru! It went like a rocket! Thanks a lot!!!

Thanks,
Nagarjun.
Post #748474
Posted Tuesday, July 07, 2009 6:52 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, February 10, 2012 5:19 AM
Points: 188, Visits: 188
Nice work... but what was the error you are getting initially?

Thanks & Regards,
Sudeepta.
http://twitter.com/skganguly
Post #748492
Posted Tuesday, July 07, 2009 10:16 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 07, 2013 10:08 PM
Points: 39, Visits: 388
Hey Sudeep,
The code in the red is my earlier code which was giving errors and the code in the green is the correct one. I know its a stupid doubt :) but i am an oracle DBA guy not SQL Server so have to.


[color=#00FF00]SET @FileName = @RestoreRootDirectory + @CurrentName + '\SFB10M01.bak'
BACKUP DATABASE [SFB10M01] TO DISK = @FileName WITH NOFORMAT, NOINIT, NAME = N'SFB10M01-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO[/color]

[color=#FF0000]BACKUP DATABASE [test] TO DISK = N'@RestoreRootDirectory + @CurrentName '+dd.bak'' WITH NOFORMAT, NOINIT, NAME = N'test-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO[/color]

Thanks,
Nagarjun.
Post #749016
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse