Solved

Need a script to archive log files

Posted on 2011-09-14
6
539 Views
Last Modified: 2013-08-02
Hello everyone,

I hope someone can help. I have had a few stabs at this but it doesn't seem to be working.

I need help writing a script that will zip up the previous month's worth of logfiles, move the zip to a network folder on a mapped drive and then delete the original logs.

The compression format isn't too important, as long as it's fast and gets reasonably good compresion ratios with text/html files, which should be all. I've been favouring 7zip.

The script language isn't too important either as long as it can be run completely in the background as a Windows 7 scheduled task and not impact performance. This rules out linux shell scripting.

An example of the format for todays logs would be:

Webs - Daily - 14 - 09 - 11.txt
OR
Webs - Daily - 14 - 09 - 11.html

Also, there is usually only one file per day, html or text.

Etc

If anyone can help I'd be very grateful.

Regards,
Anthony
0
Comment
Question by:AnthonyUK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 36538761
0
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
ID: 36538852
try something like that:


$lastMonth = ($Now.AddMonths(-1)).month
$SourcePath = "X:\sourceDirectory"
$DestPath = "\\Server\TargetPath"
$FileName="MyZip.zip"
$ZipPath=$SourcePath + $FileName

$files = dir $SourcePath

# empty arrays to hold file lists
$movedFiles = @()
$skippedFiles = @()
#create zip file
$ZipFile = (new-object -com shell.application).NameSpace($path) 

foreach ($file in $files){
	if (($file.lastwritetime).month -eq $lastMonth){
		$newname = $DestPath + "\" + $file.name + "-" + $file.lastwritetime.tostring("yyyy-MMM-dd")		
		$ZipFile.CopyHere($file.fullname)} 

		
		
	}# end if
}# end for

#move zip file
move-item -ev a -path $ZipFile.fullname -destination $DestPath + "\" + $FileName

#loop through old files, deleting them
foreach ($file in $files){
	if (($file.lastwritetime).month -eq $lastMonth){
		Remove-Item $file | out-null
	}

}

Open in new window

0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 36539101
Could be done using 7zip from a BAT file.

Would you run the scheduled job on say the last day of the month, after the backup file has been created?  This would take the date logic out of the mix, and the script could just grab all files in the folder and place them in the monthly archive on the server.

~bp
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 37124914
@AnthonyUK

Any feedback?

~bp
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question