Solved

Need a script to archive log files

Posted on 2011-09-14
6
535 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 54

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 54

Expert Comment

by:Bill Prew
ID: 37124914
@AnthonyUK

Any feedback?

~bp
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
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…

740 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