A simple script for automatic System State backup where you can easily set backup retention period and it also send you an e-mail notification after completion. With this script you can also archive backup log and if you wish u can also set backup log retention period. It can also set backup files with date stamp.
For e.g., SysBackup_ComputerName_24-03-2009.bkf
You need two small utility,
- Blat.exe for sending mail, you can get details from here.
- Forfiles.exe for checking old files, you can get details from here.
Just download following zip file and extract its content to %windir% or %windir%system32 folder.
System32.zip (75.5 KiB, 1,556 hits)
Mail Notification

Following are the basic Backup Settings
::Backup Settings START
::Set Backup Path
::~~~~~~~~~~~~~~~~~~~~
set savepath=D:TestBackup
::Set Backup Name
::~~~~~~~~~~~~~~~~~~~~
set backupname=SysBackup
::Set Backup Retention Period(in days)
::~~~~~~~~~~~~~~~~~~~~
set retention=3
::Set Log Archive Path(Only Folder Name)
::~~~~~~~~~~~~~~~~~~~~
set logarchivepath=BackupLog
::If ‘yes’ then backup log retention will enabled
::~~~~~~~~~~~~~~~~~~~~
set logretention=yes
::If avove settings is “yes” then set log Retention Period(in days)
::~~~~~~~~~~~~~~~~~~~~
set logretentionperiod=3
::———-x———
::E-Mail Settings (SMTP without authentication will work)
::———-x———
::If ‘yes’ a mail will be fired after backup
::~~~~~~~~~~~~~~~~~~~~
set _mailnotification=yes
::If ‘yes’ backup log will attached with mail
::~~~~~~~~~~~~~~~~~~~~
set attachment=no
::Type your e-mail address(for multiple e-mail ID, use ‘,’)
::~~~~~~~~~~~~~~~~~~~~
set [email protected]
:: Type from mail ID
::~~~~~~~~~~~~~~~~~~~~
set from=%computername%@TC-Auto-NTBackUP
::Set SMTP Server IP Address
::~~~~~~~~~~~~~~~~~~~~
set mserver=10.0.0.1
::Backup Settings END
Batchfile NTBackup.bat
@echo off
color 0c
MODE CON: COLS=50 LINES=11
echo ***************************************
echo Auto SystemState Backup with Retention
echo Log Archive and Mail Notification
echo Code By : Saugata Datta
echo ~~~
echo (c) https://technochat.in
echo Date: %DATE%, Time: %TIME%
echo ***************************************
::Backup Settings START
::Set Backup Path
::~~~~~~~~~~~~~~~~~~~~
set savepath=D:TestBackup
::Set Backup Name
::~~~~~~~~~~~~~~~~~~~~
set backupname=SysBackup
::Set Backup Retention Period(in days)
::~~~~~~~~~~~~~~~~~~~~
set retention=3
::Set Log Archive Path(Only Folder Name)
::~~~~~~~~~~~~~~~~~~~~
set logarchivepath=BackupLog
::If 'yes' then backup log retention will enabled
::~~~~~~~~~~~~~~~~~~~~
set logretention=yes
::If avove settings is "yes" then set log Retention Period(in days)
::~~~~~~~~~~~~~~~~~~~~
set logretentionperiod=3
::----------x---------
::E-Mail Settings (SMTP without authentication will work)
::----------x---------
::If 'yes' a mail will be fired after backup
::~~~~~~~~~~~~~~~~~~~~
set _mailnotification=yes
::If 'yes' backup log will attached with mail
::~~~~~~~~~~~~~~~~~~~~
set attachment=no
::Type your e-mail address(for multiple e-mail ID, use ',')
::~~~~~~~~~~~~~~~~~~~~
set [email protected]
:: Type from mail ID
::~~~~~~~~~~~~~~~~~~~~
set from=%computername%@FC-Auto-NTBackUP
::Set SMTP Server IP Address
::~~~~~~~~~~~~~~~~~~~~
set mserver=10.0.0.6
::Backup Settings END
::--------------------------------
::Don't change following lines.
::--------------------------------
forfiles -p "%savepath%" /m *.bkf /s /d -%retention% /c "cmd /C del @FILE"
:: get the date and time and then into single variable
for /F "tokens=1-4 delims=/ " %%i in ('date /t') do (
set MM=%%j
set DD=%%k
set YYYY=%%l
set date=%%k-%%j-%%l
set dirdate=%%j%%k%%l
)
for /f "Tokens=1-2 delims=/ " %%i in ('time /t') do (
set tm=%%i
set ampm=%%j
)
::Deleting Old Backup Log files
if not exist "%SystemDrive%%HOMEPATH%Local SettingsApplication DataMicrosoftWindows NTNTbackupData*.log" goto _nodel
del "%SystemDrive%%HOMEPATH%Local SettingsApplication DataMicrosoftWindows NTNTbackupData*.log" >nul
:_nodel
ntbackup.exe backup systemstate /v:no /r:no /rs:no /hc:off /J %backupname% /l:s /f "%savepath%%backupname%_%computername%_%date%.bkf"
if not exist "%savepath%%logarchivepath%" md "%savepath%%logarchivepath%" >nul
move "%SystemDrive%%HOMEPATH%Local SettingsApplication DataMicrosoftWindows NTNTbackupData*.log" %savepath%%logarchivepath%%backupname%_%computername%_%date%.txt >nul
if not "%_mailnotification%" == "yes" goto _end
if not "%attachment%" == "yes" goto _noattach
blat "%savepath%%logarchivepath%%backupname%_%computername%_%date%.txt" -attach "%savepath%%logarchivepath%%backupname%_%computername%_%date%.txt" -to %email% -s "NT Backup Success/Failure Report of %computername%" -f "%from%" -server "%mserver%" > nul
goto _skip
:_noattach
blat "%savepath%%logarchivepath%%backupname%_%computername%_%date%.txt" -to %email% -s "NT Backup Success/Failure Report of %computername%" -f "%from%" -server "%mserver%" > nul
:_skip
if not "%logretention%" == "yes" goto _end
forfiles -p "%savepath%%logarchivepath%" /m *.txt /s /d -%logretentionperiod% /c "cmd /C del @FILE"
:_end
exit
Now you have to create a schedule for daily backup.


Setup your own Monitoring – Disk Space Utilization Monitoring tool for free
It is always recommended to keep tracking of the disk space utilization…
Port Scanner using PowerShell with Email Notification
Few days back I was asked to create a script, which will…
Set File System Auditing via PowerShell
For last few days, I was trying to figure out how to…
AWS EBS Snapshots Automation (Disaster Recovery) from Windows PowerShell
It is always a good idea to reduce hardware dependencies by moving…
Place batch file in your %systemroot% then set schedule it for backup, unless log archive and mail sent not working..
I don’t know why, but i am also trying to find out the reason..
The style of writing is very familiar to me. Did you write guest posts for other blogs?
Not really, i don’t know u r talking about which blog..
It is working fine except mail notification
Did you placed Blat.exe and Forfiles.exe to your System32 folder?
And also tell me which mail server you have??
And to test your server use following command with
blat “%windir%system32eula.txt” -to type-your-mail-id-here -s “TestMail” -f “type-your-mail-id-here” -server “type-your-smtp-server-ip”
After executing above command u will get a test mail.
**Also i have fixed that problem to placing bat file under %systemroot%, now u can placed this batch file anywhere..
Hi,
First of all Many Thanks!! for the awesome batch file you really saved my life..
I did test the batch file and it works perfectly.. But i just wanted to know wether would it be possible to copy the .bkf file to a share folder.
I would like to have few more lines in the said batch script.
net use f: /delete
net use F: \flsrvSYS$ /USER:flsrvSYS SYS@1234
Now I want a command to copy the System_State_Backup_Test_16-05-2011.bkf to f:
Also while i test the connection by sending a test mail i get the following error.
The SMTP server does not like the sender name.
Have you set your mail address correctly?
It would be very helpful if you could help me setting up the above conditions in the batch script.
Awating for your positive reply.
Ankit S
Hi Ankit,
Try this..
http://pastebin.com/NBh91aEE
Note: You have to set your network path info on the batch file..
set mapdrivename=H:
set mapdrivepath=\servershare
set mapdriveuser=user
set mapdrivepass=password
Do you have internal smpt server in your network? if yes, check if it required authentication before sending mail?
If no, then try following command from command prompt to send a test mail to your mailbox.
blat “%windir%system32eula.txt” -to type-your-mail-id-here -s “TestMail” -f “type-your-mail-id-here” -server “type-your-smtp-server-ip”
Hi Saugata,
Many Thanks for your reply!!.
While setting the scheduled task i use
NT AUTHORITYSYSTEM user account.
Now when i schedule the batch file using said account, I do not get any log files in the Backup Log Folder. I did check that “System” user account has full access on my profile folder.
Can you please help pinning down what might the reason for not getting the Backup Report when .bat is run through schedule task??
PS: I do get the Backup Report when i run the .bat file by double-clicking the same.
Regard
Ankit S
Hi Saugata,
I was just wondering, If have the same batch srcipt for Backing System State Data of Win 2008 R2 Server.