Solved

Archive Script for Netlogon

Posted on 2010-11-10
18
584 Views
Last Modified: 2012-05-10
Hi All,

Does anyone know how to create a script (batch / vbs) for archiving the netlogon.bak file from a domain controller and then removes the archived files older than a specified retention period?

I think it's possible by scheduling a batch / vbs script to run every hour and archive the netlogon.bak to a remote location. I just don't know how to script it.

Thanks!
0
Comment
Question by:sproku
[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
  • 10
  • 8
18 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 34127172
Hi there,

A script like the this will copy the NetLogon.bak to a backup folder and then delete any file older that intFileAge days.

Regards,

Rob.
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Specify the destination backup folder
strBackupFolder = "\\server\share\NetlogonBackups\"
' Specify the file to back up
strBAK = objShell.ExpandEnvironmentStrings("%LOGONSERVER%") & "\NetLogon\NetLogon.bak"
' Specify the age of the files to retain in days
intFileAge = 30

' Append the current date and time in yyyymmdd_hhmmss format to the file name when it is backed up
If Right(strBackupFolder, 1) <> "\" Then strBackupFolder = strBackupFolder & "\"
strDate = Year(Date) & Right("0" & Month(Date), 2) & Right("0" & Day(Date), 2) & Right("0" & Hour(Time), 2) & Right("0" & Minute(Time), 2) & Right("0" & Second(Time), 2)
strNewFile = "NetLogon_" & strDate & ".bak"
' Copy the file to the backup location
objFSO.CopyFile strBAK, strBackupFolder & strNewFile, True

' Purge all files in the backup folder older than intFileAge days
For Each objFile In objFSO.GetFolder(strBackupFolder).Files
	If Right(LCase(objFile.Name), 4) = ".bak" Then
		If objFile.DateCreated > DateAdd("d", -intFileAge, Now) Then objFile.Delete
	End If
Next

Open in new window

0
 

Author Comment

by:sproku
ID: 34133429
great! i'll give this a try first :) sorry for the late reply, still trying to familiarize myself around. :)
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34133643
No problem. Let me know how you go.

Rob.
0
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!

 

Author Comment

by:sproku
ID: 34133747
Hi Rob,
I've tried the script but im not quite sure how to modify it. My apologies for this but I'm not really that good when it comes to scripting.

Here's how i understand it so far:

I've checked the order of your script (haven't tested it yet though).
As i could comprehend, the script does as what you say in the following order:

1. copy the netlogon.bak file to backup location
2. purge backup files on the destination folder older than specified period.

This is good already, however can we also add a remove operation of the original netlogon.bak file once it has been successfully copied to the destination folder?


Thanks! :)
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34134169
Sure, to do that, after this line:
objFSO.CopyFile strBAK, strBackupFolder & strNewFile, True

you can add this:
If objFSO.FileExists(strBackupFolder & strNewFile) = True Then objFSO.DeleteFile strBAK, True

That will delete the original file after it has copied to the backup folder.

That of course, assumes that it will be available the next time you run the script....there is no checking on whether the original file exists before attempting to copy it, and it would throw an error.

Regards,

Rob.
0
 

Author Comment

by:sproku
ID: 34134220
yah, i forgot. we need to check also if file exists before doing the copy operation. can we include that as well?
actually i found a batch script for this task but can't get thru yet with renaming the file to its creation date.

whichever works first :)
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34134986
Sure, well that's easy.  I've added a check to see if the original file exists before trying to copy it.

Regards,

Rob.
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Specify the destination backup folder
strBackupFolder = "\\server\share\NetlogonBackups\"
' Specify the file to back up
strBAK = objShell.ExpandEnvironmentStrings("%LOGONSERVER%") & "\NetLogon\NetLogon.bak"
' Specify the age of the files to retain in days
intFileAge = 30

If objFSO.FileExists(strBAK) = True Then
	' Append the current date and time in yyyymmdd_hhmmss format to the file name when it is backed up
	If Right(strBackupFolder, 1) <> "\" Then strBackupFolder = strBackupFolder & "\"
	strDate = Year(Date) & Right("0" & Month(Date), 2) & Right("0" & Day(Date), 2) & Right("0" & Hour(Time), 2) & Right("0" & Minute(Time), 2) & Right("0" & Second(Time), 2)
	strNewFile = "NetLogon_" & strDate & ".bak"
	' Copy the file to the backup location
	objFSO.CopyFile strBAK, strBackupFolder & strNewFile, True
	If objFSO.FileExists(strBackupFolder & strNewFile) = True Then objFSO.DeleteFile strBAK, True
	
	' Purge all files in the backup folder older than intFileAge days
	For Each objFile In objFSO.GetFolder(strBackupFolder).Files
		If Right(LCase(objFile.Name), 4) = ".bak" Then
			If objFile.DateCreated > DateAdd("d", -intFileAge, Now) Then objFile.Delete
		End If
	Next
End If

Open in new window

0
 

Author Comment

by:sproku
ID: 34151555
Hi Rob,

i can't get the script working.
i tried modifying the backup folder to my shared folder address. do i have to modify also the strBak?

the netlogon.bak file is under c:\windows\debug folder
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34151580
Ah, yes you will...I assumed this would be in the NetLogon share of a domain controller.  Just change this line:
strBAK = objShell.ExpandEnvironmentStrings("%LOGONSERVER%") & "\NetLogon\NetLogon.bak"

to this
strBAK = "C:\Windows\Debug\NetLogon.bak"

Regards,

Rob.
0
 

Author Comment

by:sproku
ID: 34151634
i think something's wrong. it will copy the file but the it will also delete it right away. I've created a test .bak file on the source folder but it will delete it on the destination folder right after it is created.

is the file age which is 30 = days?
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 125 total points
ID: 34151712
Oh whoops.  I had the logic backwards.  Please change this line:
                  If objFile.DateCreated > DateAdd("d", -intFileAge, Now) Then objFile.Delete

to this:
                  If CDate(objFile.DateCreated) < CDate(DateAdd("d", -intFileAge, Now)) Then objFile.Delete

Regards,

Rob.
0
 

Author Comment

by:sproku
ID: 34151728
perfectly working now! you're the man! :)
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34151881
No problem, but I think you should be accepting one of my comments as the answer, and not closing the question by accepting your comment.  Please close the question out correctly.

Regards,

Rob.
0
 

Author Comment

by:sproku
ID: 34151893
ooops.. sorry i didnt know.. :)
0
 

Author Comment

by:sproku
ID: 34151906
how can i correct it? it's already set to closed?

it says:

This question already has an auto close request. You must cancel the previous request before creating another.

sorry im a newbie here..
0
 

Author Comment

by:sproku
ID: 34151941
Sorry i need to award the points under Rob's account. I accidentally clicked "accept as solution" on my own comment. now i can't accept rob's comment as solution because this case is set to auto close
0
 

Author Closing Comment

by:sproku
ID: 34152083
this is my first time getting a support and im overwhelmed by the expert who responded my problem. i gave him an overall rating of A = excellent! :) Thanks ROB!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34152159
Great.  Thanks for that. Thanks for the grade too.

Regards,

Rob.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Outlook 2010 - access public folder shortcut. 1 74
Index Match Formula VBA 6 76
how to diff two files 12 58
Get average for unknown number of cells per degree.. 3 36
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
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…

739 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