Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 593
  • Last Modified:

Archive Script for Netlogon

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
sproku
Asked:
sproku
  • 10
  • 8
1 Solution
 
RobSampsonCommented:
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
 
sprokuAuthor Commented:
great! i'll give this a try first :) sorry for the late reply, still trying to familiarize myself around. :)
0
 
RobSampsonCommented:
No problem. Let me know how you go.

Rob.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
sprokuAuthor Commented:
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
 
RobSampsonCommented:
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
 
sprokuAuthor Commented:
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
 
RobSampsonCommented:
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
 
sprokuAuthor Commented:
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
 
RobSampsonCommented:
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
 
sprokuAuthor Commented:
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
 
RobSampsonCommented:
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
 
sprokuAuthor Commented:
perfectly working now! you're the man! :)
0
 
RobSampsonCommented:
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
 
sprokuAuthor Commented:
ooops.. sorry i didnt know.. :)
0
 
sprokuAuthor Commented:
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
 
sprokuAuthor Commented:
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
 
sprokuAuthor Commented:
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
 
RobSampsonCommented:
Great.  Thanks for that. Thanks for the grade too.

Regards,

Rob.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 10
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now