Operating System - Microsoft
cancel
Showing results for 
Search instead for 
Did you mean: 

Monthly Backups....dos style =)

SOLVED
Go to solution
Monty Lovell
Advisor

Monthly Backups....dos style =)

Ganesh helped me with this batch file, and it works perfectly for daily backups....thanks again Ganesh =). Now I'm hoping to add to this batch file:
cd\
cd c:\Program Files\Winzip

for /f "tokens=2,3,4 delims=/ " %%i in ('date /t') do (
set year=%%k
set month=%%i
set day=%%j
)

wzzip c:\backup\Daily\h%month%%day%%year%.zip -r -p -t h:\*.*

I'm hoping to make monthly's now. Basically all files that were made in June '04. Is this possible?
Thanks for your help ahead of time! =)
Monty.
24 REPLIES
Ganesh Babu
Honored Contributor
Solution

Re: Monthly Backups....dos style =)

Hi Monty,
I am assuming that u r looking for the batch file to backup all files which were created in June04 into one zip file with the name as todays date eg. h07122004.zip

Is this right??

if yes then u have to check filter the files in h:\ right now u have given it as h:\*.* which means all files..

is there a file name format used to find the files which are created for this month??

Ganesh
Monty Lovell
Advisor

Re: Monthly Backups....dos style =)

Hi Ganesh - Yes the zip file should contain all files made in a single month. I guess I'm not sure on how to filter them, and the files can range from a list of extensions. I don't believe we have a file name format system set up...sorry.
It sounds like it might be an awfully big Dos task to filter, and if so then I can just do it manually.
Thanks again for your time! =)
Monty.
Ganesh Babu
Honored Contributor

Re: Monthly Backups....dos style =)

Hi Monty,
this can be achieved using the windows scripting.. wait for the next post.. i will get it ..

Ganesh
Jon Finley
Honored Contributor

Re: Monthly Backups....dos style =)

Actually you got all of the info you need now to make the monthly backup.

If you run the monthly one each month, at the start of the month, it will be easy to create the batch file (See example 1).

If you are going to run the monthly from within your daily backup script, it will take more work (See example 2).

Example 1 (run after end of month)
--------------

cd\
cd c:\Program Files\Winzip

for /f "tokens=2,3,4 delims=/ " %%i in ('date /t') do (
set year=%%k
set /a month=%%i-1
if %month% < 10 set month=0%month%
set day=%%j
)

wzzip c:\backup\Monthly\h%month%_%year%.zip -r -p -t c:\backup\Daily\h%month%*%year%.zip

---------------------------

Example 2 (run during every backup)
------------

cd\
cd c:\Program Files\Winzip

for /f "tokens=2,3,4 delims=/ " %%i in ('date /t') do (
set year=%%k
set month=%%i
set day=%%j
)
if %month%==1 set chk_day=31
if %month%==2 set chk_day=28
if %month%==3 set chk_day=31
if %month%==4 set chk_day=30
if %month%==5 set chk_day=31
if %month%==6 set chk_day=30
if %month%==7 set chk_day=31
if %month%==8 set chk_day=31
if %month%==9 set chk_day=30
if %month%==10 set chk_day=31
if %month%==11 set chk_day=30
if %month%==12 set chk_day=31

if chk_day==day do (
wzzip c:\backup\Monthly\h%month%_%year%.zip -r -p -t c:\backup\Daily\h%month%*%year%.zip
)

----------------------------
The above batch file will NOT handle leap years. That will require additional calculations on the year. Also, the "set /a" command is only available on windows 2000, XP and 2003. If you are NOT running one of the above OS's, you will have to run the script manually.

Jon
"Do or do not. There is no try!" - Yoda
Jon Finley
Honored Contributor

Re: Monthly Backups....dos style =)

Oops.. one small mistake:

if chk_day==day do (
wzzip c:\backup\Monthly\h%month%_%year%.zip -r -p -t c:\backup\Daily\h%month%*%year%.zip
)

Should be:

if %chk_day%==%day% do (
wzzip c:\backup\Monthly\h%month%_%year%.zip -r -p -t c:\backup\Daily\h%month%*%year%.zip
)

Jon
"Do or do not. There is no try!" - Yoda
Monty Lovell
Advisor

Re: Monthly Backups....dos style =)

Hey Jon - Your post to me wasn't posted here or I just can't see it for some reason. Anyway here's my reply to your post:

Thanks for this. I just tried it and it ran very quickly. It turned out that no zip file was made. Anyway thanks for the reply and the work done on this.
Monty.
Jon Finley
Honored Contributor

Re: Monthly Backups....dos style =)

