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 12»»

Create and populate a Zip file Expand / Collapse
Author
Message
Posted Tuesday, October 16, 2012 8:35 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 3:02 PM
Points: 325, Visits: 441
I have a small program that runs every day from my client machine that creates a new zip folder on a shared drive, then copies the contents of another folder (and its subfolders) into that zip folder for backup. The backed-up files are around 50 MB total before zipping, and contain an ACCDB, ACCDE, and DDL file among a few others.

I would rather my MS Server 2008 do this job, since it's already there and running my SQL Server instance. I'm thinking there must be a way to do this using T-SQL in a daily scheduled job. Any resources?


Jim
Post #1373314
Posted Tuesday, October 16, 2012 8:44 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:10 AM
Points: 5,437, Visits: 10,129
Jim

If you have xp_cmdshell enabled, you can use it to run the command line for your zip utility. Otherwise, you could consider using SSIS to do the same. Another alternative is to run it from a Windows scheduled task, bypassing SQL Server altogether.

John
Post #1373318
Posted Tuesday, October 16, 2012 9:05 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:17 AM
Points: 12,927, Visits: 32,327
here's an xp_cmdshell example, like John mentioned above.
the list of files, isntead of being a single file, can be a space delimited set uf multiple files.


--http://www.7-zip.org/download.html
--http://downloads.sourceforge.net/sevenzip/7za920.zip

--xp_cmdshell is limited to ONE set of double quotes.
--one of the paths needs to not contain spaces!
--see 2003 MS KB http://support.microsoft.com/kb/266224
DECLARE @results TABLE(results varchar(255))
declare @command varchar(2000)
--zip one file
SET @command =
'"C:\DataFiles\7zip_CommandLine_7za465\' --path to 7za command line utility note the dbl quotes for long file names!
+ '7za.exe"' --the exe: i'm using in the command line utility.
+ ' a ' --the Add command: add to zip file:
+ 'C:\DataFiles\' --path for zip
+ 'myZipFile.zip' --zip file name, note via xp_cmdshell only one pair of dbl quotes allowed names!
+ ' ' --whitespace between zip file and file to add
+ 'C:\DataFiles\' --path for the files to add
+ 'SandBox_2011-07-25.bak' --the file
+ ' -y' --suppress any dialogs by answering yes to any and all prompts
print @command
--"C:\DataFiles\7zip_CommandLine_7za465\7za.exe" a C:\DataFiles\myZipFile.zip C:\DataFiles\SandBox_2011-07-25.bak -y

insert into @results
exec xp_cmdshell @command

select * from @results
/*
NULL
7-Zip (A) 4.65 Copyright (c) 1999-2009 Igor Pavlov 2009-02-03
NULL
Scanning
NULL
Updating archive C:\DataFiles\myZipFile.zip
NULL
Compressing SandBox_2011-07-25.bak
NULL
Everything is Ok
NULL
*/




Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1373339
Posted Monday, October 22, 2012 10:13 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 3:02 PM
Points: 325, Visits: 441
Lowell, I continue to struggle with this. I downloaded zip-7, and wrote a vbs script to call it. Problem is, both the source and target paths have spaces in the pathnames. In any shell situation, I can't seem to use two sets of double quotes. One set gets eaten up. Am I doing it wrong?

    Option Explicit

' Need to create a full path following the Target Location:
' Higher Level Folder: "YYYY-MMM_Backup"
' File Name "YY-MM-DD.Zip"
' Backing up the "CPAS folder and all its subfolders"

Dim YYYY
Dim YY
Dim MMM
Dim MM
Dim DD

Dim strZipInstruction
Dim strZipProgLoc
Dim strTargetLoc
Dim strSourceLoc
Dim FileNameZip
Dim strDate

Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")


YYYY = Cstr(Year(Date))
YY = Right(YYYY,2)
MM = FormatNumber((Month(Date)),0,-1)
MMM = Left(MonthName(Month(Date)),3)
DD = FormatNumber(Day(Date),0,-1)

strTargetLoc = "H:\mtv\secure\Construction\Access\All Database Backup\" & YYYY & "-" & MMM & "_Backup\"
If Not oFSO.FolderExists(strTargetLoc) Then
oFSO.CreateFolder strTargetLoc
End If
strTargetLoc = """H:\mtv\secure\Construction\Access\All Database Backup\" & YYYY & "-" & MMM & "_Backup\"""
strSourceLoc = "H:\mtv\secure\Construction\Access\CPAS\"
strZipProgLoc = """C:\Documents and Settings\jamesshaffer\My Documents\7-Zip\7za.exe"" a "
strDate = YY & "-" & MM & "-" & DD
FileNameZip = strDate & ".zip"
strZipInstruction=strZipProgLoc & strTargetLoc & FileNameZip & " " & strSourceLoc
MsgBox strZipInstruction



Jim
Post #1375577
Posted Monday, October 22, 2012 11:06 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:17 AM
Points: 12,927, Visits: 32,327
Jim the single set of double quotes is an xp_cmdshell thing;there's only the classic extra-step work arounds.

the work around is to either use a variable, or create it in a non-space-containing folder,and simply move it after words....c:\Temp or %appdata%, for example.

so create the zip in %appdata%\Zipfilename.zip, and then move %appdata%\Zipfilename.zip to the final folder that has spaces.

if the move is on the same drive it's super quick, since it's just updating the allocaiton tables, right?


Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1375618
Posted Tuesday, October 23, 2012 1:35 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:10 AM
Points: 5,437, Visits: 10,129
Jim

A vbs script isn't a SQL Server solution, so it's a little outside the scope of these forums. I think it would be simpler to create a batch file, or use the xp_cmdshell solution described by Lowell. If you're having trouble escaping your double quotes in your vbs script, this discussion has a couple of suggestions.

John
Post #1375876
Posted Tuesday, October 23, 2012 6:04 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:17 AM
Points: 12,927, Visits: 32,327
jim if you print the variable results, you'll see there's a quote in the wrong place.
"C:\Documents and Settings\jamesshaffer\My Documents\7-Zip\7za.exe" a "F:\mtv\secure\Construction\Access\All Database Backup\2012-Oct_Backup\"12-10-23.zip F:\mtv\secure\Construction\Access\CPAS\


i've corrected your vbs code below to build the path correctly:
--"C:\Documents and Settings\jamesshaffer\My Documents\7-Zip\7za.exe" a "F:\mtv\secure\Construction\Access\All Database Backup\2012-Oct_Backup\12-10-23.zip" "F:\mtv\secure\Construction\Access\CPAS\"

' Need to create a full path following the Target Location:
' Higher Level Folder: "YYYY-MMM_Backup"
' File Name "YY-MM-DD.Zip"
' Backing up the "CPAS folder and all its subfolders"

Dim YYYY
Dim YY
Dim MMM
Dim MM
Dim DD

Dim strZipInstruction
Dim strZipProgLoc
Dim strTargetLoc
Dim strSourceLoc
Dim FileNameZip
Dim strDate

Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")


YYYY = CStr(Year(Date))
YY = Right(YYYY, 2)
MM = FormatNumber((Month(Date)), 0, -1)
MMM = Left(MonthName(Month(Date)), 3)
DD = FormatNumber(Day(Date), 0, -1)

strTargetLoc = "M:\mtv\secure\Construction\Access\All Database Backup\" & YYYY & "-" & MMM & "_Backup\"
If Not oFSO.FolderExists(strTargetLoc) Then
'oFSO.CreateFolder strTargetLoc
End If
strTargetLoc = """M:\mtv\secure\Construction\Access\All Database Backup\" & YYYY & "-" & MMM & "_Backup\"
strSourceLoc = """M:\mtv\secure\Construction\Access\CPAS\"""
strZipProgLoc = """C:\Documents and Settings\jamesshaffer\My Documents\7-Zip\7za.exe"" a "
strDate = YY & "-" & MM & "-" & DD
FileNameZip = strDate & ".zip"""
strZipInstruction = strZipProgLoc & strTargetLoc & FileNameZip & " " & strSourceLoc
'Debug.Print strZipInstruction
MsgBox strZipInstruction



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1375990
Posted Tuesday, October 23, 2012 9:04 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, October 31, 2013 3:44 AM
Points: 314, Visits: 4,128
This problem could probably be more "cleanly" solved with a CLR object.
Happy to help you knock one up if you'd like to go that route.
Post #1376117
Posted Tuesday, October 23, 2012 9:10 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:17 AM
Points: 12,927, Visits: 32,327
OTF (10/23/2012)
This problem could probably be more "cleanly" solved with a CLR object.
Happy to help you knock one up if you'd like to go that route.


That would be a valuable contribution; i'd love to see it!


Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1376124
Posted Tuesday, October 23, 2012 9:17 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 3:02 PM
Points: 325, Visits: 441
I'd love to have that learning experience.

VBS is very unsatisfactory. No editor, No debugger. I wrote the original program in VBA and deployed it as a standalone db (with no tables...) and set it up as a daily scheduled task. I've had lots of authentication issues that I think are now behind me, so that approach will likely work now. We'll see tonight....

So, if you could show me how to implement this as a CLR object, I'd love to learn it. Thanks.


Jim
Post #1376132
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse