Backing up DHCP for Windows Server

Although Windows Server’s DHCP feature comes with a backup, I’ve never been able to restore it to another server. Instead, I use a batchfile that runs a netsh command. Here’s how I do it:

All my server batchfiles run from c:\batchfiles, a directory I create as part of my standard Windows Server setup actions. So save the following code to c:\batchfiles\backup-dhcp-files.bat

set backuplocation=\\ghc-file8738.ghc.local\backups\other\dhcp
@ECHO OFF
REM ====================================================================
netsh dhcp server \\%computername% export c:\batchfiles\dhcpdb.backup all
net use o: %backuplocation%
echo f|xcopy C:\batchfiles\dhcpdb.backup o:\%computername%\%DATE:~-2%-%DATE:~4,2%-%DATE:~7,2%-dhcpdb.backup /y /e
o:
cd %computername%
setlocal
set /a cnt=0
set "keep=60"
for /f "eol=: delims=" %%F in ('dir /b /o-d *.backup') do (
 if defined keep (
 2>nul set /a "cnt+=1, 1/(keep-cnt)" || set "keep="
 ) else del "%%F"
)
c:
net use o: /d /y

Set that as a scheduled task to run once a day, and voila – two months of regular backups, of my DHCP server, ready to be restored.

Oh, and to restore the backup, the commands are very similar:

netsh
dhcp server \\servername import c:\location\to\backup\dhcpdb.backup all

And you’re all done.