Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Script to zip all database backups

Author  Topic 

prash.dba
Starting Member

5 Posts

Posted - 2009-12-07 : 10:39:52
DECLARE @name VARCHAR(50) -- database name
DECLARE @path VARCHAR(256) -- path for backup files
DECLARE @path1 VARCHAR(256) -- path for backup files
DECLARE @fileName VARCHAR(256) -- filename for backup
DECLARE @ZipName VARCHAR(256)
DECLARE @fileDate VARCHAR(20) -- used for file name
Declare @SQL nvarchar(255)

SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)


DECLARE db_cursor CURSOR FOR
SELECT name
FROM master.dbo.sysdatabases

OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name

WHILE @@FETCH_STATUS = 0
BEGIN

SET @path = 'D:\SQLBackup\'+@name+'\'
SET @ZipName= @path + @name + '_' + @fileDate + '.zip'
SET @fileName = @path + @name + '_' + @fileDate + '.BAK'
--SET @SQL = zip @ZipName @filename
Exec xp_cmdshell '''zip' @ZipName @Filename ''

FETCH NEXT FROM db_cursor INTO @name
END

CLOSE db_cursor
DEALLOCATE db_cursor

Hi, I wrote this script to get all database names from sysdatabases and wrote a zip command to execute in xp_cmdshell to zip the backups of all databases from their respective directories. I seem to have problem with putting single quotes in the zip command.
Could any one suggest a solution for me.
Thanks


X002548
Not Just a Number

15586 Posts

Posted - 2009-12-07 : 10:41:56
well...first...you don't even check if the file exists or not



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

prash.dba
Starting Member

5 Posts

Posted - 2009-12-07 : 10:51:11
Yea, The file exists, Iam getting syntax error.

Thanks
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-08 : 01:34:17
What is the error?
Refer this to know how single quotes work in SQL Server
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/02/19/understanding-single-quotes.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -