Solved

Need a script to archive log files

Posted on 2011-09-14
6
518 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
  • 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 53

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 53

Expert Comment

by:Bill Prew
ID: 37124914
@AnthonyUK

Any feedback?

~bp
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

770 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