?
Solved

Command Line NTBackup

Posted on 2005-05-12
5
Medium Priority
?
2,142 Views
Last Modified: 2012-05-05
Hello-

I am trying to create a bat script, that would use Windows NTbackup program to do the following using command line:

- backup Data, System State and Exchnage
- Mail a notification upon creation, with log files attached
- Keep three days worth of backups

What I want, is to ask:

1. Is it possible, to know the name of the log file that the backup generats, and store it in a variable. This should include the path of this file as well ?

2. Can anyone please help me with keeping the latest 3 files for NTBackup and delete the rest from a folder ?

Has anyone implimented this using NTBackup ? I know there are better solutions like Veritas available, but NTBackup and a batch script is the only choice I have.

TIA
0
Comment
Question by:KaliKoder
  • 3
  • 2
5 Comments
 
LVL 97

Expert Comment

by:Lee W, MVP
ID: 13993412
I have done something much like this.  The system state must be a seperate job.  In addition, you will likely need a 3rd party mail program to mail the info to you - I prefer gbmailer (command line; sends the contents of a text file).


Here's a copy of the message I receive after backup:

Backup Request Executed on Fri 05/13/2005 at  1:00:02.93 by administrator
 
Job Variables:
    BackupDrive=J:
    BackupFile=J:\all\20050513. 10002.bkf
    BackupLogFile=20050513. 10002.log
    BackupType=Differential
    JobName=all
    SysState=J:\sysstate\20050513. 10002.bkf
 
Existing backups on this backup disk:
------------------------------------------
J:\all\20050510. 10002.bkf
J:\all\20050511. 10005.bkf
J:\all\20050512. 10002.bkf
J:\sysstate\20050510. 10002.bkf
J:\sysstate\20050511. 10005.bkf
J:\sysstate\20050512. 10002.bkf
 
Disk Space Before Backup Began: 52,028,325,888
 
Executing Commands:
 
        ntbackup backup "@C:\WINDOWS\backups\all.bks" /j "Differential Backup" /f "J:\all\20050513. 10002.bkf" /d "Differential - all" /v:yes /r:no /l:s /m Differential /snap:off /rs:no /hc:off
        ntbackup backup "@C:\WINDOWS\backups\sysstate.bks" /j "System State Backup" /f "J:\sysstate\20050513. 10002.bkf" /d "System State" /v:yes /r:no /l:s /m normal /snap:off /rs:no /hc:off
Disk Space After Backup: 48,510,865,408
0
 
LVL 97

Expert Comment

by:Lee W, MVP
ID: 13993417
There has been a problem including the log files - I'm not quite sure what it is, but it should be something I can figure out when I get the chance.

I'll post the script shortly.
0
 
LVL 97

Accepted Solution

by:
Lee W, MVP earned 2000 total points
ID: 13993427
One other thing - I backup to removable hard drives, not tape.

Here's the script:

@echo off
If "%1" == "" Goto Help
If "%1" == "/?" Goto Help
If "%1" == "-?" Goto Help

If /i "%1" == "prepare" Goto Prepare
Echo Backup started at %date% %time% by %username% >> %backupdrive%\history.log

Rem Check for the backup drive
If Exist F:\backup.txt SET BackupDrive=F:
If Exist H:\backup.txt SET BackupDrive=H:
If Exist I:\backup.txt SET BackupDrive=I:
If Exist J:\backup.txt SET BackupDrive=J:
If Exist K:\backup.txt SET BackupDrive=K:
If Exist L:\backup.txt SET BackupDrive=L:
If Exist M:\backup.txt SET BackupDrive=M:
If Exist N:\backup.txt SET BackupDrive=N:
If Exist O:\backup.txt SET BackupDrive=O:
If Exist P:\backup.txt SET BackupDrive=P:
If Exist Q:\backup.txt SET BackupDrive=Q:
If Exist T:\backup.txt SET BackupDrive=T:
If Exist U:\backup.txt SET BackupDrive=U:
If Exist W:\backup.txt SET BackupDrive=W:
If Exist X:\backup.txt SET BackupDrive=X:
If Exist Y:\backup.txt SET BackupDrive=Y:
If Exist Z:\backup.txt SET BackupDrive=Z:
If "%backupdrive%" == "" (
    set Error=No backup drive found.  Please connect a backup drive.
    Goto Error
)

If /i "%1" == "diff" Set BackupType=Differential
If /i "%1" == "full" Set BackupType=Normal
If "%BackupType%" == "" (
    Set Error=Invalid backup type specified or incorrect sequence of parameters.
    Goto Error
)

If /i "%2" == "all" (
    Set JobName=all
    Rem Stop the mysql database to ensure correct backup of data
    Net Stop MySql >> %backupdrive%\history.log 2>>&1
)
If /i "%2" == "full" (
    Set JobName=full
    Rem Stop the mysql database to ensure correct backup of data
    Net Stop MySql >> %backupdrive%\history.log 2>>&1
)
If /i "%2" == "users" Set JobName=users
If /i "%2" == "sgp" (
    set JobName=sgp
    Rem Stop the mysql database to ensure correct backup of data
    Net Stop MySQL >> %backupdrive%\history.log 2>>&1
)
If /i "%2" == "groups" set JobName=groups
If /i "%2" == "accting" set JobName=accting
IF /i "%2" == "slides" set JobName=slides
If /i "%2" == "sysstate" set JobName=sysstate
If "%jobname%" == "" (
    Set Error=Invalid job name specified or incorrect sequence of parameters.
    Goto Error
)

