Solved

Archive Script for Netlogon

Posted on 2010-11-10
18
581 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
  • 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 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