Which one did you run? Example 2?

If so, then it won't create a zip file until the last day of the month.

The design was to allow you to run it during the month in your standard backup script, but at the end of the month (when chk_day = the last day of the month 30,31, etc) that a complete Monthly backup file would be created, containing ALL of the previous zip file backups from the entire month.

This also assumes that a "monthly" folder had been created at c:\backup\monthly to contain the zip file. If c:\backup\monthly does not exist, then at the end of the month, this script will fail with a file not found error, or something like it.

Make more sense?

Jon
"Do or do not. There is no try!" - Yoda
Monty Lovell
Advisor

Re: Monthly Backups....dos style =)

Hi Jon - Yes it does make more sense, and thanks. But I was wondering if this batch file could look at all the files on the server, and not the already made daily zip files. We need to do this because files during the month might have changed after the daily zip funcion. Thanks Again =)
Monty.
Jon Finley
Honored Contributor

Re: Monthly Backups....dos style =)

Oh sure... no prob..

Just change the wzzip line to point back to your data folder on H:\ as it was originally.

if %chk_day%==%day% do (
wzzip c:\backup\Monthly\h%month%_%year%.zip -r -p -t h:\*.*
)

Jon
"Do or do not. There is no try!" - Yoda
Ganesh Babu
Honored Contributor

Re: Monthly Backups....dos style =)

Hi Monty,
Sorry i was tied up in a work and not able to post the script faster...

Here it is now..

copy the below code to some filename.wsf for ex., c:\copyfiles.wsf


<script language="VBScript">

dim FSO
dim objsourcefolder
dim filecollection
dim filepath
dim oWS
dim Lastmonth
dim returncode
dim scmd

folder_path="h:\"

Set FSO = CreateObject("Scripting.FileSystemObject")
set oWS = CreateObject("Wscript.Shell")

Set objSourceFolder = FSO.GetFolder(folder_path)
Set fileCollection = objSourceFolder.files

For Each filePath in fileCollection
Lastmonth = CInt(DateDiff("m",filePath.DateLastModified,Now))

scmd = "c:\Program Files\Winzip\wzzip c:\backup\Daily\h" & month(now) & day(now) & year(now) & ".zip -r -p -t "

If Lastmonth = 1 Then

scmd = scmd & folder_path & filepath.name

'WScript.Echo scmd
returnCode = oWS.Run(scmd, 0, true)

End If
next

dim subfolderscoll
dim subfol

set subfolderscoll = objsourcefolder.subfolders

for each subfol in subfolderscoll

Set fileCollection = subfol.files

For Each filePath in fileCollection

Lastmonth = CInt(DateDiff("m",filePath.DateLastModified,Now))

scmd = "c:\Program Files\Winzip\wzzip c:\backup\Daily\h" & month(now) & day(now) & year(now) & ".zip -r -p -t "

If Lastmonth = 1 Then

scmd = scmd & folder_path & subfol.name & "\" & filepath.name

' WScript.Echo scmd
returnCode = oWS.Run(scmd, 0, true)

End If
next
next

</script>


run this script as

CScript.exe C:\copyfiles.wsf


Let me know how it goes..

Ganesh
Monty Lovell
Advisor

Re: Monthly Backups....dos style =)

WOW Thanks Ganesh! I'll have to give it a whirl and Jon's as well. I've been totally swapped here today so I'm hopin' to try it in the mornin.
Thanks again for all your work you guys =)
Totally appreciated!
Monty.
Monty Lovell
Advisor

Re: Monthly Backups....dos style =)

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\lovell.HMFH>Cscript.exe c:\backup\sys\copyfiles.wsf

Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

c:\backup\sys\copyfiles.wsf(31, 2) (null): The system cannot find the file speci
fied.

Hi Ganesh. I just tried it, maybe I gave you the wrong information...or I tried to run it incorrectly. I copied this from the DOS window. Does the script file look for all files within the H: drive or is it looking through the already made daily zip files?
I will totally understand if you just want to drop this...sorry for this.
Ganesh Babu
Honored Contributor

Re: Monthly Backups....dos style =)

Hi Monty,
I think the issue is the system did not take the long folder name..

change this statement in the file

scmd = "c:\Program files\Winzip\wzzip c:\backup\Daily\h" & month(now) & day(now) & year(now) & ".zip -r -p -t "

to

scmd = "c:\Progra~1\Winzip\wzzip.exe c:\backup\Daily\h" & month(now) & day(now) & year(now) & ".zip -r -p -t "

this statement is there 2 times.. so make sure u replace it in both the placess..

Test it out and let me if it works..

Ganesh
Monty Lovell
Advisor

Re: Monthly Backups....dos style =)

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\lovell.HMFH>CScript.exe c:\backup\sys\copyfile.wsf
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

c:\backup\sys\copyfile.wsf(31, 2) (null): The system cannot find the file specif
ied.

Hi Ganesh - I changed it to "~1" both places and it gave me this again. Do you think I'm giving you some wrong data, variables, etc.? Again, if you want to cut your losses I will more than understand. Thanks for all your time in this.
Monty =)
Ganesh Babu
Honored Contributor

Re: Monthly Backups....dos style =)

Hi Morty,
The issue is here only

scmd = "c:\Progra~1\Winzip\wzzip.exe c:\backup\Daily\h" & month(now) & day(now) & year(now) & ".zip -r -p -t "

either the wzzip.exe directory is wrong or the path where it is going to create the zip file might be wrong

1) what is the path of wzzip.exe?
2) does c:\backup\Daily\ directory exists??

Ganesh
Monty Lovell
Advisor

Re: Monthly Backups....dos style =)

Howdy - Yep those are the right directory names. I'm curious though does this script search the existing zip files on my c: drive or all the files on H: drive?
I'm hoping to write a zip file that searches all files on H: drive.
Hope I didn't throw a wrench in the gears.
Monty =)
Ganesh Babu
Honored Contributor

Re: Monthly Backups....dos style =)

Hi Monty,
i want u to run this script

job debug="true">
<script language="VBScript">

dim FSO
dim objsourcefolder
dim filecollection
dim filepath
dim oWS
dim Lastmonth
dim returncode
dim scmd

folder_path="h:\"

Set FSO = CreateObject("Scripting.FileSystemObject")
set oWS = CreateObject("Wscript.Shell")

Set objSourceFolder = FSO.GetFolder(folder_path)
Set fileCollection = objSourceFolder.files

For Each filePath in fileCollection
Lastmonth = CInt(DateDiff("m",filePath.DateLastModified,Now))

scmd = "c:\Progra~1\Winzip\wzzip c:\backup\Daily\h" & month(now) & day(now) & year(now) & ".zip -r -p -t "

If Lastmonth = 1 Then

scmd = scmd & folder_path & filepath.name

WScript.Echo scmd
'returnCode = oWS.Run(scmd, 0, true)

End If
next

</script>


when it runs it will display a message box with the output of the scmd variable. This is the command which has to run to create the zipfile.

So copy that or note it down and run it from where u running the script. And tell me whether there were any errors. If there are any errors. Let me know..

Ganesh
Monty Lovell
Advisor

Re: Monthly Backups....dos style =)

Hi Ganesh - Sure here's the output from this script. Hope this helps, and thanks for your persistance! =)
Monty

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\lovell.HMFH>CScript.exe c:\backup\sys\test.wsf
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

c:\backup\sys\test.wsf(37, 4) Windows Script Host: The end tag does not have a
corresponding start tag : job

>
Monty Lovell
Advisor

Re: Monthly Backups....dos style =)

Oh yeh just to let you know I'll be out of the office all next week so if you want we can take this back up on the 26th when I get back.
Jon Finley
Honored Contributor

Re: Monthly Backups....dos style =)

The following line:
job debug="true">

Should be:


Correct the line, then rerun the script.

Jon
"Do or do not. There is no try!" - Yoda
Monty Lovell
Advisor

Re: Monthly Backups....dos style =)

Thanks Jon - I think I screwed up there when cut/pasting....sorry bout that. "<" was correctly in place.
Monty.
Monty Lovell
Advisor

Re: Monthly Backups....dos style =)

Opps your right...I was thinking it was the original script file your were mentioning. I will place the "<" within the test script, and test again.
Thanks!
Monty.
Monty Lovell
Advisor

Re: Monthly Backups....dos style =)

Hey Cool Guys....here's what I got =)
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\lovell.HMFH>CScript.exe c:\backup\sys\test.wsf
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

c:\Progra~1\Winzip\wzzip c:\backup\Daily\h7162004.zip -r -p -t h:\EFFE.DC5
c:\Progra~1\Winzip\wzzip c:\backup\Daily\h7162004.zip -r -p -t h:\sketchup.skb
c:\Progra~1\Winzip\wzzip c:\backup\Daily\h7162004.zip -r -p -t h:\sketchup.skp

Ganesh Babu
Honored Contributor

Re: Monthly Backups....dos style =)

Hi monty,
Sorry about that "<" missing in the latest script.. can u change wzzip to wzzip.exe in the scmd line. I think then it should work.

Thanks Jon for fixing the missing "<".

Ganesh