Set BackupFile=%backupdrive%\%jobname%\%date:~-4%%date:~4,2%%date:~7,2%.%time:~0,2%%time:~3,2%%time:~6,2%.bkf
Set BackupLogFile=%date:~-4%%date:~4,2%%date:~7,2%.%time:~0,2%%time:~3,2%%time:~6,2%.log
Set SysState=%backupdrive%\sysstate\%date:~-4%%date:~4,2%%date:~7,2%.%time:~0,2%%time:~3,2%%time:~6,2%.bkf

Echo Backup Request Executed on %date% at %time% by %username% > %temp%\~backuplogemail.tmp
Echo. >> %temp%\~backuplogemail.tmp
Echo Job Variables: >> %temp%\~backuplogemail.tmp
Echo        BackupDrive=%backupdrive% >> %temp%\~backuplogemail.tmp
Echo        BackupFile=%backupfile% >> %temp%\~backuplogemail.tmp
Echo        BackupLogFile=%BackupLogFile% >> %temp%\~backuplogemail.tmp
Echo        BackupType=%backupType% >> %temp%\~backuplogemail.tmp
Echo        JobName=%jobname% >> %temp%\~backuplogemail.tmp
Echo        SysState=%sysState% >> %temp%\~backuplogemail.tmp

Echo. >> %temp%\~backuplogemail.tmp
Echo Existing backups on this backup disk: >> %temp%\~backuplogemail.tmp
Echo ------------------------------------------ >> %temp%\~backuplogemail.tmp
Dir /s /b %backupdrive%\*.bkf >> %temp%\~backuplogemail.tmp
echo. >> %temp%\~backuplogemail.tmp
For /f "tokens=3" %%a in ('dir %backupdrive%\ ^| find /i "bytes free"') Do Echo Disk Space Before Backup Began: %%a >> %temp%\~backuplogemail.tmp

Echo Executing commands: >> %backupdrive%\history.log
Echo. >> %temp%\~backuplogemail.tmp
Echo Executing Commands: >> %temp%\~backuplogemail.tmp
Echo. >> %temp%\~backuplogemail.tmp

If "%jobname%" NEQ "sysstate" (
    Echo ntbackup backup "@%windir%\backups\%jobname%.bks" /j "%backupType% Backup" /f "%BackupFile%" /d "%backupType% - %jobname%" /v:yes /r:no /l:s /m %backupType% /snap:off /rs:no /hc:off >> %backupdrive%\history.log
    Echo         ntbackup backup "@%windir%\backups\%jobname%.bks" /j "%backupType% Backup" /f "%BackupFile%" /d "%backupType% - %jobname%" /v:yes /r:no /l:s /m %backupType% /snap:off /rs:no /hc:off >> %temp%\~backuplogemail.tmp
    ntbackup backup "@%windir%\backups\%jobname%.bks" /j "%backupType% Backup" /f "%BackupFile%" /d "%backupType% - %jobname%" /v:yes /r:no /l:s /m %backupType% /snap:off /rs:no /hc:off
    Copy "%userprofile%\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\*.log" %backupdrive%\%jobname%\%backuplogfile%
    Type "%backupdrive%\%jobname%\%backuplogfile%" >> %temp%\~backuplogemail.tmp
    If Exist "%backupdrive%\%jobname%\%backuplogfile%" Del "%userprofile%\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\*.log"
)
Rem Record the command to the log file
Echo         ntbackup backup "@%windir%\backups\sysstate.bks" /j "System State Backup" /f "%sysstate%" /d "System State" /v:yes /r:no /l:s /m normal /snap:off /rs:no /hc:off >> %temp%\~backuplogemail.tmp
Echo ntbackup backup "@%windir%\backups\sysstate.bks" /j "System State Backup" /f "%sysstate%" /d "System State" /v:yes /r:no /l:s /m normal /snap:off /rs:no /hc:off >> %backupdrive%\history.log

REm Execute the command
ntbackup backup "@%windir%\backups\sysstate.bks" /j "System State Backup" /f "%sysstate%" /d "System State" /v:yes /r:no /l:s /m normal /snap:off /rs:no /hc:off

REM massage the logfile location.
Copy "%userprofile%\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\*.log" %backupdrive%\sysstate\%backuplogfile%
Type "%backupdrive%\sysstate\%backuplogfile%" >> %temp%\~backuplogemail.tmp
If Exist "%backupdrive%\sysstate\%backuplogfile%" Del "%userprofile%\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\*.log"

Rem Append free space count on disk to email message
For /f "tokens=3" %%a in ('dir %backupdrive%\ ^| find /i "bytes free"') Do Echo Disk Space After Backup: %%a >> %temp%\~backuplogemail.tmp
Set JobExecuted=True

Echo Script completed at %date% %time% >> %backupdrive%\history.log
Echo ------------------------------------------------------------------ >> %backupdrive%\history.log

Goto End

:Prepare
Echo Preparing Disk %2 for use as a backup disk > %2\history.log
If "%2" == "" (
    Set Error=No drive letter specified to prepare.
    Goto Error
)
Label %2 Backup >> %2\history.log 2>>&1
Copy %windir%\scripts\support\backup.txt %2\ >> %2\history.log 2>>&1
md %2\all >> %2\history.log 2>>&1
md %2\full >> %2\history.log 2>>&1
md %2\users >> %2\history.log 2>>&1
md %2\sgp >> %2\history.log 2>>&1
md %2\groups >> %2\history.log 2>>&1
md %2\slides >> %2\history.log 2>>&1
md %2\accting >> %2\history.log 2>>&1
md %2\sysstate >> %2\history.log 2>>&1
md %2\logs >> %backupdrive%\history.log 2>>&1
compact /c /s:f:\ /i >> %2\history.log 2>>&1
Echo Disk prepared on %date% %time% >> %2\history.log
Echo ------------------------------------------------------------------ >> %2\history.log
Goto End

:Error
Echo %error%
Echo %error%  >> %backupdrive%\history.log
Echo %error%  >> %temp%\~backuplogemail.tmp
Sendmail -v -to user@mydomain.com -file %temp%\~backuplogemail.tmp -h smtp.mydomain.com -from user@mydomain.com -s "NT Backup FAILED" >> %backupdrive%\history.log 2>>&1

Goto End

:Help
Echo --------------------------------------------------------------------------
Echo This is my company Backup Script. This script utilizes the Windows Backup
Echo software, setting it's parameters based on the command line information
Echo provided after the script name.  The order of the parameters is critical
Echo for a successful backup.
Echo.
Echo %0 - USAGE:
Echo    %0 BackupType Job
Echo    %0 Prepare X:
Echo.
Echo     Backup Types:    Jobs:          
Echo       Diff             Slides
Echo       Full             Accting
Echo                        Groups (Includes I Drive ^& Accounting)
Echo                        SGP (Includes a Database ^& Web)
Echo                        Users (Includes e-mail)
Echo                        All (Excludes graphics folder)
Echo                        Full (Backs up EVERYTHING on ALL drives)
Echo.
Pause
Echo    The Prepare option prepares drive X: to act as a backup disk.  It puts
Echo    the necessary marker file on the disk and creates the sub folders for
Echo    each job.  In addition, it sets the compression attribute to compress
Echo    all files stored on the disk.
Echo.
Echo    Examples:
Echo.
Echo        %0 diff accting     ^<-- Backs up changed accounting files
Echo        %0 full users       ^<-- Backs up old and new files in user
Echo                       folders, including Email
Echo        %0 diff slides      ^<-- Backs up only new or changed slides
Echo                       since last backup
Echo        %0 prepare x:       ^<-- Prepares x: for use as a backup disk

:End
Rem Restart SQL if stopped
net start | find /i "mysql" > NUL
If errorlevel 1 Net Start MySQL >> %backupdrive%\history.log 2>>&1

REM Send email with backup log

If /i "%JobExecuted%" == "True" Sendmail -v -to user@mydomain.com -file %temp%\~backuplogemail.tmp -h smtp.mydomain.com -from user@mydomain.com -s NTBackupLog >> %backupdrive%\history.log 2>>&1

REM Remove the e-mail text file
If Exist %temp%\~backuplogemail.tmp del %temp%\~backuplogemail.tmp

Rem Cleanup Environment variables
Set Jobname=
Set backupType=
Set backupdrive=
Set backupfile=
Set backuplogfile=
Set Error=
Set JobExecuted=
0
 
LVL 11

Author Comment

by:KaliKoder
ID: 13999948
Thanks Leew, you have got a very good script, it has some good pointers. I would get back to you shortly.
0
 
LVL 11

Author Comment

by:KaliKoder
ID: 14011713
Thank you very much for your help Leew. I was able to achieve the objective. I took the lines from your script, where you actually copy the log file generated and give it a unique name with dd:mm:yyyy-mm:ss format. This was very important to me, because having that in the name means no two log file names can be the same. I then copied them to a specific directory at the end of the script and used the mailsend program (a command line freeware) that sends an email to a list of folks listed in a text file, with a predefined message also listed in a text file, and also attaches the files you specify.

Thanks again
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

We have adopted the strategy to use Computers in Student Labs as the bulletin boards. The same target can be achieved by using a Login Notice feature in Group policy but it’s not as attractive as graphical wallpapers with message which grabs the att…
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Watch the video to learn how one can deal with PST file corruption issue with an outstanding Kernel for Outlook PST Repair Tool easily. Using this tool, non-technical users can swiftly perform the repair process to restore their essential data witho…
Suggested Courses

589 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